Apple Intelligence is now available on iPhone, iPad, and Mac!

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Text cell find row with matching content in any cell ("dictionary" / "lookup" table)

Hello!


I am fairly new to the Numbers / table game and am struggling with a formula solving the following problem, maybe someone here can help:


The formula should return the first column of a row if the target value (text) is found within any of the subsequent columns of that row. A whole table ("dictionary" or "lookup table") should be searched.


I think this screenshot makes it more clear:


Any help would be appreciated!

MacBook Pro Apple Silicon

Posted on Feb 22, 2023 2:39 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 22, 2023 3:28 AM

You can try something like this:




The formula in H2 of Lookup Table, filled down:


=TEXTJOIN("|",TRUE,B2:E2)


This takes the values in the cells in the specific range and joins (concatenates) them with a separating character you specify. Having them all in one column makes a lookup possible.


The formula in B2 of Data Table, filled down:


=XLOOKUP("*"&$A2&"*",Lookup Table::G,Lookup Table::A,"not found",2)


The * is the symbol denoting a wildcard. You need that because the text you are searching for from column A of the data table will be surrounded by other text in the Join column in Lookup Table. Note the required 2 argument in XLOOKUP if you need to use a wildcard like this.


More on wildcards here:


Functions that accept conditions and wildcards as arguments - Apple Support


More on XLOOKUP here:


XLOOKUP - Apple Support



If your region uses , as a decimal separator then replace the , in the formulas with ;


SG



5 replies
Question marked as Top-ranking reply

Feb 22, 2023 3:28 AM in response to jjffjjss

You can try something like this:




The formula in H2 of Lookup Table, filled down:


=TEXTJOIN("|",TRUE,B2:E2)


This takes the values in the cells in the specific range and joins (concatenates) them with a separating character you specify. Having them all in one column makes a lookup possible.


The formula in B2 of Data Table, filled down:


=XLOOKUP("*"&$A2&"*",Lookup Table::G,Lookup Table::A,"not found",2)


The * is the symbol denoting a wildcard. You need that because the text you are searching for from column A of the data table will be surrounded by other text in the Join column in Lookup Table. Note the required 2 argument in XLOOKUP if you need to use a wildcard like this.


More on wildcards here:


Functions that accept conditions and wildcards as arguments - Apple Support


More on XLOOKUP here:


XLOOKUP - Apple Support



If your region uses , as a decimal separator then replace the , in the formulas with ;


SG



Feb 22, 2023 4:10 AM in response to jjffjjss

I have a slight change to what SGIII suggested to prevent matches of substrings ("eagle" is within the word "beagle" so "eagle" came back as a dog not a bird).



Table 1::B2 ="|"&TEXTJOIN("|",TRUE,C2:J2)&"|"

Fill down to complete the column.

This formula puts a "|" on both sides of all the words


Table 2::B2 =XLOOKUP("*|"&$A2&"|*",Table 1::B,Table 1::A,"not found",2)

Fill down to complete the column

The formula looks for the specified word with a "|" character on both sides of it, ensuring only whole-word matches are returned.


Hide column Table 1::B. If you later add new columns to the right of the table you will have to unhide the column and adjust the formula to include them. If you add new columns in the middle of the table you will not.

Mar 1, 2023 11:33 AM in response to jjffjjss

Thanks for your great responses, those approaches work very well!


I got another requirement though that makes it a little more tricky:


In the search value of the XLOOKUP function (e.g. the animal names), I want any substring (preferably separated by spaces) that matches the search range (e.g. criteria) to be a "hit".


I adjusted the example so the animal names also contain the pet names to illustrate the problem:



Do you know a solution for this too?


Thanks for any replies!


Mar 2, 2023 2:13 AM in response to jjffjjss

This is beginning to sound as if you're trying to convert 2D data to 1D, to make it easier to perform lookups, etc.


If so, you may find this Shortcut helpful (clink link in Safari to inspect and download).


Click a cell in 2D table and choose 'Flatten 2D data to 1D' from the shortcuts menu (if you have pinned it there after clicking the circled i).





SG






Mar 2, 2023 6:44 AM in response to jjffjjss

In the formula for Table 2::B2, replace $A2 with TEXTBEFORE(A2," ")

This will trim off everything to the right of the first space (including the space itself) leaving only "Labrador". This will work as long as none of your animal types have a space in them.


SGIII makes a good point about flattening your table from 2D to 1D. It makes lookups so much simpler without having to do tricks like concatenating all the words in each row so there is something to use for a lookup.

Text cell find row with matching content in any cell ("dictionary" / "lookup" table)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.