=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 Overflow=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.
COUNTIFS On Separate Ranges - Google Docs Editors Community
I want to use countif for multiple columns at once
How to set up COUNTIFS with multiple criteria. - Google Docs Editors Community
Using COUNTIF with multiple criteria.
How can I count unique values with multiple criteria using COUNTIF in Google Sheets?
What is the syntax of the COUNTIF function in Google Sheets?
How can I count cells with specific text using COUNTIF in Google Sheets?
Hey, I got 3 columns, all with a number (0-15), I want to count how many rows would have an exact combination of numbers. Like how many times does a row have "8" in A:A and "2" in B:B and "12" in C:C
Hello, I am attempting to count the number of "Passes" but only depending on what they passed. For example, I need to count "Pass" but only if it was a pass for "Test A". So one column lists what they were working on and the other lists their results. The general idea would be along the lines of "If X1:X100 = "Test A", then countif Y1:Y100 "Pass" for each cell that is = "Test A". I hope I articulated this effectively. Thanks.
Is 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.