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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
sarthakgirdhar
New Contributor III

How to calculate percentage of column total?

2.png

 
Hi DAX gurus,

I have some good news! AI won't be replacing us anytime soon. Both ChatGPT and CoPilot were unable to solve this.

All I am trying to do is write a dynamic DAX measure that calculates the percentage of students who obtained a grade X over the column total. 

I have many filters/slicers on this page. As you can see in the attached image, the default tooltip for "100% stacked bar chart" gives the right percentage (15.38%, in this case) every time. I can change the filters that affects the total, and I still get the correct percentage.

My written DAX measure Grade Percentage fails to do so. I'd like your help in writing the correct DAX measure. I will eventually use this measure to create a customized Tooltip.

P.S. - The REPLY has the Data Model too.


Many thanks,
Sarthak


1 ACCEPTED SOLUTION
GeraldGEmerick
Contributor

@sarthakgirdhar The classic approach to this would be something along the lines of:

% Total = 
VAR _Total = CALCULATE( COUNTROWS( 'Course Grades' ), ALLSELECTED( 'Course Grades' ) )
VAR _Part = COUNTROWS( 'Course Grades' ) 
// or something like COUNTROWS( FILTER( 'Course Grades', [Grade] = "A" )
VAR _Return = DIVIDE( _Part, _Total )
RETURN _Return

View solution in original post

5 REPLIES 5
sarthakgirdhar
New Contributor III

3.png

 

THE DATA MODEL

GeraldGEmerick
Contributor

@sarthakgirdhar The classic approach to this would be something along the lines of:

% Total = 
VAR _Total = CALCULATE( COUNTROWS( 'Course Grades' ), ALLSELECTED( 'Course Grades' ) )
VAR _Part = COUNTROWS( 'Course Grades' ) 
// or something like COUNTROWS( FILTER( 'Course Grades', [Grade] = "A" )
VAR _Return = DIVIDE( _Part, _Total )
RETURN _Return

Hey @GeraldGEmerick,

Thank you for your reply!

I figured out the issue. It wasn't my DAX measure that was the issue, it was actually the "100% stacked Bar chart".
You see, the aforementioned visual already normalizes the values, and with my measure, there was double normalization that was happening hence, the wrong percentage. The calculation works just fine on the "normal" Stacked bar chart.

Glad to hear it!

v-priyankata
Honored Contributor

Hi @sarthakgirdhar 

Thank you for reaching out to the Microsoft Fabric Forum Community.

@GeraldGEmerick Thank you for your prompt response.

I'm glad your issue has been resolved. Please mark the helpful reply or your own response as the solution so it can assist others with similar issues.

Helpful resources

Announcements
Users online (27)