I have a sheet that looks like this:
I'd like to have a formula that queries the data in A2:E7 and pulls the corresponding column headers from A1:E1. The trick is that values may appear more than once.
Sample results would look like this:
Example sheet: https://docs.google.com/spreadsheets/d/1Zr_q8nwYPixUjxWYdT-WlQLiGIUzqoK_cSQXSp19nJg
use:
=ARRAYFORMULA(IFERROR(TRIM(SPLIT(IFNA(VLOOKUP(G1:G,
SPLIT(FLATTEN(QUERY(TRANSPOSE(QUERY(QUERY(UNIQUE(SPLIT(FLATTEN(
IF(A2:E="",,A2:E&"♂×"&A1:E1&"♀×"&A1:E1)), "×")),
"select Col1,max(Col2)
where Col2 is not null
group by Col1
pivot Col3"),
"offset 1", 0)),,9^9)), "♂"), 2, 0)), "♀"))))
=ARRAYFORMULA(ARRAY_CONSTRAIN(IFERROR(TRIM(SPLIT(IFNA(VLOOKUP(G1:G,
SPLIT(FLATTEN(QUERY(TRANSPOSE(QUERY(QUERY(UNIQUE(SPLIT(FLATTEN(
IF(A2:E="",,A2:E&"♂×"&A1:E1&"♀×"&A1:E1)), "×")),
"select Col1,max(Col2)
where Col2 is not null
group by Col1
pivot Col3"),
"offset 1", 0)),,9^9)), "♂"), 2, 0)), "♀"))), 9^9, 2))