Home > Blockchain >  In Google Sheets, can the IF function handle a query as both arguments?
In Google Sheets, can the IF function handle a query as both arguments?

Time:08-17

What I've tried...

  1. Various syntax variations
  2. Google searches

What I'd like, in simple terms...

=IF(B2=" ",QUERY(RawData!$A$2:$I, "SELECT A,B,C,D,E WHERE H = '"&A3&"'"),QUERY(RawData!$A$2:$I, "SELECT A,B,C,D,E WHERE H = '"&A3&"' AND A = '"&A4&"'"))

In other words, if B2 is blank, return ALL results; otherwise, return results based on the text found in B2.

The formula DOES work if both A3 and A4 have values. It does NOT work if A4 is blank.

I can't share the sheet (gov't data), sorry, but hopefully I've explained the issue clearly enough.

Now I'm thinking that maybe a FILTER function would be better?

Thoughts?

CodePudding user response:

IF it:

=IF(B2="", 
 QUERY(RawData!$A$2:$I, "SELECT A,B,C,D,E WHERE 9=9 "&
 IF(A3="",," and H = '"&A3&"'")),
 QUERY(RawData!$A$2:$I, "SELECT A,B,C,D,E WHERE 9=9 "&
 IF(A3="",," and H = '"&A3&"'")&
 IF(A4="",," and A = '"&A4&"'")))

CodePudding user response:

Since you did't include the enter image description here

Use this formula

=IF(B2="",
 QUERY(RawData!$A$2:$I, "SELECT A,B,C,D,E WHERE H = '"&A3&"'"),
 QUERY(RawData!$A$2:$I, "SELECT A,B,C,D,E WHERE H = '"&A3&"' AND A = '"&A4&"'"))

Result

1 - When B2 is Empty ""

enter image description here

2 - When B2 is Not Empty

enter image description here

  • Related