If you need to add ‘Percent of Total’ to your charts in Power BI, here’s how..

Microsoft’s Power BI is excellent.  It is so easy to get up and running with data coming from a multitude of places. Out of the box you can easily show facts and figures related to your data.  One thing it currently misses though is a ‘percent of total’ aggregation for measures.

Recently I wanted to show browser usage for visitors to a web based questionnaire. The client had asked to see the browsers ordered by popularity with their respective percentage share displayed.

The SQL Server table ‘Dim_Respondents’ had the following columns available: LocalRespondentId (unique integer), Browser (text), DateStarted (Date) plus some foreign key references to other tables I could filter by such as Questionnaire Name, Questionnaire Status etc.  It was easy to get the browser count with a horizontal bar chart as follows

Simple Power BI chart by count

Notice how I have just added Browser to the Axis and ‘Count of LocalRespondentId’ to the Value.  The difficulty comes in trying to get percentages because you can only aggregate your data by sum, average, minimum, maximum and count.  The closest you can come is a 100% stacked bar chart;

Power BI stacked bar chart

but with a lot of browsers that is not a workable option.  Luckily Power BI lets us create our own measures using DAX. To do so , you need to right-click on your data table and select ‘New Measure’.

Power BI - Create a new measure

You can then enter your measure name and a DAX expression. (See ‘DAX basics in Power BI Desktop’ for basic DAX information). For our purposes we would like to know the number of respondents with a particular browser divided by the total number of respondents.  Before I create the measure though we need to consider that it is highly likely that this chart will have been further filtered – maybe by date or something else. Our calculation must take that into account.  The easiest way to express it is to say that we need the number of respondents for a particular browser taking into account all applied filters divided by the number of respondents for all browsers taking into account all applied filters (sorry, I know that is a long-winded statement, but bear with me).

In DAX we can write ‘COUNTA(Dim_Respondents[Browser]) ‘ for the first part which is simple enough (basically counts the number of values in a column after all filters have been applied – and remember that we have Browser on an axis so we are effectively filtering by Browser for each chart position), but the second part is a little more tricky. We can solve it by using ‘CALCULATE(COUNTROWS(Dim_Respondents),ALL(Dim_Respondents[Browser])) ‘ which says calculate the first expression ‘COUNTROWS(Dim_Respondents)’ for the second filtered data set ‘ALL(Dim_Respondents[Browser])’. The ‘ALL(..)’ statement clears the filter for the mentioned column so in our case ignores the axis filter on Browser and gives us a total count. The whole expression is

Browser % = COUNTA(Dim_Respondents[Browser]) / CALCULATE(COUNTROWS(Dim_Respondents),ALL(Dim_Respondents[Browser]))

Note that I haven’t multiplied the answer by 100 because it is better to click your new measure and set its format type to ‘Percentage’ in the Modeling tab.

Power BI - Format measure as percentage

You can now replace the count of LocalrespondentId directly with your new measure and set your chart to sort by the percentage;

Power BI - Chart by Percentage

One last nice touch is to put the percentages on the graph by setting Data Labels to ‘on’.

Power BI - Chart with data labels

Hope this helps!

SHARE IT:

Commenting area

  1. I have read books on Dax on Bi and Dax and honestly, your demonstration is the ONLY sound one I found on this topic. You provided the best forwarded solution for calculating percentages in power BI. Looking forward to more insight from you.
    Thanks in advance for the great work
    M. Rayan
    Seni
    TX, USA

  2. Luis Giraldo 30th June 2016 at 3:38 pm · · Reply

    Thanks it was a real solution to my problem! thanks a lot!

  3. Thanks for your comment Luis.

Leave a Reply