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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
robhel
Helper I
Helper I

Days between Dates with TODAY and End Date before Start Date

I am really after some much needed help here after reading through and experimenting with a number of prior posts, however none of them are assisting me.

 

I am after a column that will calculate the days between two date fields, with TODAY() used in any blank end date fields, with one other parameter is that some scheduled start dates are after the actual completion date

 

I’ve tried both DATEDIFF (however got error message of start date cannot be before end date) and also DATESBETWEEN (however got error message of only able to use dates in this function)

 

Days Design = IF(ISBLANK([DESIGN_ACT]),DATEDIFF([DESIGN_SCHED],TODAY(),DAY),DATEDIFF([DESIGN_SCHED],[DESIGN_ACT],DAY))

 

Any assistance in DAX code to complete this would be greatly appreciated.

Thanking you

1 ACCEPTED SOLUTION

I thought so, the DATEDIFF function was fixed to return negative numbers in like the June version of Power BI Desktop. Perhaps earlier. Just tried it and it worked perfectly.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
robhel
Helper I
Helper I

Thank you for your fast responses to my question & yes, downloading the latest BI desktop performed the DATEDIFF function
Greg_Deckler
Super User
Super User

I thought that they had fixed that with DATEDIFF, what version of Power BI are you on?

 

You can always use:

 

Column 2 = ([Column2].[Date]-[Column1].[Date]) * 1.

Make sure that the new column is set to type Whole number. This will give you the days between the dates and will return negatives.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

I thought so, the DATEDIFF function was fixed to return negative numbers in like the June version of Power BI Desktop. Perhaps earlier. Just tried it and it worked perfectly.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
OwenAuger
Super User
Super User

@robhel

You can simply subtract one date from another (and multiply the result by 1 to force an integer to be returned) to get the difference in days. Subtracting dates will never return an error, but you will get a positive or negative result depending on the order of the dates.

 

I would recommend DAX similar to below, including the table name before any column references to avoid confusion with measures:

Days Design =
VAR EndDate =
    IF ( ISBLANK ( TableName[DESIGN_ACT] ), TODAY (), TableName[DESIGN_ACT] )
RETURN
    1 * ( EndDate - TableName[DESIGN_SCHED] )

DATEDIFF performs the same function if the third argument is DAY, but in some versions of the engine has the limitation you encountered where the first date must not be later than the second date.

 

DATESBETWEEN returns a single-column table of dates taken from a date column in a physical table, between specified start and end dates. This function is normally used to produce a set of dates to be used as a filter, such as within the CALCULATE function.

 

Regards

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.