Search code examples
google-sheetsgoogle-sheets-formulaarray-formulasgoogle-sheets-querygoogle-query-language

How to use multiple TEXT funtions in QUERY's second argument?


Using the the Query function with text() or To_text() function in the second argument works completely fine for me, as in this example:

=QUERY({June2019!$M$3:$Z;July2019!$M$3:$Z;August2019!$M$3:$Z},"select 
 SUM(Col5) where Col1 > "&text((Column()-2)*5,"#")&" and Col1 <= 
 "&text((Column()-1)*5,"#")&" label SUM(Col5) ''")

However as soon as I use this query function in a more complex way, I get one of two errors:

=IF((ISBLANK(B27:27)=False), 
 (QUERY({June2019!$M$3:$Z;July2019!$M$3:$Z;August2019!$M$3:$Z},"select 
 SUM(Col5) where (Col1 > "&text((Column()-2)*5,"#")&") and (Col1 <= 
 "&text((Column()-1)*5,"#")&") label SUM(Col5) 
 ''"))/(QUERY({June2019!$M$3:$Z;July2019!$M$3:$Z;August2019!$M$3:$Z},"select 
 COUNT(Col5) where (Col1 > "&text((Column()-2)*5,"#")&") and (Col1 <= 
 "&text((Column()-1)*5,"#")&") label COUNT(Col5) ''")),)

Error: Query completed with an empty output.


Error: Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "Col1 "" at line 1, column 24. Was expecting one of: "(" ... "(" ...

Any help would be appreciated


Solution

  • try:

    =ARRAYFORMULA(IF(ISBLANK(B27:27)=FALSE, 
     QUERY({June2019!$M$3:$Z; July2019!$M$3:$Z; August2019!$M$3:$Z}, 
     "select SUM(Col5) 
      where Col1 >  "&(COLUMN()-2)*5&" 
        and Col1 <= "&(COLUMN()-1)*5&"
      label SUM(Col5)''")/
     QUERY({June2019!$M$3:$Z; July2019!$M$3:$Z; August2019!$M$3:$Z},
     "select COUNT(Col5) 
      where Col1 >  "&(COLUMN()-2)*5&"
        and Col1 <= "&(COLUMN()-1)*5&"
      label COUNT(Col5)''"), ))
    

    or:

    =ARRAYFORMULA(IF(ISBLANK(B27:27)=FALSE, 
     QUERY({June2019!$M$3:$Z; July2019!$M$3:$Z; August2019!$M$3:$Z}, 
     "select avg(Col5) 
      where Col1 >  "&(COLUMN()-2)*5&" 
        and Col1 <= "&(COLUMN()-1)*5&"
      label avg(Col5)''"), ))