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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
_Alex_
New Member

DAX Help: Unique count by row

Suppose I have a dataset as follows:

 

Car_ID    Owner_Name  Number_of_Owners

1             Adam               3

1             Bob                  3

1             Carrie               3

2             Alex                 2

2             Bill                   2

3             Aron                1

 

What DAX expression would produce the column "Number_of_Owners" above?

 

Thank you!

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

As a column:

 

Number of Owners = 
VAR __table = FILTER(ALL('Table',[Car_ID]=EARLIER([Car_ID]))
RETURN COUNTROWS(__table)

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Super User
Super User

As a column:

 

Number of Owners = 
VAR __table = FILTER(ALL('Table',[Car_ID]=EARLIER([Car_ID]))
RETURN COUNTROWS(__table)

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Greg_Deckler,

 

Thanks for your response!

 

A few questions:

 

1. When using the code you provided I'm getting an error which states "Too few arguments were pased to the FILTER function".  As such, I'm assuming that there should be a right-parenthesis after 'Table'.

 

Number of Owners = 
VAR __table = FILTER(ALL('Table',[Car_ID])=EARLIER([Car_ID]))
RETURN COUNTROWS(__table)

 

2.  After adding the right-parenthesis after [Car_ID] I'm now getting an error which states "EARLIER/EARLIEST refers to a nearlier row context which does not exist".

 

Thank you!

 

*Edit:

 

Nevermind; it looks like the EARLIER statement doesn't produce this error when I create a column instead of a measure.

Awesome. If you want it as a measure, that would be:

 

Measure 4 = 
VAR __carid = MAX([Car_ID])
VAR __table = FILTER(ALL(Table5),[Car_ID]=__carid)
RETURN COUNTROWS(__table)

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Could you please try this..

 

TotalOwners = CALCULATE(COUNT(CarOwners[CarID]),FILTER(ALL(CarOwners),CarOwners[CarID] = EARLIER([CarID])))

image.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.