google-sheetsgoogle-sheets-formula

How to List All Combinations of Values from Two Semicolon-Separated Columns in Excel/Sheets?


This might be a very beginner's question, but after searching unsuccessfully, I decided to ask for help, although I have the feeling that the answer is just within reach.

I'm working on a glossary in Excel/Sheets, where each row contains a list of related terms in two columns: Column A (English terms) and Column B (Spanish terms). Semicolons separate the terms in each column. For example:

English Spanish
a; b C; D
e F; G
h; i J

I need to output all possible combinations (like a Cartesian product) of the terms from Column A and Column B within the same row in separate rows. The result for the above example should be like this:

English Spanish
a C
a D
b C
b D
e F
e G
h J
i J

Can you help me with a formula or any other method to achieve that in Excel?

I tried SPLIT function, for example, which divides text around a specified character or string, and puts each fragment into a separate cell in the row, but that is not what I'm actually looking for, and would be extremely tedious to manually rearrange for hundreds or thousands of values.


Solution

  • In Google Sheets:

    =let( 
      get_, lambda(a, filter(a, len(A2:A), len(B2:B))), 
      split_, lambda(a, split(a, "; ", false)), 
      pairs, map(get_(A2:A), get_(B2:B), lambda(en, es, 
        torow(sort(split_(en) & "→" & tocol(split_(es)))) 
      )), 
      sort(split(tocol(pairs, 1), "→")) 
    )
    

    screenshot

    See let(), lambda(), filter(), split(), map(), torow(), tocol() and sort().