cancel
Showing results for
Did you mean:

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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:

 Category Color TypeSortOrder Type Size cat1 red 1 A 22 cat1 red 1 A 24 cat1 blue 1 A 22 cat1 yellow 2 B 24 cat1 red 2 B 24 cat1 blue 1 A 26 cat1 blue 2 B 28 cat1 yellow 2 B 22 cat1 red 1 A 24 cat1 blue 1 A 24 cat2 yellow 1 A 22 cat2 black 1 A 24 cat2 blue 1 A 22 cat2 red 2 B 24 cat2 blue 2 B 22 cat2 yellow 3 C 24

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

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 ?

Here is the link to the PBIX:

Thanks

Thierry

1 ACCEPTED SOLUTION
Resident Rockstar

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

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

Regards,

Thejeswar

4 REPLIES 4
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...

Resident Rockstar

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

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

Regards,

Thejeswar

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

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

The Measure formula that is used is

`Measure = DISTINCTCOUNT(Table1[Color]) `

Regards,

Thejeswar

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.

Announcements

#### New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

#### Power BI Monthly Update - May 2024

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

#### Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors