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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
kg_powerbi
Frequent Visitor

DateAdd and DatesMTD not working

Hello All,

I am working on a measure that will return MTD sales for completed days in the month vs the same amount of days in the previous month.  For whatever reason if I use this formula it doesn't work as expected.

DATEADD(
DATESMTD('Date'[Date])
,-1,MONTH)
 
It turns the total days for the previous month.  So first I tested if
DATESMTD('Date'[Date]) worked as expected by created a DAX table with the DATESMTD formula and it worked as expected.

 
 

DATESMTD.png

 


Then if I changed the formula to the below 
I get the whole month of November, not 11/1/2020 through 11/19/2020 as I thought it would work.

 

 DATESMTD-1.png

 

Then I found curbal's video https://www.youtube.com/watch?v=-xBYtOVyMTs&t=308s and made some changes to my formula to exclude the last date in my calendar table (today) and it works perfectly.

 

 

 

 

DATESMTD-1_day.png
When I try to change this formula to include the last date, it returns the total days in November again.

 

 

 

 

DATESMTD-1_day_wrong.png
Any idea what I'm doing wrong, or another way to code around this?  The reason I like the simplicity is that for completed months in the past, works like PREVIOUSMONTH formula, but for the current month, it works by comparing apples to apples (not previous completed MTD total vs current month completed days).

 

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

To write a measure that will return MTD sales for completed days in the month vs the same amount of days in the previous month, you do not need to create another Date table.  Assuming you already have a Calendar table which only goes till the data till which there is sale, write these measures.

Total sales = sum(data[sale])

Total sales in same period last month = if(max(calendar[date])<eomonth(max(calendar[date]),0),calculate([total sales],datesbetween(calendar[date],edate(min(calendar[date]),-1),edate(max(calendar[date]),-1))),calculate([total sales],previousmonth(calendar[date])))

To your visual/filters/slicers, drag date and other other date field (month/year) from the Calendar Table.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
kg_powerbi
Frequent Visitor

Thanks again for your help.  Any idea how to do Quarter To Date vs same time last quarter?

You are welcome.  Try the STARTOFQUARTER() and ENDOFQUARTETR() DAX functions.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
kg_powerbi
Frequent Visitor

Thanks Ashish.

Any idea why the DateADD (DATESMTD(Date[Date]),-1,MONTH) isn't working as expected?  Seems like a bug?

You are welcome.  I do not use the DATEADD() function.  Did my solution work?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Ashish_Mathur
Super User
Super User

Hi,

To write a measure that will return MTD sales for completed days in the month vs the same amount of days in the previous month, you do not need to create another Date table.  Assuming you already have a Calendar table which only goes till the data till which there is sale, write these measures.

Total sales = sum(data[sale])

Total sales in same period last month = if(max(calendar[date])<eomonth(max(calendar[date]),0),calculate([total sales],datesbetween(calendar[date],edate(min(calendar[date]),-1),edate(max(calendar[date]),-1))),calculate([total sales],previousmonth(calendar[date])))

To your visual/filters/slicers, drag date and other other date field (month/year) from the Calendar Table.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 Power BI update to learn about new features.