Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
RK9009
Frequent Visitor

Distinct Count [NOT IN]

Hi Community I need help with dax 

 

I have a table with IDs and Category:

 

IDCategory
1A
2B
3C
4D
5A
5B
5C
6A
6C
7B
7C
8A
8B
8D
8E
9A

 

  • Distinct count of the IDs is 9 
  • Distinct count of IDs in Cat. A is 5

Now, when we count the distinct IDs in Category B, we need to exclude the IDs which were counted in A (i.e Distinct count of IDs in Catergory B should have a result of 3 and not 4 because the ID 8 is already counted in Cat. A once)

 

Please HELPP. 

 

 

 

7 REPLIES 7
amitchandak
Super User
Super User

@RK9009 , Using distinct at the final stage should work better, if the solution works

 

countrows(distinct(except(selectcolumns(filter(Table,Table[Category] ="B"), "ID", Table[ID]),selectcolumns(filter(Table,Table[Category] ="A"), "ID", Table[ID]))))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

@amitchandak ,tried this one seem like its working in terms of count but the ID counted in A are still showing up in B. 

Thank you for your response 

 

@RK9009 - I mocked this up in PBIX, Page 11. I missed a couple ALL statements.

 

Count in B Measure = 
  VAR __As = DISTINCT(SELECTCOLUMNS(FILTER(ALL('Table (13)'),[Category]="A"),"ID",[ID]))
  VAR __Bs = DISTINCT(SELECTCOLUMNS(FILTER(ALL('Table (13)'),[Category]="B"),"ID",[ID]))
RETURN
  IF(MAX([Category])="B",COUNTROWS(DISTINCT(EXCEPT(__Bs,__As))),BLANK())

 

Fixes the showing up in A problem. PBIX below sig. The answer is actually 2, not 3 because 5 and 8 are both in A according to your test data.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
sqlguru448
Helper III
Helper III

count in B Measure =
VAR __As = DISTINCT(SELECTCOLUMNS(FILTER('Table',[category]="A"),"ID",[ID]))
VAR __Bs = DISTINCT(SELECTCOLUMNS(FILTER('Table',[category]="B"),"ID",[ID]))
RETURN
COUNTROWS(DISTINCT(EXCEPT(__Bs,__As)))
 
Thanks

@sqlguru448 ,  no luck buddy

 

Greg_Deckler
Super User
Super User

@RK9009 Try:

Count in B Measure =
  VAR __As = DISTINCT(SELECTCOLUMNS(FILTER('Table',[Category]="A"),"ID",[ID]))
  VAR __Bs = DISTINCT(SELECTCOLUMNS(FILTER('Table',[Category]="A"),"ID",[ID]))
RETURN
  COUNTROWS(DISTINCT(EXCEPT(__Bs,As)))


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler , Thank you for the response, I tried this measure but no luck 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.