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
Anonymous
Not applicable

Count of distinct values of a column grouped by another column

Hi

 

Is it possible to write a DAX formula to count distinct values of a column grouped by another column please?

 

For example, in the below table, the Count column displays the distinct count of 'Pricing' grouped by D_Reference, L_Number combination. All I'm trying to do is to write a DAX formula to calculate the values in the Count column as displayed below.

 

Any help would be super appreciated. Thanks in advance.

 

D_ReferenceL_NumberPricingCount
D11A2
D12B2
D13A2
D21A1
D22A1
D23A1
D31A3
D32B3
D34C3
1 ACCEPTED SOLUTION

Give below a try
 
Measure = countrows(filter(all('Table'),'Table'[D_Reference] = max('Table'[D_Reference]) && 'Table'[L_Number] = max('Table'[L_Number])))

Thanks
Ankit Jain

Do Mark it as solution if the response resolved your problem. Do like the response if it seems good and helpful.

View solution in original post

7 REPLIES 7
v-shex-msft
Community Support
Community Support

HI @Anonymous ,

You can write a measure formula to calculate distinct count of current group:

Measure =
CALCULATE (
    COUNTROWS ( VALUE ( Table[Pricing] ) ),
    VALUES ( Table[D_Reference] ),
VALUES ( Table[L_Number]) )

BTW, you expected result seems only filter by 'D_Reference' field, 'L_Number' not use in calculation.

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Apologies @v-shex-msft . Here's the list of revised count values that I'm trying to calculate (As you can see the count values now pertain to distinct count of Pricing values grouped by D_Reference and L_Number)

 

Table.JPG

 

I tried to use the formula that you provided however, I see a syntax error message as below..

 

PBI.JPG

 

Do you have any advice?

Give below a try
 
Measure = countrows(filter(all('Table'),'Table'[D_Reference] = max('Table'[D_Reference]) && 'Table'[L_Number] = max('Table'[L_Number])))

Thanks
Ankit Jain

Do Mark it as solution if the response resolved your problem. Do like the response if it seems good and helpful.

Anonymous
Not applicable

How do i write this query if want to group only by one column

Anonymous
Not applicable

Thanks @AnkitBI . This seems to work as I expect. Although, I don't quite understand the formula. For instance, would you mind explaining the point of having the max functions in the formula?

 

'Table'[D_Reference] = max('Table'[D_Reference]) && 'Table'[L_Number] = max('Table'[L_Number])

 

Thanks again. 

Max is to get the current values of fields from the visual.
AnkitBI
Solution Sage
Solution Sage

How your count is grouped by line number. It seems only grouped by d_reference

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.