Google Sheets Cartesian product with transposed columns

Viewed 229
2 Answers

There is a relatively simple way to do than using FLATTEN:

={
  ARRAYFORMULA(A1:C1), "Product";
  ARRAYFORMULA(
    VLOOKUP(
      FILTER(
        FLATTEN(ROW(D2:F) + 0 * ISBLANK(D2:F)),
        FLATTEN(D2:F <> "")
      ),
      FILTER(
        {ROW(A2:A), A2:C},
        A2:A <> ""
      ),
      {2, 3, 4},
      0
    )
  ),
  FILTER(
    FLATTEN(D2:F),
    FLATTEN(D2:F <> "")
  )
}

enter image description here

Not sure why ARRAYFORMULA(ROW(D2:F)) gives row wise row numbers and not a 2d matrix with the row numbers. Could've remove 0 * ISBLANK(D2:F) otherwise...

Please use the following query formula:

=QUERY(QUERY({A:D;A2:C,E2:E;A2:C,F2:F},"where Col1 is not null order by Col1"), "where Col4 is not null")

enter image description here

Functions used:

Related