Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Spreadsheet multiple column filter using OR

I have a Google Spreadsheet with 3 columns that are either blank or have a value. I want to get the count of the number of rows that has A and either B or C populated. If I were writing a SQL query it would be

select count(*) 
from Table 
where A is not null and (B is not null or C is not null)

But I can't for the life of me figure out how to get this in a Google Spreadsheet

like image 773
thefroatgt Avatar asked Mar 25 '10 02:03

thefroatgt


People also ask

How do I filter data in multiple columns in Google Sheets?

Step 2: Apply Filter to Multiple Columns To do so, click cell A1 and then click the Data tab and then click Create a filter: Next, click the Filter icon next to Region and then click Filter by condition.

Can you apply multiple filters in Google Sheets?

Now you can filter data by another column. Click on the filter button for total sales (G2), select Number Filters, and click Greater Than. Note that you could also choose Equals, Does Not Equal, Less Than, etc.

How do I filter multiple columns with one criteria?

Enter this formula: =ISERROR(MATCH("Helen",A2:C2,0)) into cell D2, and then drag the fill handle down to the cells to apply this formula, and the FALSE and TRUE displayed into the cells, see screenshot: Note: In the above formula: “Helen” is the criteria that you want to filter rows based on, A2:C2 is the row data.


1 Answers

The formula below should do what you are after:

=ROWS(FILTER(A2:A, NOT(ISBLANK(A2:A)), NOT(ISBLANK(B2:B))+NOT(ISBLANK(C2:C)) ))

And to explain:

  • ROWS counts the rows of the argument (filtered, in our case)
  • FILTER returns the rows of arg1 (A2:A) that all subsequent arguments match
  • The + (addition) symbol combines two predicates with a logical OR

Finally, if you are not using header columns you can change the references from A2:A to A:A

Alternatively, you can use the QUERY function:

(Broken into multiple lines for readability)

=ROWS(QUERY(A2:C, 
    "SELECT A WHERE A IS NOT NULL AND (B IS NOT NULL OR C IS NOT NULL)"))

For more information on the syntax of the queries, see the Visualization API Query Language Reference and specifically the Language Reference

like image 154
Richard Szalay Avatar answered Oct 06 '22 00:10

Richard Szalay