Numbers index/match function that returns multiple values

I’m guessing this isn’t possible, but-


In table 1, I have a list of books and metadata. In table 2, I have a list of authors and metadata. I want table 2 to automatically pull the titles of books by each author. If there is only one book by an author, I can use index and match. But if there are multiple books, is there a formula to pull all titles as multiple lines in a single cell?

MacBook Air 13″, macOS 14.5

Posted on Jul 26, 2024 12:48 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 27, 2024 2:26 PM

=IF(A2≠"",TEXTJOIN(", ",TRUE,REGEX.EXTRACT("~"&TEXTJOIN("~",FALSE,Table 1::A:B)&"~","(?<=~"&A2&"~)(.*?)(?=~)",0)),"")


In this case, it is textjoining the two columns like this: ~Author A~Book 1~Author B~Book 2~ and so on. REGEX.EXTRACT is looking behind for "~Author A~" and ahead for the next "~" and returning the characters between them. If a book is titled "Author A" but was not written by Author A, it would return the next author's name instead of a book title. A book titled "Author A" written by Author A would be okay.


Table 1 column C is not required.

11 replies
Question marked as Top-ranking reply

Jul 27, 2024 2:26 PM in response to mkjohnnie

=IF(A2≠"",TEXTJOIN(", ",TRUE,REGEX.EXTRACT("~"&TEXTJOIN("~",FALSE,Table 1::A:B)&"~","(?<=~"&A2&"~)(.*?)(?=~)",0)),"")


In this case, it is textjoining the two columns like this: ~Author A~Book 1~Author B~Book 2~ and so on. REGEX.EXTRACT is looking behind for "~Author A~" and ahead for the next "~" and returning the characters between them. If a book is titled "Author A" but was not written by Author A, it would return the next author's name instead of a book title. A book titled "Author A" written by Author A would be okay.


Table 1 column C is not required.

Jul 27, 2024 6:14 AM in response to mkjohnnie

I hope one day a multiple-match function will come out to make multiple matches easier. But there are several ways to do it. Here is one. I can't say I have extensively tested it so let me know if it has any problems.


Add a column to your data table to put the author and book together.

Table 1::C2 =A2&":"&B2

Fill down to complete the column

You can hide this column later


Formula in Table 2::B2 =IF(A2≠"",TEXTJOIN(", ",TRUE,REGEX.EXTRACT("~"&TEXTJOIN("~",TRUE,Table 1::C)&"~","(?<=~"&A2&":)(.*?)(?=~)",0)),"")

Fill down


Explanation (using Author A as the input):

  1. It uses TEXTJOIN to join together all the Author::Book strings from Table 1::C with a ~ character between them, plus it puts one in front and back of the entire string.
  2. It then uses REGEX.EXTRACT to find all the times "~Author A:any characters~" appears in that textjoined string and makes an array of all the "any characters" it found, which will be the book titles for that author. More precisely it does a lookbehind (?<=~"&A2&":) to find "~Author A:" and does a look ahead (?=~) to find the "~" after the book title and it returns the text between those two things, for each occurrence it finds.
  3. Finally it uses TEXTJOIN to join all of those book titles together with a comma between them.


If I could guarantee a book title would not be the same as the name of an author (such as Carl Sagan authoring a book about space and someone else writing a book about him titled titled "Carl Sagan"), the extra column in Table 1 would not be needed and a slightly different formula would be used.


The tilde character "~" is not an acceptable character in a book title or author name. If there are any, you will need to use a different character in the formulas or in the authors/books. The ":" can also be problematic in specific cases but probably not a problem.

Jul 27, 2024 9:30 PM in response to mkjohnnie

If you keep increasing the scope like this, at some point you will have to use another method. I am not an expert on regular expressions (those weird combinations of characters used for the search) but the version used in Numbers does not appear to allow wild cards in a lookahead or lookbehind.


Table 1 C2 =IF(COUNTMATCHES(A2,'Table 1-1'::$A$2),B2,"~")

Fill down


Table 1-1::B2 =SUBSTITUTE(TEXTJOIN(", ",TRUE,Table 1::C),REGEX(", ~|~, "),"")


The formula in Table 1-1 should not have to be this complicated except that there is a bug in Number's version of the TEXTJOIN so some extra work is required. The formula in Table 1 should have been written to result in either the book title or a null string "", not "~". TEXTJOIN should have been able to join the book titles with commas between them, ignoring the null strings, and that should have been the end of it. Unfortunately TEXTJOIN does not ignore null strings so you end up with bunches of repeated commas. The formulas I provided are one way to work around that bug until Apple decides to fix it (unless they already fixed it in versions later than 13.1, the version I have).

Jul 27, 2024 3:25 AM in response to mkjohnnie

Hi mkjohnnie,


We are not looking over your shoulder. A screen shot of a small part of table 1 (books and metadata) and another screenshot of a small part of table 2 (authors and metadata) will help us to see what you see.


Are the metadata the lookup values for INDEX and MATCH? Or do the metadata somehow contain lookup values?

More details may lead to a solution.


Regards,

Ian.

Jul 27, 2024 11:14 AM in response to Badunit

Thanks for this!


I set up my tables as you suggested, and copied and pasted your formula (making sure it referenced the right cells), but I’m getting this error: REGEX.EXTRACT couldn’t find a match for the regular expression “(?<=~Author A:)(.*?)(?=~)”.


If it matters, a lot of my book titles do have a “:” character, but none of them have the same name as an author.

Jul 27, 2024 2:12 PM in response to mkjohnnie

And if it’s not too much trouble to add one more question-


I’m also trying to filter the results so only some titles appear. When I only had one title to worry about, I made an extra hidden column (G) in Table 1 that used if functions to check my conditions and output YYYAuthorYYY if the conditions were met (or NNNAuthorNNN if not). Then, in Table 2, I used the formula INDEX(Books Owned::Title,MATCH("YYY"&"*"&Author&"*"&"*YYY*",Books Owned::G,0)) to get the relevant title. (The wildcards are in case a book has multiple authors.) I’m not very familiar with regex, so I’m not sure how to make that work here though?

Jul 27, 2024 4:19 PM in response to Badunit

Unfortunately, no, it’s too much data for filters to really be manageable.


You’ve been so helpful! One last question: when using REGEX.EXTRACT to find "~Author A:any characters~”, is it possible to add wildcards between “Author A” and “:”? Sometimes a book has “Author A & Author B”, and I’d want it to show up in the cells for both authors.

Jul 27, 2024 12:39 PM in response to mkjohnnie

Your Table 1 has an "Author A" in column A with the same capitalization and spaces and no extra characters of any kind? If it appears so, try copying the author's name from Table 1 and pasting it into Table 1-1::A2 so you can be sure it is the same and see if that works.


If not that, is the formula in Table 1::C2 correct? Does it give results like in my Table 1 with a ":" directly after the author's name, no added spaces before or after?


If capitalization may be a problem, we can turn the author name into all caps in Table 1::C and in the search formula so it is no longer an issue. If extraneous spaces may be a problem, we can use TRIM to remove them in Table 1::C.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Numbers index/match function that returns multiple values

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