Search code examples
google-sheetssumgoogle-sheets-formulagoogle-query-languageimportrange

Blank vs #REF response to INDEXing QUERY search


I'm making an analytic sheet in Google Spreadsheets and am using the QUERY function bundled in an INDEX function. However, I'm encountering a weird error. Sometimes, when the query returns no response, it gives a #REF error, reading "Function INDEX parameter 2 value is 2. Valid values are between 0 and 1 inclusive." This makes sense to me, since there's nothing being returned. However, there are also some times that I don't get an error, and the function returns just a blank cell.

When I remove the INDEX portion of the function, both times (error and blank cell) read the same thing, which is just "sum FEE", ie the operation and column title. I'm also using IMPORTRANGE so the data portion of the query function is a little tricky, but just know that it's calling a variable name of the original spreadsheet.

Here are the functions from both error response and blank response.

Error: =index(QUERY(IMPORTRANGE(Ranges!$B$1,"'"&$A6&"'"&"!"&"A:O"), "Select sum(Col8) where Col3=''",1),2,0)

Blank: =index(QUERY(IMPORTRANGE(Ranges!$B$1,"'"&$A7&"'"&"!"&"A:O"), "Select sum(Col8) where Col3 is null",1),2,0)

Solution

  • try:

    =IFERROR(INDEX(QUERY(IMPORTRANGE(Ranges!$B$1,"'"&$A6&"'"&"!"&"A:O"), 
     "select sum(Col8) where Col3=''", 1), 2, 0))