Search code examples
google-sheetsformulasgoogle-sheets-query

Condense a multi-row, multi-column formula output into a single row


I have a QUERY formula that imports the first three rows of some data, "Select Col1, Col2 Order by Col2 desc limit 3", producing an output like this:

   |   c1   | c2
r1 | Red    | 53
r2 | Blue   | 45
r3 | Yellow | 15

I'd like to get need those same formula values to look like this:

   |  c1  |  c2  |  c3  |  c4  |   c5   |  c6 
r1 | Red  |  53  | Blue |  45  | Yellow |  15

where the whole output is in a single row.
Is this possible?


Solution

  • Yep, wrap your query function with TEXTJOIN

    =TEXTJOIN(" ", TRUE, QUERY(....))
    

    To get multiple columns, you would wrap this with SPLIT, and instead of joining on " ", join with some arbitrary delimiter that is guaranteed to not be in the query result. If you expect a wide variety of text to be a possible formula result, you may need to get quite creative, or use unicode.

    =SPLIT(TEXTJOIN("-;-", TRUE, QUERY(....)), "-;-", FALSE)