cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Fair-UL
Helper II
Helper II

Calculate Time between two dates on different rows

My issue is a bit tricky. I have this mock dataset with employees, levels, training group, and dates of start of level. not all employees are L2 in all training groups. I want to be able to calculate the time between L1 and L2 for every training group. if there is no L2 date, then I don't care about that entry. the results I want are in the second table (time in months)

employeeleveltraining groupL1 start dateL2 start date
1L1A1/1/2020 
1L1B2/1/2020 
1L1C3/1/2020 
1L2B 10/1/2021
1L1D1/1/2021 
2L1A3/1/2020 
2L1F2/1/2020 
2L1N5/1/2020 
2L2A 3/1/2021
2L1H5/1/2019 
2L1I11/1/2018 
2L2H 7/1/2021
3L1B2/1/2019 
4L1B5/1/2020 

 

the results needed are below

employeetraining groupTime between L1 and L2
1B20 months
2A12 months
2H26 months

 

this is a mock dataset but my real issue is that dates are on two separate rows. any help is appreciated. Thanks

4 REPLIES 4
v-henryk-mstf
Community Support
Community Support

Hi @Fair-UL ,

 

According to your description, I did a test reference as follows:

 

col_l2 =
CALCULATE (
    MIN ( 'Table'[L1 start date] ),
    FILTER (
        ALL ( 'Table' ),
        'Table'[training group] = EARLIER ( 'Table'[training group] )
    )
)
Col = 
var a = MONTH('Table'[L2 start date])-MONTH('Table'[col_l2])
var b = YEAR('Table'[L2 start date])-YEAR('Table'[col_l2])
return
IF('Table'[L2 start date]<>0,IF(a>=0, b*12+a,b*12-a))

vhenrykmstf_0-1635303939591.png

vhenrykmstf_1-1635304065948.png

If the problem is still not resolved, please provide detailed error information and let me know immediately. Looking forward to your reply.


Best Regards,
Henry


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

Hi @v-henryk-mstf 

the issue is in column col_l2, I need to calculate the time between L1 and L2 start dates for any employee who has L2 in a training group. So, for employee1, I need to find the time between  2/1/2020 and 10/1/2021 [20 months] . any other L1 start date can be ignored for any training groups for that employee unless he has another L2 start date for another training group

 

FairUL_0-1635524107741.png

 

Greg_Deckler
Super User
Super User

@Fair-UL See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous


@ me in replies or I'll lose your thread!!!
Check out my External Tool for Power BI Desktop! Microsoft Hates Greg's Quick Measures
YouTube Channel! Microsoft Hates Greg
Check out my latest book!

@Greg_Deckler 

I am not sure I was able to follow this logic. it did not give me the result I was looking for

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

November Power BI Update 768x460.png

Check it Out!

Click here to read more about the November 2021 Updates!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Kudoed Authors