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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

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

MayPowerBICarousel

Fabric Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.