10-21-2018 06:09 AM - last edited 10-21-2018 06:22 AM
With apologies to Theodor Geisel...
Measure totals have you perturbed?
It's Measure Totals, The Final Word,
These measures work with matrices,
They work with tables,
They work with rows and columns and labels.
They work in the daytime,
They work at night,
They work to make sure the totals are right!
Now that you've seen them,
Now that you've heard,
Shout it out loud, it's Measure Totals, The Final Word!
At some point, we've all been frustrated by measure totals. If you want to understand why, read this post.
The technique employed here is fairly simple and should work in all "standard" cases of where you just want the Total line to, well, display the total (sum) of a measure. For more complex scenarios, see my Matrix Measure Total Triple Threat Rock & Roll measure.
Essentially, create a measure, any measure, that performs your desired calculation and returns the correct result at the row level. This becomes your "m_Single" measure. Now, create an "m_Total" measure that performs a SUMMARIZE of your data, exactly as how it is displayed in your table or matrix and use the "m_Single" measure within that SUMMARIZE statement to provide the values for the individually summarized rows. Finally, perform a SUMX across that summarized table. The measures presented in this PBIX file also do a HASONEVALUE check that isn't really necessary in most cases but perhaps lends a little confidence to the user that the SUMX is only employed in the Total line and might also add some performance improvements.
In effect, you are recreating the displayed visualization in memory as a table and then doing a summation across that table for the total line, as you would intuitively expect a total line in a table or matrix to work.
So, if we have a measure like:
m_Single = SUM(Table1[Value])-50
This measure will cause problems in total lines. So, if we are summarizing by [Name], we create this measure:
m_Total 1 = VAR __table = SUMMARIZE('Table1',[Name],"__value",[m_Single]) RETURN IF(HASONEVALUE(Table1[Name]),[m_Single],SUMX(__table,[__value]))
If we are summarizing by [Category1], we create this measure:
m_Total 2 = VAR __table = SUMMARIZE('Table1',[Category1],"__value",[m_Single]) RETURN IF(HASONEVALUE(Table1[Category1]),[m_Single],SUMX(__table,[__value]))
And so on...
We use these "m_Total" measures in our visualizations. The "m_Single" measure is still used, but not directly in the visuals themselves.
Is it annoying to have to create multiple measures and specifically tailor them to each individual visual? Yes, yes it is.
Firstly thanks Greg for this great article.
when dealing with a measure that is in itself a sumx , is there something i need to do differently in creating the variable table?
the formula works for each month but total is returnign zero and i ssupect it is due to the sumx formula not creating the summarizes table correctly.
After looking at this a bit have developed an alterative solution:
i created an index column in the data table for the level of calculations i need ie Customer + Product+ Month
Then i created a new table summarising that column and linked the new table back to the data table.
Then in the sumx formula used the new table as the reference Sumx( new table (custProdMonthnum), calculations)
this means it forces the total column to do the calcs at the month level, so all adds up. also it means just one formula and doesnt worry if you add in qrtly totals etc..
hi - I used your solution at the lowest level of my hiearchy but now how do I get the numbers for aggregate up to the higher levels properly (see table below)?
I suggest you post your question as a new thread in the "Help" Forum. I don't think this thread should be used to ask questions for Help. You will get many more eyes looking at it which will assist you further.
Also please include as much detail as you can.
Good Luck and Best Regards,
@rj10014 Don't feel bad man, this one comes up a LOT!! It is super unintuitive to people just learning DAX but is a great example to help expand your understanding of how important context is to DAX!
Below is a screen shot from my excel file that I have uploaded into Power BI.
I want to show that in Cook County (for my current period and for the fuel type water) that Brand 1 has sold a total of 8, which is equal to 13% share
In Power BI if I filter on Cook I get what I need.
When I add another County, the numbers get messed up.
Where am I going wrong. I’ve attached the pbix file. I am not a Dax guy, I am trying to learn. Thanks.
I have a calculated field Goal which is not giving the sum correct.
How do I make it working?