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
Malcolm
Regular Visitor

Time Difference between lines

Dear Community, good day!

 

I need to verify the date difference between two time stamps which are not in the same line?!?

The data model has a booking reference as identifier, from / to locations with its dates and a sequence. 

This theoretical model has a booking eg 1234 which has 3 train connections. From A to B, from B to C, from C to D

 

I need to check that the between arrival B to departure C is no longer then 2 days and of course show if that kind of delay is the case. 

 

Preferably DAX but also Power Query would be great. Basically appreciate any approach that can solve it

 

Expected result would be :

Booking = 1234 : 

Between Arrival  in Transit Station A (Sequence 1) and Departure from Transit Station A (Sequence 2) no delay that needs actions

Between Arrival in Transit Station B (Sequence 2) and Departure from Transit Station B (Sequence 3) delay exceeds 2 days and should be displayed

 

Booking = 2345 : 

No delays exceeding 1 day and nothing to report

 

Booking RefTrainDeparture StationDeparture DateArrival StationArrival DateSequence
1234TommyMain Station A16.01.2023 16:00Transit Station A16.01.2023 23:001
1234MartaTransit Station A17.01.2023 10:00Transit Station B17.01.2023 16:002
1234GilbertTransit Station B30.01.2023 12:00Destination30.01.2023 16:003
2345CarlaMain Station A23.01.2023 13:00TT Loc A23.01.2023 14:301
2345RobertoTT Loc A23.01.2023 16:30Destination23.01.2023 18:302

 

1 ACCEPTED SOLUTION

Hi @Malcolm ,

 

Please try this:

Column =
VAR _booking = 'Table'[Booking Ref]
VAR _arrival_date = 'Table'[Arrival Date]
VAR _departure_date =
    CALCULATE (
        MAX ( 'Table'[Departure Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Booking Ref] = _booking
                && 'Table'[Sequence]
                    = EARLIER ( 'Table'[Sequence] ) - 1
        )
    )
VAR _days =
    DATEDIFF ( _departure_date, _arrival_date, DAY )
VAR _result =
    IF ( _days >= 2, "Delay" )
RETURN
    _result

vcgaomsft_0-1673933034777.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

View solution in original post

4 REPLIES 4
Malcolm
Regular Visitor

@Greg_Deckler :

Thanks! I tried running but ran into Dax not beeing able use MAXX  for direct query. 

Will change to a data flow and test again. Just to be sure :

1 ) I coudnt find any reference to the sequence numbers. The previous date needs to be the date from the sequence before. There can be 5 or 6 sequences in row and needs to be always 3 -> looking at 2 etc

2) [Value] = Whats the reference ? In my example the booking number ? 

Hi @Malcolm ,

 

Please try this:

Column =
VAR _booking = 'Table'[Booking Ref]
VAR _arrival_date = 'Table'[Arrival Date]
VAR _departure_date =
    CALCULATE (
        MAX ( 'Table'[Departure Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Booking Ref] = _booking
                && 'Table'[Sequence]
                    = EARLIER ( 'Table'[Sequence] ) - 1
        )
    )
VAR _days =
    DATEDIFF ( _departure_date, _arrival_date, DAY )
VAR _result =
    IF ( _days >= 2, "Delay" )
RETURN
    _result

vcgaomsft_0-1673933034777.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

@v-cgao-msft : Thanks! Exactly what I needed. You nailed it!

Greg_Deckler
Super User
Super User

@Malcolm 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...

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.