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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
TL5866
Frequent Visitor

Inconsistent results in DAX: SUMX() on DISTINCTCOUNT() w/o SortOrder

Hi,

 

I'm using the follwing formula:

Measure = SUMX(VALUES('Table'[Type]), CALCULATE(DISTINCTCOUNT('Table'[Color])))

on the following table:

 

CategoryColorTypeSortOrderTypeSize
cat1red1A22
cat1red1A24
cat1blue1A22
cat1yellow2B24
cat1red2B24
cat1blue1A26
cat1blue2B28
cat1yellow2B22
cat1red1A24
cat1blue1A24
cat2yellow1A22
cat2black1A24
cat2blue1A22
cat2red2B24
cat2blue2B22
cat2yellow3C24

 

In modeling, Type is set as sorted by TypeSortOrder. I set a visual (or page or report) filter on (Category = cat1) and (Size=22 or Size=24).

 

Then I'm showing the measure in a matrix visual - all fine

2018-06-01 14_05_13-SUMX on DistinctCount - Power BI Desktop.png

If I remove the sorting on Type and turn it back to default, here is what I get - values for Color and Type are incorrect.

Can you help understand why ? Is it the expected behavior ?

2018-06-01 13_53_28-SUMX on DistinctCount - Power BI Desktop.png

Here is the link to the PBIX:

https://drive.google.com/file/d/1N26gmBa3Apo-W7oDlpYBLXERhhBpoq8V/view?usp=sharing

 

Thanks

 

Thierry

1 ACCEPTED SOLUTION

Hi @TL5866,

Try using the below measure

 

I am using an additional column called Concatenated whose expression is 

 

 

Concatenated = CONCATENATE(Table1[Type],Table1[Color])

 

and the Measure is

 

 

Measure  = IF(HASONEVALUE(Table1[Color]),DISTINCTCOUNT(Table1[Color]),IF(HASONEVALUE(Table1[Type]),DISTINCTCOUNT(Table1[Color]),IF(HASONEVALUE(Table1[Category]),DISTINCTCOUNT(Table1[Concatenated]))))

 

My Output is

Output-DistinctCOUNT.PNG

 

 

I think this is what your expected output is!!!

 

Regards,

Thejeswar

 

 

 

 

View solution in original post

4 REPLIES 4
TL5866
Frequent Visitor

Hi,

 

Thanks for your replies. Your results are correct, but this does not match the case that I'm trying to solve here. 

 

I am using SUMX() over Type because I want the Category to be the sum of distinct Colors within one Type at the time. So using my formula with SUMX() gives the following result:

  • Type A contains (blue, red) -> Sum for Type A = 2
  • Type B contains (red, yellow) -> Sum for Type B = 2
  • Cat1 contains (Type A, Type B) -> Sum for Cat1 = 2+2 = 4

Using CALCULATE() with VALUES(), or simply DISTINCTCOUNT() simply does not give the correct result. In that case, the total for Cat is 3 (blue, red, yellow), because, one of the colors is the same across Type. So it does not solve the issue actually...

Hi @TL5866,

Try using the below measure

 

I am using an additional column called Concatenated whose expression is 

 

 

Concatenated = CONCATENATE(Table1[Type],Table1[Color])

 

and the Measure is

 

 

Measure  = IF(HASONEVALUE(Table1[Color]),DISTINCTCOUNT(Table1[Color]),IF(HASONEVALUE(Table1[Type]),DISTINCTCOUNT(Table1[Color]),IF(HASONEVALUE(Table1[Category]),DISTINCTCOUNT(Table1[Concatenated]))))

 

My Output is

Output-DistinctCOUNT.PNG

 

 

I think this is what your expected output is!!!

 

Regards,

Thejeswar

 

 

 

 

Thejeswar
Resident Rockstar
Resident Rockstar

Hi @TL5866,

The Values won't generally get affected by the Columns that we use for sorting

 

I tried your case with your data assuming that you are trying to find the Distinct Count of Value Column (in this case color) for your attributes (category, Type and Color) and the output screenshot is as below

Output-DistinctCOUNT.PNG

 

 

I find the values to be appropriate (as expected).

 

The Measure formula that is used is 

 

Measure = DISTINCTCOUNT(Table1[Color]) 

Regards,

Thejeswar

v-qiuyu-msft
Community Support
Community Support

Hi @TL5866,

 

Do you want to calculate distinct count rows within category, Size and Color group, right? 

 

If it does, you need to modify the measure as below: 

Measure = CALCULATE(DISTINCTCOUNT('Table'[Color]),VALUES('Table'[Category]))

 

The value will not change whether we set Type column sort by default or TypeSoryOrder column. 

 

For detail information, see this new report: https://www.dropbox.com/s/4fwdr404zbxjnre/SUMX%20on%20DistinctCount.pbix?dl=0

 

Best Regards,

Qiuyun Yu 

Community Support Team _ Qiuyun Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Power BI Header Change

Power BI Header Update

Read about the exciting updates for the Power BI forum.

Top Solution Authors