cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jsquaredz
Advocate I
Advocate I

Datediff start date cannot be greater than the end date

I have been using DAX DATEDIFF statement  to calculate No Days between two dates which are

Target Completion Date and Actual Completion Date

 

Which works all very well except now I have sometimes the Actual Completion Date is before the Target Completion Date (ie where an Action Officer performed better and completed the task before the "target" date).

 

However, of course, DATEDIFF doesn't like this and gives error "In DATEDIFF function, the start date cannot be greater than the end date"

 

Why can't this Dax function just return the negative value in this case?  Is there a way we can enhance this function to return a signed integer?

1 ACCEPTED SOLUTION
Framet
Resolver II
Resolver II

Hi,

 

Whilst I can't answer the "why" it can't return a negative integer for dates where the start date is after the end date I can suggest you could simply handle it with a IF statement. Test your dates if the start date is greater than the end date then change the sign of the result datediff with the dates passed to the function the other way round.

DateDiffMeasure :=
IF (
    [StartDateField] > [EndDateField],
    0 - DATEDIFF ( [EndDateField], [StartDateField], DAY ),
    DATEDIFF ( [StartDateField], [EndDateField], DAY )
)

Might work for you?

View solution in original post

11 REPLIES 11
Catcheson
Frequent Visitor

I was using the Feb 2018 version of BI Desktop and the DATEDIFF worked perfectly and did return a negative answer.  However for reasons of incompatibility with the Report Server I had to revert to the June 2017 version and I now have this problem.

 

I’m not sure what version you are on and when the the feature started to work but upgrading might be a solution for you.

 

unfortunately for me, it’s still a pain in the neck as in fact is most of Power BI!  Never again will I use software that isn’t close to being finished and operational!  

 

Anonymous
Not applicable

Makes totally sense what you suggest. Microsoft, Why not fix this properly or create a function what does work (to ensure existing reports do not change behaviour).

kdaya
Helper I
Helper I

HI please can someone help 

 

I am trying to calculate the total amount of hours between Todays date and set date that i have in my table but i keep getting the same error

 

DUE BY TIME = DATEDIFF(request[DueBy Time].[Date], TODAY(),HOUR)

 

The due by time is usually greater than the current date

 

Thanks,

heidibb
Helper IV
Helper IV

I realize this is an older topic, but I came across it when searching for a similar solution. I wanted to share that this solution also worked for me:

 

Days Btwn = -1 * (format(start_date, "MM-DD-YYYY") - format(end_date, "MM-DD-YYYY")) 

 

I needed to add the format since our dates have timestamp and I wanted the difference in days regardless of the hour. If you are ok with getting portions of days, leave off the format!

 

Hope this  helps someone!

 

Framet
Resolver II
Resolver II

Hi,

 

Whilst I can't answer the "why" it can't return a negative integer for dates where the start date is after the end date I can suggest you could simply handle it with a IF statement. Test your dates if the start date is greater than the end date then change the sign of the result datediff with the dates passed to the function the other way round.

DateDiffMeasure :=
IF (
    [StartDateField] > [EndDateField],
    0 - DATEDIFF ( [EndDateField], [StartDateField], DAY ),
    DATEDIFF ( [StartDateField], [EndDateField], DAY )
)

Might work for you?

This worked well.  Thanks for posting!

This worked well.  Thanks for posting!

hmedinaa
Frequent Visitor

It works great

Thanks,

Thanks.  I realize there is a workaround we can wrap around the functions behaviour, but  I would like this built in Dax function so that it returns a signed integer.  Makes more sense to me that I just put in two dates and it returns the difference.

In that case I think your best course of action is to make a suggestion on the idea forum for the behaviour of the function be changed and see if other users in the community support it.

https://ideas.powerbi.com/forums/265200-power-bi-ideas

 

Many thanks

 

Thomas

Looks like someone else has this same idea.  Please vote this up.  Much appreciated.

 

https://ideas.powerbi.com/forums/265200-power-bi-ideas/suggestions/16841479-date-diff-start-date-not...

 

 

Helpful resources

Announcements
August 2022 update 768x460.jpg

Check it Out!

Click here to learn more about the August 2022 updates!

Power BI Dev Camp Session 25 with aka link 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, August 25 at 11a PDT for a great session with Ted Pattison!

August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors