skip to main content
Power BI
    • What is Power BI
    • Why Power BI
    • Customer stories
    • Data visuals
    • Power BI Desktop
    • Power BI Pro
    • Power BI Premium
    • Power BI Mobile
    • Power BI Embedded
    • Power BI Report Server
  • Pricing
    • Azure + Power BI
    • Office 365 + Power BI
      • Energy
      • Healthcare
      • Manufacturing
      • Retail
    • For analysts
    • For IT
    • For developers
    • Overview
    • Find consulting services
    • Partner showcase
    • Find a partner
    • Become a partner
      • Overview
      • Guided learning
      • Documentation
      • Webinars
      • Roadmap
      • Overview
      • Issues
      • Give feedback
    • Blog
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • Events
    • User groups
    • Community blog
    • Register
    • ·
    • Sign In
    • ·
    • Help
    Go To
    • Galleries
    • COVID-19 Data Stories Gallery
    • Themes Gallery
    • Data Stories Gallery
    • R Script Showcase
    • Webinars and Video Gallery
    • Quick Measures Gallery
    • MBAS Gallery
    • MBAS Gallery 2020
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power BI Community
    • Galleries
    • Quick Measures Gallery
    • Measure Totals, The Final Word

    Measure Totals, The Final Word

    10-21-2018 06:09 AM - last edited 10-21-2018 06:22 AM

    Super User IV Greg_Deckler
    Super User IV
    53494 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    Greg_Deckler
    Super User IV Greg_Deckler
    Super User IV
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    Measure Totals, The Final Word

    ‎10-21-2018 06:09 AM

    With apologies to Theodor Geisel...

     

    Measure totals have you perturbed?
    Fear not!
    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.

     

     

     

     

     

    eyJrIjoiODBmNmI4YjItZTMwYi00ZDU4LTg0MWItMzYyZWU3ODk4ZWI4IiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9


    ---------------------------------------

    @ me in replies or I'll lose your thread!!!

    I have a NEW book! 
    DAX Cookbook from Packt
    Over 120 DAX Recipes!




    Did I answer your question? Mark my post as a solution!

    Proud to be a Super User!




    Preview file
    19 KB
    Measure Totals The Final Word.pbix
    Labels:
    • Labels:
    • Mathematical
    • Totals
    Message 1 of 33
    53,494 Views
    52
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    AlejandroPCar
    AlejandroPCar Helper IV
    Helper IV
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎04-12-2019 08:43 PM

    AMAZING! thanks to you a lot!

    Message 11 of 33
    3,066 Views
    0
    Reply
    tkrupka
    tkrupka Resolver II
    Resolver II
    In response to AlejandroPCar
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎04-21-2019 07:43 AM

    I'm still trying to get this, and I just cannot wrap my head around it.

     

    Can't find the correct formula to replace "2"Can't find the correct formula to replace "2"

    Message 12 of 33
    2,971 Views
    0
    Reply
    tkrupka
    tkrupka Resolver II
    Resolver II
    In response to tkrupka
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎04-21-2019 07:59 AM

    Nevermind, 

     

    This worked:

     

    Trial = 
    IF(ISFILTERED(GeneratorNames[GeneratorName])&&ISFILTERED(RunData[Date])&&ISFILTERED(Locations[Location])&&ISFILTERED(IBXs[IBX_Name]),
    SUMX(RunData,[Engine Run Time]*SUMX(GeneratorNames,GeneratorNames[FuelRate])),
    VAR __table = SUMMARIZE(RunData,Locations[Location],IBXs[IBX_Name],GeneratorNames[GeneratorName],RunData[Date],"__value",SUMX(RunData,[Engine Run Time]*SUMX(GeneratorNames,GeneratorNames[FuelRate])))
    RETURN
    IF(ISFILTERED(GeneratorNames[GeneratorName])&&ISFILTERED(RunData[Date])&&ISFILTERED(Locations[Location])&&ISFILTERED(IBXs[IBX_Name]),
    SUMX(RunData,[Engine Run Time]*SUMX(GeneratorNames,GeneratorNames[FuelRate])),SUMX(__table,[__value])))
    Message 13 of 33
    2,967 Views
    0
    Reply
    cosminc
    cosminc Post Partisan
    Post Partisan
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎03-29-2019 09:37 AM

    Hi again,

    i used this and works wonderful

    m_Total 1 = 
    VAR __table = SUMMARIZE('Table1',[Name],"__value",[m_Single])
    RETURN
    IF(HASONEVALUE(Table1[Name]),[m_Single],SUMX(__table,[__value]))

    but you have any idea how can i obtain a evolution of this measure vs same period last year for the same [Name] items?

    i calculated a top 20 for 2019 YTD and i need to compare with same 20 items vs last year same period (not top 20 from last year)

    Thanks,

    Cosmin

    Message 10 of 33
    46,578 Views
    0
    Reply
    cosminc
    cosminc Post Partisan
    Post Partisan
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎03-29-2019 09:11 AM

    Hi

    you don't want to know how much time took me to find this treasure:)

    Thanks a lot!
    Cosmin

     

    Message 9 of 33
    46,643 Views
    0
    Reply
    omarevp
    omarevp Helper II
    Helper II
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎02-12-2019 07:56 AM

    Hi @Greg_Deckler. really awesome post! I hope to see more of this kind of posts in this community.

     

    I've got a question for you, this has been killing me for a while.

     

    I have a "Purchases" table, in whic I want to know the "price" of every item I buy. This is possible by making this simple measure:
    SUM(table[import]) / SUM(table[Quantity]). Divide the sum of the purchase with the quantity of that item bought. Now, I'm trying to compare "Price this month" with "Price last month" in which I ASUME that, I need to make two different measures to know that:

     

    Price this month = CALCULATE ( SUM(table[import]) / SUM([Quantity]), CalendarTable[Relative_Month]="This month")

    Price last month = CALCULATE ( SUM(table[import]) / SUM([Quantity]), CalendarTable[Relative_Month]="Last month")

     

    Now, the next thing I would LOVE to know is "Impact" between those prices, I mean, I need to calculate


    Impact = Price this month - Price last month * SUM(table[Quantity])

     

    It works fine in a table visual, because in table, it is showed by item. And if I export that data to a CSV file, and I do the sum by myself, it shows the correct result, attached is the excel file.

     

    BUT, when I see the TOTAL at the end of the visual it is wrong, and I need it as a Card Visual, so when I put that measure in a Card Visual it shows the wrong result and I have no way to use different filters on it. Attached is the current result I have.


    Another comments=

    - Sometimes, "price this month" or "price last month" is in blank.

    - I already tried applying your measure:

     

    c_Impacto real =
    VAR __table = SUMMARIZE(table,table[item],"__value",[Impact])
    RETURN
    IF(HASONEVALUE(table[item]),[Impact],SUMX(__table,[__value]))
     

    I'd really appriciate your help, or another's help, this been killing me for a big while

     

    THANK YOU SO MUCH!

     

     

    Preview file
    15 KB
    Preview file
    67 KB
    Message 8 of 33
    48,834 Views
    0
    Reply
    JosePintor
    JosePintor
    Frequent Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎12-27-2018 09:39 AM

    Hi Greg,

     

    Great stuff,

     

    With drilldown and TOPN the totals doesn't work properly to repent the total of TOPN ative dimension at current drilldown level,

     

    Can you take a look on this link that i published in Power BI Forum:

     

    https://community.powerbi.com/t5/Desktop/MATRIX-GrandTotal-calculation-with-TOPN-multiples-dimension...

     

    Thanks,

     

    José Pintor 

    Preview file
    216 KB
    Message 7 of 33
    50,595 Views
    0
    Reply
    Anonymous
    Not applicable
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎12-17-2018 11:41 AM

    What happens if the if the "Categories" come from different tables, would the summarize work too?

    Message 5 of 33
    50,747 Views
    0
    Reply
    Greg_Deckler
    Super User IV Greg_Deckler
    Super User IV
    In response to Anonymous
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎12-17-2018 12:23 PM

    Yes, should not matter if the categories come from another table. As long as the things are related properly on the backend in the model, everything should work just the same.


    ---------------------------------------

    @ me in replies or I'll lose your thread!!!

    I have a NEW book! 
    DAX Cookbook from Packt
    Over 120 DAX Recipes!




    Did I answer your question? Mark my post as a solution!

    Proud to be a Super User!




    Message 6 of 33
    50,744 Views
    0
    Reply
    Anonymous
    Not applicable
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎10-25-2018 05:31 PM

    Wow.  This also worked with AVERAGEX.  Now I just have to understand why it works! 🙂  Thank you so much.  

    Message 3 of 33
    51,693 Views
    2
    Reply
    luketerry
    luketerry Advocate II
    Advocate II
    In response to Anonymous
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎10-26-2018 05:01 AM

    This has got to be the biggest oversight of Power BI is the lack of an elegent solution to handling totals.

    Message 4 of 33
    51,674 Views
    9
    Reply
    luketerry
    luketerry Advocate II
    Advocate II
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎10-23-2018 06:42 AM

    Awesome stuff.  I look forward to digging into this more.

    Message 2 of 33
    51,745 Views
    1
    Reply

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Automate
    • Power Virtual Agents

    • Sign in
    • Sign up

    Browse

    • Solutions
    • Partners
    • Consulting Services

    Downloads

    • Power BI Desktop
    • Power BI Mobile
    • Power BI Report Server
    • See all downloads

    Learn

    • Guided learning
    • Documentation
    • Support
    • Community
    • Give feedback
    • Webinars
    • Developers
    • Blog
    • Newsletter

    • © 2021 Microsoft
    • Follow Power BI
    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Trademarks