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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
mandar
Frequent Visitor

DAX calculation for MTD -- with a small twist

Hi,

 

I need to be able to write a DAX logic which would give me the MTD sales on any given day. Below logic works like a charm

 

Sales = CALCULATE(SUM('Fact'[Sales]), DATESMTD('Date'[Date]))
 
However, the catch is that the 1st of every month should show the MTD Sales of previous month. Going by the current logic it would just give me Sales for that day which is the 1st.
 
Any suggessions please.
 
mandar_0-1681216182734.png

 

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@mandar You could do something like this:

Sales = 
  VAR __Date = MAX('Date'[Date])
  VAR __Result =
    IF(DAY(__Date) = 1,
      CALCULATE(SUM('Fact'[Sales]), PREVIOUSMONTH('Date'[Date])),
      CALCULATE(SUM('Fact'[Sales]), DATESMTD('Date'[Date]))
    )
RETURN
  __Result

That said, the TI functions are slow.

 


Follow on LinkedIn
@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

2 REPLIES 2
Greg_Deckler
Super User
Super User

@mandar You could do something like this:

Sales = 
  VAR __Date = MAX('Date'[Date])
  VAR __Result =
    IF(DAY(__Date) = 1,
      CALCULATE(SUM('Fact'[Sales]), PREVIOUSMONTH('Date'[Date])),
      CALCULATE(SUM('Fact'[Sales]), DATESMTD('Date'[Date]))
    )
RETURN
  __Result

That said, the TI functions are slow.

 


Follow on LinkedIn
@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler  - Awesome, work amazingly well!! Thank you so much!! 🙂

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.