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
    • Office 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
    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
    • To **bleep** With TOTALMTD

    To **bleep** With TOTALMTD

    07-25-2020 08:31 AM

    Super User Greg_Deckler
    Super User
    822 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
    • Email to a Friend
    • Report Inappropriate Content

    To **bleep** With TOTALMTD

    ‎07-25-2020 08:31 AM

    It's a little known fact that there are actually five, not four, Horsemen of the Apocalypse, their names being:

    • Death
    • Famine
    • War
    • Plague
    • Insanity (AKA - DAX Time Intelligence Functions)

    So, similar to how I recreated all of those Excel functions, my next fun project is unwiding the insanity that are the DAX Time Intelligence Functions. Sure, I started that a long time ago but might as well get specific. BTW, this all started with To **bleep** With STARTOFQUARTER. Here I cover TOTALMTD, TOTALQTD and TOTALYTD. This also ends up covering DATESMTD, DATESQTD and DATESYTD. The code for these last three is basically all of the code up to and including the __DATESMTD, __DATESQTD and __DATESYTD variables.

    Thus, one can see that the TOTALx functions are really just basically syntax sugar for a CALCULATE more or less because they could be written as TOTALMTD = CALCULATE(...,DATESMTD()) similar to all of those useless OPENINGBALANCE and CLOSINGBALANCE functions.

    ToHellWithTOTALMTD = 
        VAR __Date = MAX('ProductInventory'[Date])
        VAR __Month = MONTH(__Date)
        VAR __Year = YEAR(__Date)
        VAR __StartOf = DATE(__Year,__Month,1)
        VAR __DATESMTD = FILTER(SELECTCOLUMNS(ALL('ProductInventory'),"Date",[Date]),[Date] >= __StartOf && [Date] <= __Date)
    RETURN
        SUMX(FILTER(ALL('ProductInventory'),[Date] IN __DATESMTD),'ProductInventory'[UnitCost]*'ProductInventory'[UnitsBalance])
    
    
    ToHellWithTOTALQTD = 
        VAR __Date = MAX('ProductInventory'[Date])
        VAR __Month = MONTH(__Date)
        VAR __Year = YEAR(__Date)
        VAR __StartOf =
            SWITCH(TRUE(),
                __Month <= 3,DATE(__Year,1,1),
                __Month <= 6,DATE(__Year,4,1),
                __Month <= 9,DATE(__Year,7,1),
                DATE(__Year,10,1)
            )
        VAR __DATESQTD = FILTER(SELECTCOLUMNS(ALL('ProductInventory'),"Date",[Date]),[Date] >= __StartOf && [Date] <= __Date)
    RETURN
        SUMX(FILTER(ALL('ProductInventory'),[Date] IN __DATESQTD),'ProductInventory'[UnitCost]*'ProductInventory'[UnitsBalance])
    
    
    ToHellWithTOTALYTD = 
        VAR __Date = MAX('ProductInventory'[Date])
        VAR __StartOf = DATE(YEAR(__Date),1,1)
        VAR __DATESYTD = FILTER(SELECTCOLUMNS(ALL('ProductInventory'),"Date",[Date]),[Date] >= __StartOf && [Date] <= __Date)
    RETURN
        SUMX(FILTER(ALL('ProductInventory'),[Date] IN __DATESYTD),'ProductInventory'[UnitCost]*'ProductInventory'[UnitsBalance])

    eyJrIjoiNDhhYzc5ZGEtMmI5Zi00MjZjLThmMmUtMTZmZWFiYThhZDg2IiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9


    @ me in replies or I'll lose your thread!!!
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Learn Power BI 2nd Edition
    ToHellWithTOTALMTD.pbix
    Labels:
    • Labels:
    • Time Intelligence
    Message 1 of 1
    822 Views
    1
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic

    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

    © 2022 Microsoft

    Follow Power BI

    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Trademarks