Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hello,
I am creating a measure (expression at the very bottom of the post) to tell me the percentage of items that completed on time. To that end, I have a calculated column that is basically [# yes] / ([# yes] + [# no]. However when I put this intro a matrix visual to validate the answer, this is what I ended up with:
This confuses me because I thought that the measure would be calculated independently for each row, so I would end up with 100% on the 'Yes' row, and 0% on the 'No' and 'N/A' rows.
I am a relative newbie to Power BI and DAX, so I assume I'm just missing something here. Why does this not behave the way I think it should? What changes would need to be made to make it work that way?
Thanks for your help!
% Programs Completed On Time =
CALCULATE(
DISTINCTCOUNT('Table - PROGRAMS'[ID]),
&& 'Table - PROGRAMS'[PROGRAM_STATUS] = "Completed"
&& 'Table - PROGRAMS'[PROGRAM_COMPLETED_ON_TIME] = "Yes"
)
/
CALCULATE(
DISTINCTCOUNT('Table - PROGRAMS'[ID]),
&& 'Table - PROGRAMS'[PROGRAM_STATUS] = "Completed"
&& 'Table - PROGRAMS'[PROGRAM_COMPLETED_ON_TIME] IN {"Yes", "No"}
)
Solved! Go to Solution.
@pbiuser12345 It's because you are trying to use CALCULATE with a single table. That tends not to work. Ditch CALCULATE can go with COUNTROWS( DISTINCT( SELECTCOLUMNS( FILTER(...), ... ) ) )
@pbiuser12345 It's because you are trying to use CALCULATE with a single table. That tends not to work. Ditch CALCULATE can go with COUNTROWS( DISTINCT( SELECTCOLUMNS( FILTER(...), ... ) ) )
Thanks Greg, that put me on the right track. The below expression is working the way I would have expected.
I'm not sure I understand the 'why' of this though. Are you able to elaborate or point me to something that explains why CALCULATE is bad on a single table? Should I consider it best practice to reserve CALCULATE for measures that span multiple tables, and stick to more basic/fundamental functions for single table applications?
% Programs Completed On Time =
COUNTROWS(
FILTER(
'Table - PROGRAMS',
&& 'Table - PROGRAMS'[PROGRAM_STATUS] = "Completed"
&& 'Table - PROGRAMS'[PROGRAM_COMPLETED_ON_TIME] = "Yes"
)
)
/
COUNTROWS(
FILTER(
'Table - PROGRAMS',
&& 'Table - PROGRAMS'[PROGRAM_STATUS] = "Completed"
&& 'Table - PROGRAMS'[PROGRAM_COMPLETED_ON_TIME] IN {"Yes", "No"}
)
)
Have a read of this, which I think answers the "why". https://www.sqlbi.com/articles/the-importance-of-star-schemas-in-power-bi/
User | Count |
---|---|
77 | |
75 | |
46 | |
31 | |
28 |
User | Count |
---|---|
99 | |
93 | |
50 | |
49 | |
46 |