Count distinct with conditions

Uganda

Board Regular
Joined
Jun 10, 2015
Messages
76
Office Version
  1. 365
Hello, I would like to write a calculation using data in a table to know the number of distinct IDs which are there for each sector and company. My data is much larger than this example and I will have duplicate records (there are other columns which have different data in them).

IdSectorCompany
123​
Sector ACompany 1
123​
Sector BCompany 1
234​
Sector ACompany 2
234​
Sector CCompany 2
234​
Sector ACompany 1
123​
Sector BCompany 1
567​
Sector CCompany 2

The result would be:
SectorCompanyNumber
Sector ACompany 1
2​
Sector BCompany 1
1​
Sector ACompany 2
1​
Sector CCompany 2
2​

Sector B Company 1 appears twice but for the same ID so that is considered as 1 whereas Sector C Company 2 appears twice with different IDs so this counts as 2.
In SQL, I would do this as

select sector, company, count(distinct ID) from table
group by sector, company

If that helps - this is what I really want to do. I have searched and tried options using SUMPRODUCT and COUNTIFS but I couldn't seem to make it work.

Thanks in advance
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Here is one way assuming ms365:

Book1
ABCDEFG
1IdSectorCompany
2123Sector ACompany 1Sector ACompany 12
3123Sector BCompany 1Sector BCompany 11
4234Sector ACompany 2Sector ACompany 21
5234Sector CCompany 2Sector CCompany 22
6234Sector ACompany 1
7123Sector BCompany 1
8567Sector CCompany 2
Sheet12
Cell Formulas
RangeFormula
E2:G5E2=LET(x,UNIQUE(B2:C8,0),HSTACK(x,BYROW(x,LAMBDA(z,COUNTA(UNIQUE(FILTER(A2:A8,MMULT(--(B2:C8=z),{1;1})=2)))))))
Dynamic array formulas.
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Here is one way assuming ms365:

Book1
ABCDEFG
1IdSectorCompany
2123Sector ACompany 1Sector ACompany 12
3123Sector BCompany 1Sector BCompany 11
4234Sector ACompany 2Sector ACompany 21
5234Sector CCompany 2Sector CCompany 22
6234Sector ACompany 1
7123Sector BCompany 1
8567Sector CCompany 2
Sheet12
Cell Formulas
RangeFormula
E2:G5E2=LET(x,UNIQUE(B2:C8,0),HSTACK(x,BYROW(x,LAMBDA(z,COUNTA(UNIQUE(FILTER(A2:A8,MMULT(--(B2:C8=z),{1;1})=2)))))))
Dynamic array formulas.
Hello, thanks for your help - I did think it would be more straight forward. If I wanted to add a condition into this - imagine I have a flag in column D which is 1 or 0 and I want to them only count the rows with 1 in them. How could I adapt the array please?
 
Upvote 0
Another option
Fluff.xlsm
ABCDEFG
1IdSectorCompany
2123Sector ACompany 1Sector ACompany 12
3123Sector BCompany 1Sector BCompany 11
4234Sector ACompany 2Sector ACompany 21
5234Sector CCompany 2Sector CCompany 22
6234Sector ACompany 1
7123Sector BCompany 1
8567Sector CCompany 2
9
Lists
Cell Formulas
RangeFormula
G2:G5G2=ROWS(UNIQUE(FILTER($A$2:$A$8,($B$2:$B$8=E2)*($C$2:$C$8=F2))))
 
Upvote 0
Another option
Fluff.xlsm
ABCDEFG
1IdSectorCompany
2123Sector ACompany 1Sector ACompany 12
3123Sector BCompany 1Sector BCompany 11
4234Sector ACompany 2Sector ACompany 21
5234Sector CCompany 2Sector CCompany 22
6234Sector ACompany 1
7123Sector BCompany 1
8567Sector CCompany 2
9
Lists
Cell Formulas
RangeFormula
G2:G5G2=ROWS(UNIQUE(FILTER($A$2:$A$8,($B$2:$B$8=E2)*($C$2:$C$8=F2))))
Thank you so much for this. This is one of my tasks but I also will need to calculate the distinct IDs for each row that meet another criteria. E.g. if there is a 1 in column D, I will count it but not if there is a 0. How would I update the formula please?

IdSectorCompanyCompleted
123​
Sector ACompany 1
1​
123​
Sector BCompany 1
1​
234​
Sector ACompany 2
0​
234​
Sector CCompany 2
0​
234​
Sector ACompany 1
0​
123​
Sector BCompany 1
1​
567​
Sector CCompany 2
0​
 
Upvote 0
How about
Fluff.xlsm
ABCDEFG
1IdSectorCompanyCompleted
2123Sector ACompany 11Sector ACompany 11
3123Sector BCompany 11Sector BCompany 10
4234Sector ACompany 20Sector ACompany 20
5234Sector CCompany 20Sector CCompany 20
6234Sector ACompany 10
7123Sector BCompany 11
8567Sector CCompany 20
9
Lists
Cell Formulas
RangeFormula
G2:G5G2=IFERROR(ROWS(UNIQUE(FILTER($A$2:$A$8,($B$2:$B$8=E2)*($C$2:$C$8=F2)*(D2:D8=1)))),0)
 
Upvote 0
How about
Fluff.xlsm
ABCDEFG
1IdSectorCompanyCompleted
2123Sector ACompany 11Sector ACompany 11
3123Sector BCompany 11Sector BCompany 10
4234Sector ACompany 20Sector ACompany 20
5234Sector CCompany 20Sector CCompany 20
6234Sector ACompany 10
7123Sector BCompany 11
8567Sector CCompany 20
9
Lists
Cell Formulas
RangeFormula
G2:G5G2=IFERROR(ROWS(UNIQUE(FILTER($A$2:$A$8,($B$2:$B$8=E2)*($C$2:$C$8=F2)*(D2:D8=1)))),0)
This is fantastic, thanks so much!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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