Skip to main content

Playing with Totals in Power BI

Are you a fan of matrix visual in Power BI? If you are as I am, I always struggle to get the correct totals and get something else instead of the totals it can be average. After a lot of research and going over different community posts, finally we have found 3 common scenarios that can elevate your matrix to the next level.

To start with, I am using Sample Superstore data. Let's first explain the 3 different scenarios that we will tackle -

  1. We would like to show both Total Sales and Average Monthly Sales across different categories and different periods.
  2. We would like to show the Average Sales in the row subtotals and Total Sales in the column subtotals.
  3. Last and the most interesting scenario is to show the Total sales excluding the furniture sales in the row subtotals and total sales in the column subtotals.

Let's start by getting the correct totals in a matrix. Generally, if use basic Sum, Average... functions in your measures then most likely you will get the incorrect totals. But why? Very simple answer would be Power BI isn't smart enough to recognize the level on which Sum, Average... should be calculated. How to get it right then? Use of iterator functions such as SUMX, and AVERAGEX.. to know more about it in detail we would recommend having a quick read on one of the most popular blogs on our website- Wrong totals in Power BI.

Once you have the totals corrected we will start with the first scenario. So, we will pull Order Year (calculate column) in the columns and Category in the rows. For every year we would like to see Average Monthly Sales next to the Total Sales. We would need a different measure that will help to land into this condition if there is one value provided for the year then show the total sales otherwise the average sales are calculated with AVERAGEX.


Avg Monthly Sales =
IF (
    HASONEVALUE ( 'Date'[Year] ),
    [Total Sales],
    AVERAGEX ( SUMMARIZE ( 'Date', 'Date'[MonthNumber] ), [Total Sales] )
)


We have utilized HASONEVALUE to define this condition. HASONEVALUE checks if a particular column has a single value if not then it returns FALSE. We have taken advantage of the FALSE scenario of HASONEVALUE. We would recommend, to refer the DAX Guide for HASONEVALUE to know in detail what else can be achieved with this function.

Easy peasy lemon squeezy!! 😂 Moving to the second scenario, now we are looking for a similar setup as the first scenario but this time we only need the average sales on the row subtotals and total sales as the column subtotals. To achieve it with a single measure we will take benefit of basic SWITCH and ISINSCOPE functions.


Correct Subtotals =
SWITCH (
    TRUE (),
    ISINSCOPE ( SampleSuperstore[Category] )
        && ISINSCOPE ( SampleSuperstore[Order Year] ), [Total Sales],
    ISINSCOPE ( SampleSuperstore[Category] ), [Average Sales],
    ISINSCOPE ( SampleSuperstore[Order Year] ), [Total Sales],
    [Total Sales]
)

To calculate Average Sales we are using this measure

Average Sales =
CALCULATE (
    AVERAGEX ( VALUES ( SampleSuperstore[Order Year] ), [Total Sales] )
)



What is going on here 😵‍💫??? Okay, let's debunk the DAX step by step. To understand the Correct Subtotals measure wewill first need to understand the function ISINSCOPE. In simple words, ISINSCOPE allows us to detect if a particular field is in the row or column of a visual. To understand ISINSCOPE in detail we would recommend visiting DAXGUIDE.

Correct Subtotals DEBUNKED =
SWITCH (
    TRUE (),
    ISINSCOPE (Row_Name)
        && ISINSCOPE ( Column_Name ), [Total Sales], /// if this is true then show all numbers in between
    ISINSCOPE ( Row_Name ), [Average Sales], /// if this is true, then show numbers on right
    ISINSCOPE ( Column_Name), [Total Sales], /// if this is true, then show the total sales at bottom
    [Total Sales] /// else show the total at the bottom right corner
)

Too much playing with DAX for the day!! Let's do one last scenario, we would like to show the Order year in the row and Categories in the column. Row subtotals should represent the Sales without the furniture category sales and column subtotals should show the total sales.


Correct Subtotals excluding furniture =
SWITCH (
    TRUE (),
    ISINSCOPE ( SampleSuperstore[Category] )
        && ISINSCOPE ( SampleSuperstore[Order Year] ), [Total Sales],
    ISINSCOPE ( SampleSuperstore[Order Year] ), [Sales excluding furniture],
    ISINSCOPE ( SampleSuperstore[Category] ), [Total Sales],
    [Sales excluding furniture]
)


