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
GJUDGE
Helper II
Helper II

Help with DateDiff

Probably a straightforward one for most, but how to do I write a measure to caluclate the movement in days of each construction project from one month to the next? So for example, the movement in construction date for project A would show 30 days?

 

It would be easier for me to have separate measures for changes in construction start date and commissioning date. 

 

Also is there any way to make this dynamic by referring to the max reporting period and one less than this as opposed to using Sept-21 and Aug-21 in the measure?

 

Thanks,

 

ProjectOutputConstruction Start DateCommissioning DateReporting Period
A5001/01/202301/06/2023Aug-21
B2501/04/202307/04/2023Aug-21
C5001/05/202301/07/2023Aug-21
A5001/02/202301/06/2023Sep-21
B2501/04/202307/04/2023Sep-21
C5001/05/202301/06/2023Sep-21

 

 

10 REPLIES 10
v-eqin-msft
Community Support
Community Support

Hi @GJUDGE ,

 

 the COD date hasn't changed in the most recent two months

The latest date is 2025-Feb, so if there do not exist COD= 2025-Jan and 2024-Dec, then datediff should be 0 ?

 

Measure 2 = 
var _last=CALCULATE(MAX('Table'[Construction Start Date]),ALLEXCEPT('Table','Table'[Project]))
var _pre= MAXX(FILTER(ALL('Table'),'Table'[Project]=MAX('Table'[Project]) && [Construction Start Date]<_last),[Construction Start Date])
return IF(DATEDIFF(_pre,_last,MONTH)<=2,DATEDIFF( _pre,_last,DAY),0)

Eyelyn9_0-1635497772412.png

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

@v-eqin-msft Yes, the COD date hasn't changed, but even if this is the case I need the measure to only compare the COD dates of the last two months. Even is this is zero. For example, in project B in the test data I provided, I should still see a return of zero. Basically what I want to do is COD Date (Filltered reporting period for the latest month) minus COD Date (filtered for reporting period last month minus 1 month).

v-eqin-msft
Community Support
Community Support

Hi @GJUDGE ,

 

I have added a row to simply reproduce data as you said your actual data contains many months data.

My first measure returns the date diff for each changed .

Eyelyn9_0-1635494252220.png

Below will return the diff between the latest two date:

 

Measure 2 = 
var _last=CALCULATE(MAX('Table'[Construction Start Date]),ALLEXCEPT('Table','Table'[Project]))
var _pre= MAXX(FILTER(ALL('Table'),'Table'[Project]=MAX('Table'[Project]) && [Construction Start Date]<_last),[Construction Start Date])
return DATEDIFF(_pre,_last,DAY)

 

Eyelyn9_1-1635494565300.png

 

 

If it is still not your expected,please show me what you want .

 

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

@v-eqin-msft I really appreciate your help. When I try your latest measure on my actual data I still get the incorrect result.  Below is project A. COD date is the same as construction start date. When I use either of your two measures, I get a difference of 153 days when this should be zero (i.e. the COD date hasn't changed in the most recent two months). I hope that makes sense!

GJUDGE_0-1635496568300.png

 

v-eqin-msft
Community Support
Community Support

Hi @GJUDGE ,

 

Please try:

Measure = 
var _pre=MAXX(FILTER(ALL('Table'),[Project]=MAX('Table'[Project]) && [Construction Start Date]<MAX('Table'[Construction Start Date])),[Construction Start Date])
return DATEDIFF(_pre,MAX('Table'[Construction Start Date]),DAY)

Eyelyn9_0-1635490601984.png

 


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

@v-eqin-msft This almost gets me there! The only other complexity is that my actual data contains many months data. Using your measure it appears to be comparing the last and first dates rather than the last two months. is there any way to modify your measure?

lbendlin
Super User
Super User

please explain how you get to the 120.

@lbendlin In August, project A was planned to start construction on 01/01/2023. In September this date had slipped to 01/05/2023. I.e. it had moved back 120 days. 

Your sample data says that project A slipped to 2023-02-01.

 

 

@lbendlin You are completely right, I was misreading my own rows! So looking for a measure to calculate the 30 days difference, if you can assist?

 

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.