Forum Discussion
Formula Help: Count Unique Domains per Account ID
Hello Community!
I have an Excel file with two columns: Account ID and Domain Name. A single Account ID can have multiple Domain Names associated with it. In other words, the relationship between Account ID and Domain Name is one-to-many (1:N). What's the best way to pull the number of UNIQUE domains associated with a specific account ID? Here is what the sample data might look like:
| Account ID | Domain Name |
| 1234 | f.com |
| 1234 | e.com |
| 1234 | g.com |
| 4321 | a.com |
| 4321 | b.com |
| 4321 | b.com |
| 5678 | z.com |
| 5678 | y.com |
For Account ID 1234, there are 3 unique domains. Similarly, there are 2 unique domains for account IDs 4321 and 5678. Can anyone please assist.
The formula in the sample file returns the intended result. The formula must be entered as an arrayformula with ctrl+shift+enter if someone doesn't work with Office 365 or Excel for the web or Excel 2021.
10 Replies
- rachelSteel Contributor
Came across this formula recently, quickly became one of my favourites:
=SUMPRODUCT(--($A$2:$A$9=F2),1/COUNTIFS($A$2:$A$9,$A$2:$A$9,$B$2:$B$9,$B$2:$B$9))
(can be drag down)
- SergeiBaklanDiamond Contributor
If for legacy Excel we may add one more
For Account ID
=IFERROR( INDEX( accountID, AGGREGATE(15,6, (ROW(accountID)-ROW($A$1)) / (COUNTIF($E$14:E14, accountID)=0), 1) ), "" )for # of Domains
=IF( $E15="", "", SUMPRODUCT(--(accountID=$E15)/COUNTIFS(DomainName,DomainName) ) )and drag both down
- rachelSteel ContributorBrilliant! That is exactly why excel is fun!
- yashank_pCopper ContributorThanks, Rachel!
- rachelSteel ContributorGlad you like it.
- SergeiBaklanDiamond Contributor
If on Excel 365
=LET( u, UNIQUE(accountID), count, MAP(u, LAMBDA(a, ROWS(UNIQUE(FILTER(DomainName, accountID=a))) ) ), VSTACK( {"Account ID","# of Domains"}, HSTACK(u, count)) )- yashank_pCopper ContributorThanks, Sergei!
- SergeiBaklanDiamond Contributor
yashank_p , you are welcome
- OliverScheurichGold Contributor
The formula in the sample file returns the intended result. The formula must be entered as an arrayformula with ctrl+shift+enter if someone doesn't work with Office 365 or Excel for the web or Excel 2021.
- yashank_pCopper Contributor
Thanks so much for all the help, OliverScheurich! Keep inspiring