Search code examples
arraysgoogle-sheetstransposearray-formulasgoogle-sheets-query

Google Sheets formula to add column to array?


In Google Sheets I have a 2D array, with unpredictable size, returned by a Query:

08-02-2020 | Red
10-02-2020 | Brown
12-02-2020 | Green

Now I'd like to add a third column containing the constant "Golf" to the array. For instance:

08-02-2020 | Red   | Golf
10-02-2020 | Brown | Golf
12-02-2020 | Green | Golf

Is there a way to add a column to the initial array? Could not tame this with arrayformula...

current query:

=query(transpose(query(transpose(query(transpose(query(A1:11)),"select * where Col1 > 0")),"select * where Col1 <> ''",1)),"select * where Col2 > 0")

Solution

  • try:

    =ARRAYFORMULA(QUERY(TRANSPOSE(QUERY(TRANSPOSE(QUERY(TRANSPOSE(QUERY(A1:11)), 
     "where Col1 > 0")), 
     "where Col1 <> ''", 1)), 
     "select "&TEXTJOIN(",", 1, "Col"&TRANSPOSE(ROW(INDIRECT("A1:A"&COLUMNS(
     QUERY(TRANSPOSE(QUERY(TRANSPOSE(QUERY(TRANSPOSE(QUERY(A1:11)), 
     "where Col1 > 0")), "where Col1 <> ''", 1)), 
     "where Col2 > 0"))))), "'Golf'")&" where Col2 > 0"))