Reply
Highlighted
Frequent Visitor
Posts: 8
Registered: Monday
Accepted Solution

Calculate Days Between Dates

New to Power BI and need help with calculating the # of days between two date columns. I tried creating a datediff column per other solutions, but that didn't work. Any other suggestions or tweaks to the formula?

 

Data Info:

Column 1: Created Date

Column 2: Executed Agreement Date

Capture.PNGNote - 


Accepted Solutions
Super Contributor
Posts: 1,825
Registered: ‎08-11-2015

Re: Calculate Days Between Dates

[ Edited ]

@kpangelinan

You must have end_dates that are prior to their corresponding start_dates!

Even if this happens only on one row in your entire dataset - you'll get this error!

Adjust your formula like this...

Days Column = 
SWITCH (
    TRUE (),
    'Table'[start_date] < 'Table'[end_date], DATEDIFF ( 'Table'[start_date], 'Table'[end_date], DAY ),
    'Table'[start_date] > 'Table'[end_date], DATEDIFF ( 'Table'[end_date], 'Table'[start_date], DAY ) * -1,
    0
)

You multiply * -1 to get negative numbers where the event ended before it started!

This way you'll be able to quickly identify any issues with your data indicated by the negative numbers.

Hope this helps! Smiley Happy

View solution in original post


All Replies
Super Contributor
Posts: 1,825
Registered: ‎08-11-2015

Re: Calculate Days Between Dates

[ Edited ]

@kpangelinan

You must have end_dates that are prior to their corresponding start_dates!

Even if this happens only on one row in your entire dataset - you'll get this error!

Adjust your formula like this...

Days Column = 
SWITCH (
    TRUE (),
    'Table'[start_date] < 'Table'[end_date], DATEDIFF ( 'Table'[start_date], 'Table'[end_date], DAY ),
    'Table'[start_date] > 'Table'[end_date], DATEDIFF ( 'Table'[end_date], 'Table'[start_date], DAY ) * -1,
    0
)

You multiply * -1 to get negative numbers where the event ended before it started!

This way you'll be able to quickly identify any issues with your data indicated by the negative numbers.

Hope this helps! Smiley Happy

Frequent Visitor
Posts: 8
Registered: Monday

Re: Calculate Days Between Dates

Thank you @Sean! This worked and now I can easily idenify those records that have "executed" dates before a "created" dates. Thank you for the quick response.