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

Shape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.

Reply
PowerPaddy
Frequent Visitor

DATEDIFF month interval not working as expected.

Hello again,

 

I'm using the DateDiff function to work out after how many months a policy was cancelled but it's not giving me the expected results.

Cancellation Month = DATEDIFF([POLICYSTARTDATE], [CANCELDATE], MONTH)

 

I'm expecting a 0 for policies that were cancelled within one month, ie where the difference between Date1 and Date2 is less than 1 month.

 

DAX seems to only return a 0 if Date1 and Date2 are in the same month, ie if Date1 is 31 Jan 17 and Date2 is 1 Feb 17 then the above formula returns 1. My understanding of this function is that it shouldn't work like this.

 

Here's an extract...

 

Capture.PNG

1 ACCEPTED SOLUTION

@PowerPaddy

 

To get more accurate "MONTHDIFF", you can create a column like below:

 

Cancellation Month =
 ( 1
    * ( [CANCELDATE] - [POLICYSTARTDATE] ) )
    / DAY ( EOMONTH ( [CANCELDATE], 0 ) )

Regards,

View solution in original post

6 REPLIES 6
Greg_Deckler
Super User
Super User

Your understanding is incorrect. DATEDIFF counts the "interval boundaries" crossed between the two dates. Therefore, going from Jan 31st to Feb 1st crosses the month interval boundary (new month).



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Thank you.

IBM gives the description of the DATEDIFF function on their website...
"The result will be the integer portion of the number of months between the two dates, with any fractional component truncated."

 

I will have to count number the number of days instead as a workaround.

@PowerPaddy

 

To get more accurate "MONTHDIFF", you can create a column like below:

 

Cancellation Month =
 ( 1
    * ( [CANCELDATE] - [POLICYSTARTDATE] ) )
    / DAY ( EOMONTH ( [CANCELDATE], 0 ) )

Regards,

IBM is not the authority on DAX:

 

This is the authority:

 

https://msdn.microsoft.com/en-us/library/dn802538.aspx

 



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

How would you get it so that the function returns 0 if the difference between the two dates is less than one exact month? For example, if the start date is Jan 31st and the cancelled date is 1st Feb, we get 0 rather than 1. 

 

Thanks!

Well, that's sort of going to depend on your definition of 1 exact month I suppose. You could probably get close with something like this:

 

Column = MROUND([DateCancelled]-[Date]*1.,30)/30


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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.