Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@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
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

Top Solution Authors
Top Kudoed Authors