Search code examples
arraysgoogle-sheetssplitflattengoogle-query-language

Excel/Sheets combine row column content and find respecting value


Sheet1

I have this data of car price based on its color on Sheet1

Car Red Blue Black White
BMW 100 120 90 120
FIAT 90 100 110 120
FORD 70 80 80 80

Sheet2

I want these cars and colors combined in column A. In column B, I want to display its respective price. I have achieved to display column A with this formula, but I am open to it if there is any suggestion. For column B, I can not figure out yet how to accomplish the goal.

The formula for column A

=ArrayFormula(transpose(split(rept(concatenate(Sheet1!A2:A&char(9)),counta(Sheet1!B1:Sheet1!E1)),char(9)))
&" "&transpose(split(concatenate(rept(Sheet1!B1:Sheet1!E1&char(9),counta(Sheet1!A2:Sheet1!A))),char(9))))

I got this formula from here

The expected output in Sheet2

Car Color Price
BMW Red 100
BMW Blue 120
BMW Black 90
BMW White 120
FIAT Red 90
FIAT Blue 100
FIAT Black 110
FIAT White 120
FORD Red 70
FORD Blue 80
FORD Black 80
FORD White 80

Solution

  • use:

    =INDEX(QUERY(SPLIT(FLATTEN(IF(B2:E="",,A2:A&" "&B1:E1&"×"&B2:E)), "×"), 
     "where Col2 is not null"))
    

    enter image description here