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
khalidmadih
Frequent Visitor

Difference between date culumns with an IF like statement

Hey guys,

 

I am using PowerBi in my company to create some dashboards, and I need to incorporate a calculated column in my data set that calculates the following :

1/ The difference in Days between column [StartDate] and [EndDate]

2/ However if the [EndDate] row is empty, it should use (Today) as a date

 

I appreciate the help.

Regards,

 

Khal

1 ACCEPTED SOLUTION

Again..thank you very much for your help solving this.

 

I finally approached it a little bit differently, by eliminating the data rows from the logic where the "start date" was greater than the "end date". I simply added another IF statement to put a 0 in that row if that condition is true.

 

Here is the new formula, hopefully it will help other people with the same issue:

NewColumn = IF('Table'[StartDate] <= IF(ISBLANK('Table'[EndDate]),TODAY(),'Table'[EndDate]) , DATEDIFF('Table'[StartDate], if(ISBLANK('Table'[EndDate]),TODAY(),'Table'[EndDate]),DAY), 0)

View solution in original post

6 REPLIES 6
ankitpatira
Community Champion
Community Champion

@khalidmadih Below is DAX query that you want to use to create new calculated column.

 

NewColumn = DATEDIFF(YOURTABLE[startdate],IF(ISBLANK(YOURTABLE[enddate]), TODAY(), YOURTABLE[enddate]),DAY)

Thank you for looking into this!

 

I tried your solution but I am getting the following error message "In DATEDIFF function, the start date cannot be greater than the end date".

 

For reference here is what I have enetered:

Days Active = DATEDIFF('Member Profile'[Original Member Effective Date],IF(ISBLANK('Member Profile'[Terminated Member Date]), TODAY(), 'Member Profile'[Terminated Member Date]),DAY)

@khalidmadih As error suggests, you need to reverse your columns so your DAX would be,

 

Days Active = DATEDIFF('Member Profile'[Terminated Member Date],IF(ISBLANK('Member Profile'[Original Member Effective Date]), TODAY(), 'Member Profile'[Original Member Effective Date]),DAY)

 

However ensure that if first column has a date which is higher than date in second column then it is the case for all the values, otherwise query will fail.

The 1st formula is the correct one. However it looks like there is a chance that the start date could be equal or greater than the end date. Any other way to go around this issue?

@khalidmadih ok. Do this instead,

 

NewColumn = (YOURTABLE[end] - YOURTABLE[start]) * 1.

 

This will work regardless of dates are gretaer or lower in either columns and will give you number accordingly either in positive or negative.

Again..thank you very much for your help solving this.

 

I finally approached it a little bit differently, by eliminating the data rows from the logic where the "start date" was greater than the "end date". I simply added another IF statement to put a 0 in that row if that condition is true.

 

Here is the new formula, hopefully it will help other people with the same issue:

NewColumn = IF('Table'[StartDate] <= IF(ISBLANK('Table'[EndDate]),TODAY(),'Table'[EndDate]) , DATEDIFF('Table'[StartDate], if(ISBLANK('Table'[EndDate]),TODAY(),'Table'[EndDate]),DAY), 0)

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.

Top Solution Authors
Top Kudoed Authors