Search code examples
google-sheetsgoogle-sheets-formulaarray-formulasgoogle-sheets-querygs-vlookup

Search 2 columns and return all matches in a transposed row


I'm trying to take this QUERY:

=IFERROR(TRANSPOSE(QUERY(Items!$A$2:$D,"SELECT D WHERE A = '"&A2&"' and B = '"&B2&"' and C = '"&C2&"' and C is not NULL and D is not NULL", 0)),)

And turn it into a formula that I can enter one time at the top of a sheet and have it apply to all cells in the column below.

I've tried manipulating various other formulas and answers I've found/received here on SO, but getting nothing but errors.

My sheet


Solution

  • =ARRAYFORMULA(IFERROR(VLOOKUP(Estimate!A2:A&Estimate!B2:B, 
     TRIM(IFERROR(SPLIT(TRIM(TRANSPOSE(QUERY(TRANSPOSE(
     {INDEX(QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(Items!A2:A&Items!B2:B))*(LEN(Items!C2:C)),
     Items!A2:A&Items!B2:B&"♦"&Items!C2:C, )), 1, 1), "♦")), 
     "select Col1,count(Col1) where Col1 is not null group by Col1 pivot Col2", 0),,1), IF(
     ISNUMBER(QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(Items!A2:A&Items!B2:B))*(LEN(Items!C2:C)),
     Items!A2:A&Items!B2:B&"♦"&Items!C2:C, )), 1, 1), "♦")), 
     "select count(Col1) where Col1 is not null group by Col1 pivot Col2", 0)),
     QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(Items!A2:A&Items!B2:B))*(LEN(Items!C2:C)), 
     Items!A2:A&Items!B2:B&"♦♥"&Items!C2:C, )), 1, 1), "♦")),
     "select count(Col1) where Col1 is not null group by Col1 pivot Col2 limit 0", 0), )})
     ,,999^99))), "♥"))), {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}, 0)))
    

    0