=ARRAYFORMULA(SUM((A2:A12="a")*(B2:D12>0))
Sometimes it's easier to just work with Booleans like they're arrays of 0s and 1s.
Answer from MattKing on Stack OverflowIs there a way to use a countif function, or a function like it, to check for a certain criteria over multiple ranges? All I can find is using it for one criteria over one range, or multiple criteria over multiple ranges.
google sheets - How can I refer to two different columns in a COUNTIF function? - Web Applications Stack Exchange
Countif function for multiple ranges that are in different rows. - Google Docs Editors Community
Google Sheet formula, need answer fast: countif (or countifs) on a sheet, multiple ranges but same criteria - Factual Questions - Straight Dope Message Board
COUNTIF with multiple ranges in the same column - Google Docs Editors Community
=ARRAYFORMULA(SUM((A2:A12="a")*(B2:D12>0))
Sometimes it's easier to just work with Booleans like they're arrays of 0s and 1s.
COUNTIFS does not work in your first example, because you have two arrays of different sizes. A2:A12 has 1 column while B2:D12 has 3 columns. You can sum them as suggested by David Dykstra's answer, but that becomes hard to update manually if instead of 3 columns you have 300.
You can use SUMPRODUCT:
=SUMPRODUCT(--(A2:A12="a")*--(B2:D12>0))
As specified in the comments, this returns the result 8 for the example.
Use a combination of the ARRAYFORMULA, SUM and arithmetic operations
Use the approach provided in barry houdini's answer. To count all of the clicks by a 26 year old:
=ARRAYFORMULA(sum((A:A=26) * (C:C="true")))
There are 3 parts to this operation.
- The ARRAYFORMULA takes care of looping over the specified range
- The SUM manages counting all of the true results
Essentially, true is being converted to 1 and false is being converted to 0. Boolean logic is done by using arithmetic operations.
An AND operation uses multiplication:
- (1 * 1) = 1 - (True && True) = True
- (1 * 0) = 0 - (True && False) = False
- (0 * 0) = 0 - (False && False) = False
An OR operation uses a combination of the *SIGN function and addition:
- sign(1 + 1) = 1 - (True || True) = True
- sign(1 + 0) = 1 - (True || False) = True
- sign(0 + 0) = 0 - (False || False) = False
Note: The sign function is necessary because of the way boolean addition works differently than arithmetic addition. Basically in boolean addition 1 + 1 = 1, in arithmetic addition 1 + 1 = 2. Obviously, arithmetic addition will mess up the count so you need to run the results of the addition operations through a sign function. The sign function returns 1 if the value is positive, 0 if the value is 0, and -1 if the value is negative.
Lets say you wanted to count the clicks for all users between age 20-25:
=ARRAYFORMULA(sum(sign((A:A=20) + (A:A=21) + (A:A=22) + (A:A=23) + (A:A=24) + (A:A=25)) * (C:C="true")))
Google Sheets now includes the COUNTIFS function which can directly handle the required job.
=COUNTIFS(A:A, E2, C:C, true)
Syntax
=COUNTIFS(
criteria_range1, criterion1,
[criteria_range2, criterion2,
...])
You can include as many criteria_range, criterion pairs as you need.
