skip to main content
Power BI
    • What is Power BI
    • Why Power BI
    • Customer stories
    • Data visuals
    • Security
    • Power BI Desktop
    • Power BI Pro
    • Power BI Premium
    • Power BI Mobile
    • Power BI Embedded
    • Power BI Report Server
  • Pricing
    • Azure + Power BI
    • Microsoft 365 + Power BI
      • Energy
      • Healthcare
      • Manufacturing
      • Media
      • Retail
    • For analysts
    • For IT
      • Overview
      • Embedded analytics
      • Power BI visuals
      • Automation
      • Documentation
      • Community
    • Overview
    • Find consulting services
    • Partner showcase
    • Find a partner
    • Become a partner
    • Instructor-led training
    • Getting started
      • Overview
      • Online workshops
      • Self-guided learning
      • Webinars
      • Documentation
      • Roadmap
      • Overview
      • Issues
      • Give feedback
    • Blog
    • Business intelligence topics
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • Events
    • User groups
    • Community blog
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Galleries
    • Community Connections & How-To Videos
    • COVID-19 Data Stories Gallery
    • Themes Gallery
    • Data Stories Gallery
    • R Script Showcase
    • Webinars and Video Gallery
    • Quick Measures Gallery
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    • Events
    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
    • Better Average per Category

    Better Average per Category

    09-09-2022 10:41 AM - last edited 10-09-2022 05:11 AM

    Super User Greg_Deckler
    Super User
    850 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    Better Average per Category

    ‎09-09-2022 10:41 AM

    Microsoft's "Average per category" quick measure does not suffer the same fatal flaw as "Running total", you know, the one where it doesn't work when used in single table models. However, it is stupidly complicated. It's like someone sat down and said, "You know, I could solve this in 5 minutes but the solution wouldn't include CALCULATE". And then they spent the next 3 months figuring out how to incorporate CALCULATE into the calculation. You can find out more about this method at the end of the video here: Video details - YouTube Studio

     

    It's absurd, no sane person would construct the calculation this way:

     

    Value average per Month = 
    AVERAGEX(
    	KEEPFILTERS(VALUES('Table'[Month])),
    	CALCULATE(SUM('Table'[Value]))
    )

     

    Even seasoned DAX pros would probably initially scratch their heads a little having a CALCULATE statement with no filter clause whatsoever. Disreading the fact that this formula uses VALUES and anything VALUES can do DISTINCT can do better, what is going on here is complex and not easily explained to someone new to DAX. For one thing, it's very odd to see a KEEPFILTERS statement as not being part of a CALCULATE statement since the documentation on KEEPFILTERS specifically notes that it is designed for use in CALCULATE statements. However, KEEPFILTERS does return a table, which is why it is valid as the first parameter for AVERAGEX even though no sane individual would probably think of doing it that way. And, by using an X aggregator (AVERAGEX) the second parameter (the expression) is evalulated in the context of the table expression. So, it works.

     

    By why? All you want to do is SUM up some column grouped by a category and then take the average of those values. Why all the jumping through hoops and twisting your context into knots when you can accomplish the exact same thing much more intuitively, as in:

     

    Better Average per Category = 
        VAR __Table = SUMMARIZE('Table',[Month],"Value",SUM('Table'[Value]))
    RETURN
        AVERAGEX(__Table, [Value])

     

    Super simple and straight-forward. SUMMARIZE the table by your category (month) and SUM the values. Take the average across that table. Simple, straight-forward and far less complicated to explain. Oh, you don't like SUMMARIZE because of that one blog article that pointed out that in incredibly rare circumstances that you are almost certainly never to encounter that you might get wonky results? Fine, use GROUPBY.

     

    Better Average per Category 2 = 
        VAR __Table = GROUPBY('Table',[Month],"Value",SUMX(CURRENTGROUP(),'Table'[Value]))
    RETURN
        AVERAGEX(__Table, [Value])

     

     I say again. Create a table VAR. Use an X aggregator. It works and you don't need CALCULATE.

     

    Watch the video!

     

     

    eyJrIjoiMDQzMjdkNTQtOTJkZC00NDI2LTgxZjktZWQxMjNmYjUwMzVlIiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9


    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    Better Average per Category.pbix
    Labels:
    • Labels:
    • Mathematical
    • Totals
    Message 1 of 1
    850 Views
    0
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Pages
    • 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

    © 2023 Microsoft

    Follow Power BI

    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Trademarks
    California Consumer Privacy Act (CCPA) Opt-Out Icon Your California Privacy Choices