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
Nigelbloomy
Frequent Visitor

Distinct count group by specific columns

I am trying to create a measure (I don't have access to the dataset) to get a distinct count of status, by grouping year and ID.  I also only want to count statuses selected by the user.  I need to know for a given year and ID if there is more than one status, but be able to ignore some of those statuses when a user deselects them from the page filter.

 

I have tried several formulas and they all come close, but don't work all the way.  

CALCULATE(DISTINCTCOUNT(Data[Investment Status]), ALLEXCEPT(Data,Data[Investment ID], Data[Year]))
This one works but doesn't change when a user filters the statuses on the page filter.
 
CALCULATE(DISTINCTCOUNT(Data[Investment Status]), ALLSELECTED(Data[Investment Status]))
This one allows the user to filter the results, but the grouping level is based on whatever is in the table in the viz.  So for example with investment ID 25, there are 2 investment names, one approved and one draft.  This formula misses the fact that for this year and ID there are 2 statuses.
 
COUNTROWS(FILTER(ALL(Data[Year], Data[Investment ID], Data[Investment Status]), ALLSELECTED(Data[Investment Status])))
I also tried this, but don't know how to get it to work at all.  It looks promising, but gives me an error that multiple values were supplied when a single value was expected.

 

In this table the distinct count is what I am expecting when all statuses are selected:

YearSolutionInvestment IDInvestment NameInvestment StatusProject/OpExLabor/NonAmount PlannedDistinct Count
2020S1 solution25Investment 1ApprovedProjectNon102
2020S1 solution25Investment 2DraftProjectNon102
2021S1 solution25Investment 2DraftProjectNon151
2022S1 solution25investment 2DraftProjectNon151
2020S2 solution26Investment 3ApprovedProjectNon101
2020S2 solution27investment 4ApprovedProjectNon52
2020S2 solution27investment 4Ready for division reviewProjectNon82
2020S2 solution28investment 5ApprovedProjectNon101
2021S2 solution26investment 3ApprovedProjectNon122
2021S2 solution26investment 3Ready for ICSC reviewProjectNon152
2021S2 solution27investment 4Ready for division reviewProjectNon101
2021S2 solution28investment 5ApprovedProjectNon101
2020S3 solution30investment 6ApprovedProjectNon101
2021S3 solution30investment 6ApprovedProjectNon101

 

This is what I would want to see if the user has chosen to only select Approved and Draft statuses:

YearSolutionInvestment IDInvestment NameInvestment StatusProject/OpExLabor/NonAmount PlannedDistinct Count
2020S1 solution25Investment 1ApprovedProjectNon102
2020S1 solution25Investment 2DraftProjectNon102
2021S1 solution25Investment 2DraftProjectNon151
2022S1 solution25investment 2DraftProjectNon151
2020S2 solution26Investment 3ApprovedProjectNon101
2020S2 solution27investment 4ApprovedProjectNon51
2020S2 solution27investment 4Ready for division reviewProjectNon8 
2020S2 solution28investment 5ApprovedProjectNon101
2021S2 solution26investment 3ApprovedProjectNon121
2021S2 solution26investment 3Ready for ICSC reviewProjectNon15 
2021S2 solution27investment 4Ready for division reviewProjectNon10 
2021S2 solution28investment 5ApprovedProjectNon101
2020S3 solution30investment 6ApprovedProjectNon101
2021S3 solution30investment 6ApprovedProjectNon101
1 REPLY 1
Nigelbloomy
Frequent Visitor

This seems to work but I haven't tested it much.  It would be annoying to add any extra columns to the all function.  It would be more robust if you could specify the year and ID as the level of grouping.

CALCULATE(DISTINCTCOUNT(Data[Investment Status]), ALLSELECTED(Data[Investment Status]), ALL(Data[Investment Name]))

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.