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

Find the date difference for a single column

Hi,

 

I have a Date field in a table.

date screenshot.PNG

 

 

 

 

 

 

My goal is to create a new column that calculates the date difference from the previous date in # of days.

For example:

• 1/17/20 and 1/28/20 the difference is 11 days.

• 1/28/20 and 1/30/20 the difference is 2 days.

• 1/30/20 and 2/3/20 the difference is 4 days.

• etc...

 

Also, this column needs to be dynamic meaning if a value is selected in a slicer, then this new column should reflect the new calculation difference.

 

Let me know if you have any questions and thanks in advance!

1 ACCEPTED SOLUTION

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

10 REPLIES 10
Icey
Community Support
Community Support

Hi @data23f ,

 

A calculated column can't change automatically, but a measure can. So, you need to create a measure, not a column. Please check whether @Ashish_Mathur 's measure works.

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@data23f 

Try a new column like

Avg  = datediff(maxx(filter(table,table[date] <earlier(table[date])),table[date]),table[date],day)

 

you may have add or subtract 1.

Thanks @Amit for your response.

 

I did try your DAX as a new column and changed data type to date.dax.PNG

 

and when inserted into the table I got the following:

table.PNG

 

All the values are the same. Any ideas how to fix this?

Thank you.

Can someone please assist?


I have tried searching on the Community, on YouTube, and online, but I can't get this to work.


Probably 20 different formulas I've used and I still can't get this date difference to work. I've tried creating a separate column and then doing a date difference, but still nothing.

Can you clarify this please "Also, this column needs to be dynamic meaning if a value is selected in a slicer, then this new column should reflect the new calculation difference."

 

What is in the slicer?

 

If you post some sample data (not a picture) and desired outcome ,someone will help you out.

I'm trying to attach a sample power bi .pbix file but I don't see the option to.

 

How do I attach a file?

Dropbox or similar and post the link

@HotChilli  I don't want to share a folder which perhaps can have confidential information. So instead, I'm sticking with the screenshot. The data is very simple.

This is done in Excel and in the Output field it's calculating the # of days difference from the previous date.

 

screenshot4.PNG

 

The slicer just had a way of segmenting the data from Country, Region. But I should be able to incorporate this once the new column is calculated.

Hope this helps.

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

A column for the simple example

ColumnA = VAR _date = TableCats[Date]
VAR _prevDate = CALCULATE(MAX(TableCats[Date]), FILTER(TableCats, TableCats[Date] < _date))
RETURN
   DATEDIFF(_prevDate, _date ,  DAY)

 

For future posts, please post data directly (you can paste it into the post) or anonymise the data and link a pbix

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.