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
annerogers
Helper I
Helper I

DATEDIFF calculation error

Hi all, I am trying to use date diff to calculate the number of days between start date and end date.  I keep getting the error below, but when I go back into the data model to look across the rows of data the start date is always before (or less than) the end date.  so why would I get the error "the start date cannot be greater than the end date"?  Appreciate any help.

 errors1.jpgdates.png

1 ACCEPTED SOLUTION

OK, good news!  DateDiff doesn't freak out over Nulls, it just returns another Null.  (See screen shot 3).  It also has no issues with days equal to each other... (Sorry for those wrong paths).   (also screen shot 3).  

 

This is my formula if you want NULLS to pass thru for no End Date.  (Best practics)

DateDiff2 = IF(Table1[Start_Date]<=Table1[End_Date],DATEDIFF(Table1[Start_Date],Table1[End_Date],DAY),(DATEDIFF(Table1[End_Date],Table1[Start_Date],DAY)*-1))

 

This is another option if you want 0 instead of NULL for a null end date.  

DateDiff2 = IF(ISBLANK(Table1[End_Date]),0,IF(Table1[Start_Date]<=Table1[End_Date],DATEDIFF(Table1[Start_Date],Table1[End_Date],DAY),(DATEDIFF(Table1[End_Date],Table1[Start_Date],DAY)*-1)))

 

See if this works and sort the result ascending... i'm curious what PowerBI is seeing that's still making it think the dates are reveresed...  (Please let me know, I'm sucked into the mystery now...)

 

FOrrest

 

Capture.PNGCapture2.PNGCapture3.PNG




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




View solution in original post

9 REPLIES 9
fhill
Resident Rockstar
Resident Rockstar

Double check you don't have any Start Date = End Date situations.  If you do, you can easily add an IF(StartDate = EndDate , 0 , <Your current DateDiff Logic>)




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




I just tried it, I am still getting the same error. 😞

I'm thinking the 'null' or blank End Dates might be your problem....   See if this posting helps to build an IF statement around those blank values..?

 

https://community.powerbi.com/t5/Desktop/function-isnull/td-p/12121

 

Forrest




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




Thank you fhill.

I went there but I am not sure how to combine the calculation suggested in that post with the one I have built.  Any suggestions?

This is what that post had:

IF( ISBLANK(TABLE[MEASURE]), BLANK(),TABLE[MEASURE])

OR 

IF( ISBLANK(TABLE[MEASURE]), 0,TABLE[MEASURE])

 

This is what my calc for the new column:

IF(TABLE_PROGRAM[COLUMN_startdate]=TABLE_PROGRAM[COLUMN_enddate],0, DATEDIFF(TABLE_PROGRAM[COLUMN_startdate],TABLE_PROGRAM[COLUMN_enddate],DAY))

 

Thank you for your help on this.

OK, good news!  DateDiff doesn't freak out over Nulls, it just returns another Null.  (See screen shot 3).  It also has no issues with days equal to each other... (Sorry for those wrong paths).   (also screen shot 3).  

 

This is my formula if you want NULLS to pass thru for no End Date.  (Best practics)

DateDiff2 = IF(Table1[Start_Date]<=Table1[End_Date],DATEDIFF(Table1[Start_Date],Table1[End_Date],DAY),(DATEDIFF(Table1[End_Date],Table1[Start_Date],DAY)*-1))

 

This is another option if you want 0 instead of NULL for a null end date.  

DateDiff2 = IF(ISBLANK(Table1[End_Date]),0,IF(Table1[Start_Date]<=Table1[End_Date],DATEDIFF(Table1[Start_Date],Table1[End_Date],DAY),(DATEDIFF(Table1[End_Date],Table1[Start_Date],DAY)*-1)))

 

See if this works and sort the result ascending... i'm curious what PowerBI is seeing that's still making it think the dates are reveresed...  (Please let me know, I'm sucked into the mystery now...)

 

FOrrest

 

Capture.PNGCapture2.PNGCapture3.PNG




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




Fhill, You are the master!  It worked !!! 🙂

Thank you soooo much. 

 

 

Can you screen shot me some of your data containing the null values?  I want to duplciate on my side to troubelshoot..  FOrrest




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




null end date1.pngHere you go.  Thank you for helping me.

I do have some that are the same.  There are also some where there is not yet an end date.  Would I do something different for them?

 

I will try making the change above.  Thank you for your help.

Anne

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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