MrKowz
Well-known Member
- Joined
- Jun 30, 2008
- Messages
- 6,653
- Office Version
- 365
- 2016
- Platform
- Windows
I've noticed a lot of users ask the same questions on the boards, and the most searched-for item is by far "countif multiple criteria". So that gave me the thought, what about having a post that users can refer to for questions asked numerous times a day. One that thoroughly, and clearly, explains the formula(s), how they are used, and how they can be tailored to fit the user's data.
Here is my draft write-up of something we might be able to use to alleviate the number of redundant posts regarding counting multiple criteria. Please read it over, and provide feedback. I really want to make a difference on these forums beyond the standard helping, and would be happy to create other write-ups for other questions:
------------------------------------
How to perform a multiple criteria count:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
One of the most asked questions on these forums is how to perform a multiple criteria count. This is a simple solution in Excel 2007 or newer versions, however it poses a difficulty in Excel 2003 and older versions.
<o></o>
Let’s use the following as an example:<o></o>
Data:<o></o>
<o></o>
Problem: We want to count the number of Deposits in the East branch.
<o></o>
In Excel 2007 or newer versions, this is quick and easy since Microsoft added in a new function called COUNTIFS. COUNTIFS has a very simple syntax (and is nearly identical to COUNTIF):
=COUNTIFS(range_1, condition_1, range_2, condition_2, …)<o></o>
So using the COUNTIFS function to solve our problem:
=COUNTIFS(C2:C9,”=Deposit”,D2:D9,”=East”)<o></o>
Or alternatively, since COUNTIFS can support entire column references:
=COUNTIFS(C:C,”=Deposit”,D:D,”=East”)<o></o>
However, in Excel 2003 or older versions, counting multiple criteria becomes a bit of an issue. Without the native support of COUNTIFS, we must use another function in a fashion it was not originally intended. SUMPRODUCT is the function we need to use for this.<o></o>
The SUMPRODUCT function takes two or more arrays, multiplies individual entries together, and sums the results (it sums the products of the arrays, hence the name). But before we can use this, we must first identify our conditions. In our problem, we want to count the number of Deposits in the East branch. If we look at this in a logic sense, we are asking:<o></o>
C2:C9=”Deposit”
D2:D9=”East”<o></o>
If we were to look at the data based on these conditions, it would have TRUE/FALSE for where the criteria is/isn’t met (this is purely for illustrative purposes to show what goes on behind-the-scenes):<o></o>
<o></o>
Looking at this data, wherever there is a TRUE and TRUE statement, that is where both conditions are met, and should be counted. From here, there are two ways SUMPRODUCT can handle the data.<o></o>
Both formulas will return the desired result (2), but they evaluate in very different ways.<o></o>
Like COUNTIFS, SUMPRODUCT can be expanded to hold many conditions, just keep adding more conditions, following the example:<o></o>
=SUMPRODUCT((Conditional1)*(Conditional2)*(Conditional3)*etc)<o></o>
=SUMRPODUCT(--(Conditional1),--(Conditional2),--(Conditional3),etc)<o></o><o></o>
One thing to note is that unlike COUNTIFS, the SUMPRODUCT function cannot use entire column references. This means that your ranges in the conditions must be defined ranges, one-dimensional, and identical in size. If these are not followed, the formula will produce a #NUM! or #VALUE! Error.
For more examples on how SUMPRODUCT is used and the various ways it can be used to count data, please read xlDynamic's thorough writeup here.
Here is my draft write-up of something we might be able to use to alleviate the number of redundant posts regarding counting multiple criteria. Please read it over, and provide feedback. I really want to make a difference on these forums beyond the standard helping, and would be happy to create other write-ups for other questions:
------------------------------------
How to perform a multiple criteria count:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
One of the most asked questions on these forums is how to perform a multiple criteria count. This is a simple solution in Excel 2007 or newer versions, however it poses a difficulty in Excel 2003 and older versions.
<o></o>
Let’s use the following as an example:<o></o>
Data:<o></o>
Excel Workbook | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | Date | Amount | Type | Branch | ||
2 | 12/1/2010 | 4000 | Deposit | East | ||
3 | 12/1/2010 | 6000 | Deposit | East | ||
4 | 12/1/2010 | 2000 | Withdrawl | East | ||
5 | 12/1/2010 | 3000 | Deposit | West | ||
6 | 12/2/2010 | 5000 | Withdrawl | East | ||
7 | 12/2/2010 | 5000 | Withdrawl | West | ||
8 | 12/3/2010 | 10000 | Deposit | West | ||
9 | 12/3/2010 | 4000 | Withdrawl | East | ||
... |
Problem: We want to count the number of Deposits in the East branch.
<o></o>
In Excel 2007 or newer versions, this is quick and easy since Microsoft added in a new function called COUNTIFS. COUNTIFS has a very simple syntax (and is nearly identical to COUNTIF):
=COUNTIFS(range_1, condition_1, range_2, condition_2, …)<o></o>
So using the COUNTIFS function to solve our problem:
=COUNTIFS(C2:C9,”=Deposit”,D2:D9,”=East”)<o></o>
Or alternatively, since COUNTIFS can support entire column references:
=COUNTIFS(C:C,”=Deposit”,D:D,”=East”)<o></o>
However, in Excel 2003 or older versions, counting multiple criteria becomes a bit of an issue. Without the native support of COUNTIFS, we must use another function in a fashion it was not originally intended. SUMPRODUCT is the function we need to use for this.<o></o>
The SUMPRODUCT function takes two or more arrays, multiplies individual entries together, and sums the results (it sums the products of the arrays, hence the name). But before we can use this, we must first identify our conditions. In our problem, we want to count the number of Deposits in the East branch. If we look at this in a logic sense, we are asking:<o></o>
C2:C9=”Deposit”
D2:D9=”East”<o></o>
If we were to look at the data based on these conditions, it would have TRUE/FALSE for where the criteria is/isn’t met (this is purely for illustrative purposes to show what goes on behind-the-scenes):<o></o>
Excel Workbook | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | Date | Amount | Type | Branch | ||
2 | 12/1/2010 | 4000 | TRUE | TRUE | ||
3 | 12/1/2010 | 6000 | TRUE | TRUE | ||
4 | 12/1/2010 | 2000 | FALSE | TRUE | ||
5 | 12/1/2010 | 3000 | TRUE | FALSE | ||
6 | 12/2/2010 | 5000 | FALSE | TRUE | ||
7 | 12/2/2010 | 5000 | FALSE | FALSE | ||
8 | 12/3/2010 | 10000 | TRUE | FALSE | ||
9 | 12/3/2010 | 4000 | FALSE | TRUE | ||
... |
Looking at this data, wherever there is a TRUE and TRUE statement, that is where both conditions are met, and should be counted. From here, there are two ways SUMPRODUCT can handle the data.<o></o>
- =SUMPRODUCT((C2:C9=”Deposit”)*(D2:D9=”East”))
- =SUMPRODUCT(--(C2:C9=”Deposit”),--(D2:D9=”East”))
Both formulas will return the desired result (2), but they evaluate in very different ways.<o></o>
- The first formula, =SUMPRODUCT((C2:C9=”Deposit”)*(D2:D9=”East”)), creates one array within the formula, based on the conditions:
The first condition: (C2:C9=”Deposit”) returns the array {TRUE,TRUE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE}
The second condition: (D2:D9=”East”) returns the array
{TRUE,TRUE,TRUE,FALSE,TRUE,FALSE,FALSE,TRUE}
When multiplied together (as the formula shows), a new array is returned:
{TRUE*TRUE, TRUE*TRUE, FALSE*TRUE, TRUE*FALSE, FALSE*TRUE, FALSE*FALSE, TRUE*FALSE, FALSE*TRUE}
When we multiply TRUE and FALSE statements together, Excel uses their values of 1 and 0 to evaluate and return a number. So
TRUE*FALSE = 1*0 = 0
FALSE*FALSE = 0*0 = 0
FALSE*TRUE = 0*1 = 0
TRUE*TRUE = 1*1 = 1
After performing the mathematical operation, we have an array of 1 and 0s:
{1,1,0,0,0,0,0,0}
Now, SUMPRODUCT can add up the values! 1+1+0+0+0+0+0+0 = 2
- The second formula, =SUMPRODUCT(--(C2:C9=”Deposit”),--(D2:D9=”East”)), evaluates in a similar fashion, except it uses multiple arrays. It holds some advantages over the example not using the --, if you want to read more in depth on this, please read xlDynamic’s full write-up on SUMPRODUCT which is linked at the bottom of this post.
First, the -- (double unary) must be explained. The -- coerces the conditional statement within the parentheses into a 1 or a 0. By doing this, it creates an array of 1s and 0s. It does this by taking each TRUE/FALSE statement and multiplying it by -1 twice. Since TRUE/FALSE holds values of 1/0 (as shown in explanation 1), we multiply them by -1 twice to convert them to a positive, numerical, value:
--(TRUE) = (-1)(-1)(TRUE) = (-1)(-1) = 1
--(FALSE) = (-1)(-1)(FALSE) = (-1)(0) = 0
So now, SUMPRODUCT has two arrays to look at:
{1,1,0,1,0,0,1,0} and {1,1,0,0,0,0,0,0}
Following the nature of the SUMPRODUCT formula, it now multiplies these two arrays together and adds those products:
1*1 + 1*1 + 0*0 + 1*0 + 0*0 + 0*0 + 1*0 + 0*0 = 1+1+0+0+0+0+0+0 = 2
Like COUNTIFS, SUMPRODUCT can be expanded to hold many conditions, just keep adding more conditions, following the example:<o></o>
=SUMPRODUCT((Conditional1)*(Conditional2)*(Conditional3)*etc)<o></o>
=SUMRPODUCT(--(Conditional1),--(Conditional2),--(Conditional3),etc)<o></o><o></o>
One thing to note is that unlike COUNTIFS, the SUMPRODUCT function cannot use entire column references. This means that your ranges in the conditions must be defined ranges, one-dimensional, and identical in size. If these are not followed, the formula will produce a #NUM! or #VALUE! Error.
For more examples on how SUMPRODUCT is used and the various ways it can be used to count data, please read xlDynamic's thorough writeup here.