Forum Discussion
PowerBI4Life
3 years agoFrequent Visitor
How To Count Distinct States Based Distinct PersonsNames
Hi! I have two columns in a table. State and PersonsName. I want to calculate the count of distinct states that contain more than 10 distinct PersonsName. Almost like how a GROUP BY in...
AilleryO
3 years agoMemorable Member
Hi,
I made a PBI with diffrent options for your questions.
I made a table with 2 columns (States and PersonName) and gave you 5 options :
1/ In Power Query by making a Group By. Good option if you don't need you data in details. See my first query for that option.
2/ Using DAX, you can make a basic calculation but your totals will be wrong, because of the lack of filter context on totals.
NbPersonDAX_Mesure = DISTINCTCOUNT(TableStatesPersonDAX[PersonName])
Works for every lines of your table except for the total.
3/ Using DAX with variable, works on the rows of the table but gives not total, since not current state available :
NbPersonDAX_Mesure v2 =
VAR CurrState=SELECTEDVALUE(TableStatesPersonDAX[States])
RETURN
CALCULATE(
DISTINCTCOUNT(TableStatesPersonDAX[PersonName]),
TableStatesPersonDAX[States]=CurrState)
A little better since you do not have a wrong total, but maybe you need the total ?
4/ The solution I would recommend would be :
NbPersonDAX_Mesure v3 = SUMX( VALUES(TableStatesPersonDAX[States]) ,
CALCULATE( DISTINCTCOUNT( TableStatesPersonDAX[PersonName] ) ) )
Do not forget the CALCULATE before DISTINCTCOUNT because it provides the context transition needed in this case. Making the right count on the rows and the good total at the bottom of your table.
Or even better, test the formula with and without the CALCULATE to see by yourself the context transition.
5/ Create a summarized table in DAX with a filter :
TableDAX_withFILTER = FILTER( SUMMARIZECOLUMNS(TableStatesPersonDAX[States], "NbPersonStates" ,
DISTINCTCOUNT(TableStatesPersonDAX[PersonName])) ,[NbPersonStates]>2 )
If you need to display or make many calculations on those synthesis, could be an option as well.
Hope this will help you and others,
Please find attached demo pbix