It's too confusing now. Okay, let's create a debunk measure for it then. We are using the same ISINSCOPE function which will return TRUE if a particular field is used in the row or column in a visual (matrix in this case). Otherwise, it will return FALSE.

Correct Subtotals excluding furniture DEBUNKED=

SWITCH (
    TRUE (),
    ISINSCOPE ( Column_Name )
        && ISINSCOPE ( Row_Name ), [Total Sales], /// if both col and row are in scope then show all numbers in between
    ISINSCOPE ( Row_Name ), [Sales excluding furniture], /// if only order year in scope then show numbers on the right
    ISINSCOPE ( Column_Name ), [Total Sales], /// if only column is in scope then show total sales, all numbers at the bottom row 
    [Sales excluding furniture] /// if none of the row and column in scope then show the total sales without furniture sales, number on extreme right corner
)

Protip: Using all these measures is fun but beware that by default matrix will only show totals as the label for the column and row subtotals. To change it select the matrix and go to the Column Subtotal and Row Subtotal to change it. Also, if you do not want to provide a label just click on the Subtotal label and provide a space.


Isn't it amazing?? In this blog, we have only explored 3 scenarios but there can be more such scenarios. Most of such scenarios can be tackled with a slight tweaking to the measures provided above. We would strongly recommend before you try these measures have a basic understanding of HASONEVALUE and ISINSCOPE. 




"All our articles are intended to address all the frequently asked questions related to a topic. Do leave a comment if it answers any of your question. "


Thanks for Reading Let's connect on LinkedIn. For more such blogs and pro tips do follow us








Comments

Popular posts from this blog

Identify and Delete Unused Columns & Measures

Heavy dashboards and a bad data model is a nightmare for every BI Developer. Heavy dashboards can be slow due to multiple reasons. It is always advised to stick with best practices. Are you still figuring out about those best practices then you should definitely have a quick read on Best Practice Analyser ( link ). One of the most common issues with slow dashboards is unused columns and unused measures.  It is very normal to load some extra columns and create some test measures in your dashboard but as a part of cleanup process those unused columns and unused measures should be removed. Why we are removing them? Because if you keep them then ultimately it will increase the size of your data model which is not a good practice.  How to identify the culprits (unused columns and unused measures)? In today's blog we will provide you with 2 most common external tools which will help you in identifying the culprits. More external tools😒. Who's going to pay for this? To your surprise...

Best Practice Analyser (BPA) Guide

Do you want to save tons of efforts to check if your data model and PBIX file follows the standard best practices and norms? Then this blog is for you. If you are a follower of our channel we already deep dive into the importance of the DAX Studio as an external tool. If you are a beginner I would highly recommend to visit this blog . In today's blog we will check how Tabular Editor can help to optimize the data model.  Best Practice Analyser allows to define or import best practices. It will make sure that we do not violate the best practices while developing a dashboard. Isn't it exciting!! Before we start make sure you already have Tabular Editor version 2.24.1 installed on your system. To install it do visit this link and select the link for windows installer. Once Tabular Editor is installed it will reflect in your PBIX file under external tool. Also, we need to define the standard rules. To do so in your advanced scripting or C# script copy this and save it via Ctrl+S. An...

Copying Bookmarks from one Power BI report to another

Let's think of a scenario, where you want to copy the bookmarks from one report to another. Most obvious approach is to just do a copy paste of the bookmarks. What's wrong with this approach? This approach only works for all visuals but not for bookmarks and field parameters.  If you are not familiar with basics of bookmarks and field parameters do refer to the beginners guide for bookmarks  and introduction to field parameters . Then how do you copy the bookmarks? Power BI enhanced report format (PBIR) for Power BI Project files (PBIP) will help you in achieving this. Let's check it out, I have 2 reports one contains the bookmark called Bookmarks PBIR Test (origin) and other one is Rolling Average PBIR Test (destination) .  Before we get started, you have to enable Power BI Project save option under preview features. Once enabled, restart Power BI desktop. There is a TMDL icon appearing on the left pane. What is TMDL and what's in it for me? There's a lot of possi...