Countif with multiple criteria in the same range

Sinon

Active Member
Joined
Aug 6, 2015
Messages
298
Hello. I have a question for which I' haven't yet found an answer. An example of my data:

*ABC
1CompanyPolicy TypeStart date
2AHome insurance02/10/2014
3ACar insurance02/10/2014
4ATax insurance01/08/2015
5BCar insurance02/10/2014
6BTax insurance01/08/2015
7CCar insurance03/10/2014
8CTax insurance02/08/2015
9DCar insurance03/10/2014
10DCar insurance03/10/2014
11ECar insurance07/09/2015
12FHome insurance01/08/2015
13FCar insurance01/08/2015

<tbody>
</tbody>

Excel tables to the web >> Excel Jeanie HTML 4

Basically, I work with data concerning insurance policies and I want to know how I can count how many companies have only had insurance in 2015 and not 2014. Looking at the above table, that should be companies E and F only so I would like the result to be 2. (my actual data is far more extensive of course).
Obviously, a company can have multiple policies (i.e. car insurance, home insurance, tax insurance etc) incepting in the same year so they might appear multiple times.

I have actually managed to count these by copying the companies in a different sheet, removing duplicates, doing 2 separate countifs for every company, one looking for policies incepting in 2014 and another looking for policies incepting in 2015. This way, I got what I needed.

But I need this to be workable for a complete excel beginner. Ideally, they would put their data in the sheet and there would be some formula in another sheet which would pull through the correct number. I could get away with a pivot/powerpivot as I can instruct the end user to just refresh the table after adding data. Problem is, I have no idea how to do a calculated field for such a count.

I am an excel intermediate so I could probably be able to implement something more complex and just show the end user the more simplistic aspects. I am ok with helper columns as I plan to make everything into a table so any formulas would automatically be applied to new lines.
The solution might be simple and right under my nose but I've been thinking about this for so long that I cannot see the forest for the trees. Any help would be appreciated.

Sinon
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I managed to do this with 2 helper columns. It's not especially neat either. I'll try to think of a better version of this, or maybe someone else will jump in. This formula also assumes that you have headers in row 1 and your data starts in row 2.
In D2 put: =YEAR(C2)
In E2 put: =IF(MATCH(A2,A:A,0)<>ROW(),0,IF(AND(COUNTIFS(A:A,A2,D:D,"=2014")=0,COUNTIFS(A:A,A2,D:D,"=2015")>0),1,0))
Then copy those down the D and E columns. Put a SUM(E:E) at the bottom to get the number of companies you're looking for. You can hide these columns of course.
 
Upvote 0
Is possible to get the result with a very complex array formula, but i think you should use a helper column.

Something like this


A
B
C
D
E
F
1
Company​
Policy Type​
Start date​
Helper​
Count​
2
A​
Home insurance​
02/10/2014​
0​
2​
3
A​
Car insurance​
02/10/2014​
0​
4
A​
Tax insurance​
01/08/2015​
0​
5
B​
Car insurance​
02/10/2014​
0​
6
B​
Tax insurance​
01/08/2015​
0​
7
C​
Car insurance​
03/10/2014​
0​
8
C​
Tax insurance​
02/08/2015​
0​
9
D​
Car insurance​
03/10/2014​
0​
10
D​
Car insurance​
03/10/2014​
0​
11
E​
Car insurance​
07/09/2015​
1​
12
F​
Home insurance​
01/08/2015​
1​
13
F​
Car insurance​
01/08/2015​
0​

<tbody>
</tbody>


Formula in D2 (helper column) copied down
=--AND(MATCH(A2,A:A,0)=ROW(A2),COUNTIFS(A:A,A2,C:C,">="&DATE(2014,1,1),C:C,"<="&DATE(2014,12,31))=0,COUNTIFS(A:A,A2,C:C,">="&DATE(2015,1,1),C:C,"<="&DATE(2015,12,31))>0)

Formula in F2
=SUM(D:D)

Hope this helps

M.
 
Last edited:
Upvote 0
Row\Col
A​
B​
C​
D​
E​
1​
CompanyPolicy TypeStart date
2​
AHome insurance
2/10/2014
2
3​
ACar insurance
2/10/2014
4​
ATax insurance
1/8/2015
5​
BCar insurance
2/10/2014
6​
BTax insurance
1/8/2015
7​
CCar insurance
3/10/2014
8​
CTax insurance
2/8/2015
9​
DCar insurance
3/10/2014
10​
DCar insurance
3/10/2014
11​
ECar insurance
7/9/2015
12​
FHome insurance
1/8/2015
13​
FCar insurance
1/8/2015

E2, control+shift+enter, not just enter:
Rich (BB code):

=SUM(IF(FREQUENCY(IF(IF(ISNA(MATCH(IF(YEAR(C2:C13)=2015,A2:A13),
    IF(YEAR(C2:C13)=2014,A2:A13),0)),A2:A13)<>FALSE,
    MATCH(B2:B13,B2:B13,0)),ROW(B2:B13)-ROW(B2)+1),1))
 
Upvote 0

Forum statistics

Threads
1,215,634
Messages
6,125,938
Members
449,275
Latest member
jacob_mcbride

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