CountIfs with multiply AND and OR criteria

SanFelippo

Board Regular
Joined
Apr 4, 2017
Messages
124
Hello,

I have some data, and I need to get a count of this data that meets the criteria below:

Column B:B is equal to 1 or 6
AND
Column N is equal to "Intended Population"
AND
Column G is equal to 1
AND
Column H is equal to "Correspondent" or "Retail" or "Wholesale"
AND
Column D is equal to "WI"

Yes I understand this could all be accomplished with a pivot table very easily, and I had done that in the first place, but the people this is for are stubborn and stupid and want it formula based.....
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try this:

=SUM(COUNTIFS(B:B,{1,6},N:N,"Intended Population",G:G,1,H:H,{"Correspondent";"Retail";"Wholesale"},D:D,"WI"))
 
Upvote 0
I think I can help with this but I just want to clarify a couple of things

Can you confirm what you are looking at with B:B

Also, within column N, does it state the words Intended Population or does it match a number?
 
Upvote 0
Try this:

=SUM(COUNTIFS(B:B,{1,6},N:N,"Intended Population",G:G,1,H:H,{"Correspondent";"Retail";"Wholesale"},D:D,"WI"))


This works perfectly. I thought there wasn't a way to incorporate the equivalent of an "OR" function within there. You learn something everyday.

Thanks!
 
Upvote 0
Excel has lots of tricks, and nobody knows them all! Just as an FYI, you can only incorporate 2 ORs within a COUNTIFS like this. Notice that the first array constant {1,6} uses a comma, and the second one uses a semicolon.

Glad we could help! :)
 
Upvote 0
Excel has lots of tricks, and nobody knows them all! Just as an FYI, you can only incorporate 2 ORs within a COUNTIFS like this. Notice that the first array constant {1,6} uses a comma, and the second one uses a semicolon.

Glad we could help! :)

Would you be so kind as to elaborate on this? When you say a "COUNTIFS like this" do you mean just this one? Or you can never do more than 2? What's the significance of using a comma vs a semicolon?
 
Upvote 0
Hmm. This gets complicated quickly . . . you sure you just don't want to take my word for it? :diablo:

OK, I assume you know how COUNTIFS works, for the basic case.

=COUNTIFS(B:B,1)

This looks down column B and counts all the rows that have 1 in that row. Basic. Now if you want to count all the rows with 1 or 6, you could do this:

=COUNTIFS(B:B,1)+COUNTIFS(B:B,6)

Again, still pretty basic. Now to get a little fancier, we could rewrite that as:

=SUM(COUNTIFS(B:B,{1,6}))

This is now an array formula. Normally when you enter an array formula, you have to enter it using Control+Shift+Enter, which tells Excel that somewhere in the formula, where there's normally a single value, there is now a range of values that you want Excel to calculate all at once. However, if you hard-code the array, using the {}, that also tells Excel it's an array formula. Let's say that there are 4 cells with a 1, and 7 cells with a 6. Excel calculates the COUNTIFS twice, once with the 1, and once with the 6, and returns an array with the answers, like this {4,7}. Then the SUM adds those up giving 11. If you want to be able to change the {1,6} to {2,8}, you could put the numbers you want in Q1:R1, and write the formula like this:

=SUM(COUNTIFS(B:B,Q1:R1))

but now you have to use Control+Shift+Enter because you're not using the {}.

Now let's get a little fancier. Let's say we have this:

=SUM(COUNTIFS(B:B,{1,6},H:H,{"Correspondent","Retail"}))

Notice that I'm using commas in both array constants. I'm also ignoring the other columns for now. What do you think this works out as? When Excel sees 2 horizontal arrays (a comma separates items on the same row, a semicolon means go the the first item on the next row - so if you use commas, it's a horizontal array), it processes them both concurrently. Take 1 element from array1, and 1 element from array2, then the 2nd element from array1, and the second element from array2, giving us:

=SUM(COUNTIFS(B:B,1,H:H,"Correspondent"))+SUM(COUNTIFS(B:B,6,H:H,"Retail"))

which is not really what you want. It leaves out 2 combinations: 1,Retail and 6,Correspondent. (Also note, if you tried: =SUM(COUNTIFS(B:B,{1,6},H:H,{"Correspondent","Retail","Wholesale"})) you'd get an error, since the arrays are not the same size.)

So then how does the original formula work with 2 arrays? One is horizontal {1,6}, and the other is vertical {"Correspondent";"Retail";"Wholesale"}. When Excel sees that, it knows that it can't pairwise take elements from the arrays. It creates what's called a Cartesian Product, basically combining every element from the first array with every element from the second array. If you had those arrays in Q1:R1 and U1:U3, it would look something like:

QRSTUV
Correspondent1/Correspondent6/Correspondent
Retail1/Retail6/Retail
Wholesale1/Wholesale6/Wholesale

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]

[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: right"][/TD]

[TD="align: center"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: right"][/TD]

</tbody>
Sheet3



So internally, Excel creates a 2X3 array, performs a COUNTIFS for each variation in it, then SUMs it.

And after all that, here's the simple answer to your question, no, NO version of SUM(COUNTIFS can have more than 2 arrays in it, because that would entail creating a 3-D array, and Excel array functions only handle 1-d or 2-d arrays.

When you have other columns with just 1 possibility, that's fine, since the size of the array is still 2X3, each element in the array just has more parts to it.

Sometimes you can get 3-D equivalency with some complicated constructions, but those would be very dependent on the situation.

Hope this clears things up a bit more, and I didn't just muddy the waters more.
 
Upvote 0
Did I really not respond to this? My fault. This is probably one of the best explanations of a topic like this that I have ever read. I was a double major in mathematics and statistics, so I am no stranger to Cartesian products, linear algebra, matrices, arrays, etc. I just didn’t know that excel was utilizing those things behind the scenes and behind those formulas. Again,fantastic explanation, can’t thank you enough.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top