Search code examples
google-sheetsarray-formulas

How to use ARRAYFORMULA in query where clause?


In cell C1:C of my table I got 6 rows with ticket id's. I like to search different spreadsheets to search for those ticket id's and calculate the total hours spent on that ticket.

I have it working using the following formula:

=QUERY({IMPORTRANGE("SPREADSHEETID";"B3:B")\ARRAYFORMULA(TO_PURE_NUMBER(IMPORTRANGE("SPREADSHEETID";"F3:F")-IMPORTRANGE("SPREADSHEETID";"E3:E")))};"SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''")

In this example, C1 is where the ticket ID can be found.

Now I thought I could just wrap QUERY in a ARRAYFORMULA and use C1:C instead of just C1 but that won't work. Now I could just copy and paste the above formula in every cell but there must be a cleaner way.

ANSWER

I used the following formula to make it work, thanks to Max's answer below.

=QUERY(
{
  IMPORTRANGE("SPREADSHEETID";"B3:B")\
  ARRAYFORMULA(
    TO_PURE_NUMBER(
      IMPORTRANGE("SPREADSHEETID";"F3:F") - IMPORTRANGE("SPREADSHEETID";"E3:E")
    )
  )
};
"
SELECT Col1, SUM(Col2) 
WHERE Col1 = '" & JOIN("' OR Col1 = '";FILTER(C:C; C:C <> "")) & "' 
GROUP BY Col1 
LABEL SUM(Col2) ''
")

Solution

  • Sample formula is:

    =QUERY({A:B},"select * where Col1 = '"&JOIN("' or Col1 = '",FILTER(D2:D,D2:D<>""))&"'")

    enter image description here