Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Mohanad-Mustafa
Advocate III
Advocate III

Calculate MTD for 2021, 2020 and 2019

Hi Team,

 

I have the following MTD measure below that I created in DAX. It works well for 2021 so I just need to have a similar measure that will also display same MTD but for 2020 and 2019. Can you please help me? Thanks a lot, you guys are amazing and rockstars.

 

MTD = CALCULATE(SUM(Sales[Total Sales]), FILTER('Calendar','Calendar'[Date] >= DATE(YEAR(TODAY()), MONTH(TODAY()), 01)), FILTER('Calendar', 'Calendar'[Date] < TODAY()))
 
Mohanad
1 ACCEPTED SOLUTION

Hi,  @Mohanad-Mustafa 

Not clear what you want. 

You can try the following formula.

 

MTD2019 =
CALCULATE (
    SUM ( Sales[Total Sales] ),
    FILTER (
        'Calendar',
        'Calendar'[Date]
            >= DATE ( YEAR ( TODAY () ) - 2, MONTH ( TODAY () ), 01 )
    ),
    FILTER (
        ' Calendar',
        'Calendar'[Date]
            < DATE ( YEAR ( TODAY () ) - 2, MONTH ( TODAY () ), DAY ( TODAY () ) )
    )
)
MTD2020 =
CALCULATE (
    SUM ( Sales[Total Sales] ),
    FILTER (
        'Calendar',
        'Calendar'[Date]
            >= DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), 01 )
    ),
    FILTER (
        ' Calendar',
        'Calendar'[Date]
            < DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), DAY ( TODAY () ) )
    )
)

 

If it doesn't work ,please share a sample file and expected result for further research.

 

Best Regards,
Community Support Team _ Eason

View solution in original post

5 REPLIES 5
parry2k
Super User
Super User

@Mohanad-Mustafa not sure why it wouldn't work. It follows the best practice. Maybe easier if you throw a sample pbix file and I will look into it. Also, I would like to know when you tried the solution used in the video, what didn't work?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Mohanad-Mustafa there are tons of solutions on how to calculated MTD on parallel period or the same period last year, you should use DAX function DATESMTD instead of using the filter function. Here is a video and I hope it is helpful.

 

Follow us on LinkedIn

 

Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Thanks man. I did try this video before and it didn't hep much with the nature of my dataset, do you know a way to create a measure similar to the one I created that will work for 2019 and 2020 please?

Hi,  @Mohanad-Mustafa 

Not clear what you want. 

You can try the following formula.

 

MTD2019 =
CALCULATE (
    SUM ( Sales[Total Sales] ),
    FILTER (
        'Calendar',
        'Calendar'[Date]
            >= DATE ( YEAR ( TODAY () ) - 2, MONTH ( TODAY () ), 01 )
    ),
    FILTER (
        ' Calendar',
        'Calendar'[Date]
            < DATE ( YEAR ( TODAY () ) - 2, MONTH ( TODAY () ), DAY ( TODAY () ) )
    )
)
MTD2020 =
CALCULATE (
    SUM ( Sales[Total Sales] ),
    FILTER (
        'Calendar',
        'Calendar'[Date]
            >= DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), 01 )
    ),
    FILTER (
        ' Calendar',
        'Calendar'[Date]
            < DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), DAY ( TODAY () ) )
    )
)

 

If it doesn't work ,please share a sample file and expected result for further research.

 

Best Regards,
Community Support Team _ Eason

Hello Eason, thanks a million man, this works like magic. I only had to tweak the code in MTD2019 and change the -1 to -2 to reflect 2019. MTD2020 worked just fine. Code that worked is below:-

MTD 2019 = CALCULATE ( SUM ( Sales[Total Sales] ), FILTER ( 'Calendar', 'Calendar'[Date] >= DATE ( YEAR ( TODAY () ) - 2, MONTH ( TODAY () ), 01 ) ), FILTER ( 'Calendar', 'Calendar'[Date] < DATE ( YEAR ( TODAY () ) -2, MONTH ( TODAY () ), DAY ( TODAY () ) ) ) )

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.