Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine COUNTIF with OR

In Google Spreadsheets, I need to use the COUNTIF function on a range with multiple criteria. So in the table below, I would need to have something like =COUNTIF(B:B,"Mammal"or"Bird") and return a value of 4.

A         |B
-------------------
Animal    | Type
-------------------
Dog       | Mammal
Cat       | Mammal
Lizard    | Reptile
Snake     | Reptile
Alligator | Reptile
Dove      | Bird
Chicken   | Bird

I've tried a lot of different approaches with no luck.

like image 823
VivaNOLA Avatar asked Jan 29 '14 00:01

VivaNOLA


People also ask

Can you use Countifs with or?

The result is 9 since there are 6 orders that are complete and 3 orders that are pending. In this example, the goal is to use the COUNTIFS function to count data with "OR logic".


3 Answers

One option:

=COUNTIF(B:B; "Mammal") + COUNTIF(B:B; "Bird")

According to the documentation:

Notes

COUNTIF can only perform conditional counts with a single criterion. To use multiple criteria, use COUNTIFS or the database functions DCOUNT or DCOUNTA.

COUNTIFS: This function is only available in the new Google Sheets.

Example:

=DCOUNTA(B:B; 2; {"Type"; "Mammal"; "Bird"})
like image 112
wchiquito Avatar answered Oct 22 '22 11:10

wchiquito


You can also use ArrayFormula around a SUM(COUNTIFS()) construct:

=ArrayFormula(SUM(COUNTIF(B:B,{"Mammal", "Bird"}))

Source: Google Docs Product Forum

like image 26
nanselm2 Avatar answered Oct 22 '22 13:10

nanselm2


you can use regex like this:

=ARRAYFORMULA(SUM(N(REGEXMATCH(B:B, "Mammal|Bird"))))

0

like image 4
player0 Avatar answered Oct 22 '22 13:10

player0