Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Conditional column - rows condition for Time comparision

Hi team, I am a newbie and wants to compare  and match time for vehicle entry and exits to their latest ones to calculate the turn around time. Each vehicle has multiple entry and exit time stamp and I want to match the entry time with the earliest exit time and not the last exit time of that vehicle. Below is an example where the driver has two different entry time but with my current condition the first entry time matches with both greater than exit times. 

 

My "Validexit" column has the conditions if [Exit Date] > [Date] or ([Exit Date] = [Date] and [Exit Time] > [Time]) or [Exit Time] = null and [Driver] = [Exit Rec.Driver] or [Exit Time] = null then true else false

 

 

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Thanks for the reply from Ashish_Mathur and Greg_Deckler  , please allow me to provide another insight: 
    Hi  Anonymous ,

     

    Here are the steps you can follow:

    1. Create calculated column.

    Rank =
    RANKX(
        FILTER(ALL('Table'),
        'Table'[Driver]=EARLIER('Table'[Driver])),[Time],,ASC)
    Mod =
     MOD([Rank],2)
    Group =
    SUMX(
        FILTER(ALL('Table'),
        'Table'[Driver]=EARLIER('Table'[Driver])&&'Table'[Rank]<=EARLIER('Table'[Rank])),[Mod])
    Match =
    var _match_entrytime=
    MAXX(
        FILTER(ALL('Table'),  'Table'[Driver]=EARLIER('Table'[Driver])&&'Table'[Group]=EARLIER('Table'[Group])&&'Table'[Rank]=EARLIER('Table'[Rank])+1),[Time])
    return
    IF(
        _match_entrytime>=[Time] || _match_entrytime=BLANK(),TRUE(),FALSE())

    2. Result:

     

     

    Best Regards,

    Liu Yang

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

5 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    Anonymous Can you post your sample data as text? Also, I am not clear on your expected results.

     

    Sorry, having trouble following, can you post sample data as text and expected output?
    Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

    Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

    The most important parts are:
    1. Sample data as text, use the table tool in the editing bar
    2. Expected output from sample data
    3. Explanation in words of how to get from 1. to 2.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Greg_Deckler Sorry for the confusion and not being clear. Below is my Sample text data.  I have entry and exit for each truck with the time stamp. I  want to match the first entry with the first exit and then subsequent entry and exit for each vehicle. The source file is getting updated with time stamps and some of the trucks don't have the exit time stamps till it data source gets updated.

    TypeLocationTruckDriverTime
     ExitMoncton MOCKBS 1920-19  Gourav Singh8:24:30 AM
     EntryMoncton MOCKBS 1920-19  Gourav Singh9:34:17 AM
     ExitMoncton MOCKBS 1920-19  Gourav Singh10:42:56 AM
     EntryMoncton MOCKBS 1920-19  Gourav Singh11:56:50 AM
     ExitMoncton MOCKBS 1920-19  Gourav Singh12:14:55 PM
     ExitMoncton MOCKBS 2022-20  Renan Felippe7:17:14 AM
     ExitMoncton MOCKBS 2025-20  iurii savonik8:26:34 AM
     EntryMoncton MOCKBS 2025-20  iurii savonik9:55:28 AM
     ExitMoncton MOCKBS 2025-20  iurii savonik10:33:46 AM
     EntryMoncton MOCKBS 2025-20  iurii savonik11:42:20 AM
     ExitMoncton MOCKBS 2025-20  iurii savonik12:15:41 PM

     

    • Greg_Deckler's avatar
      Greg_Deckler
      Community Champion

      Anonymous Not sure if a DAX solution is acceptable. Regardless, are these the results you are expecting? PBIX is attached below signature.

      Valid Exit = 
          VAR __Type = [Type]
          VAR __Truck = [Truck]
          VAR __Location = [Location]
          VAR __Driver = [Driver]
          VAR __Time = [Time]
          VAR __Table = SELECTCOLUMNS( FILTER( 'Table', [Driver] = __Driver && [Location] = __Location && [Truck] = __Truck && [Type] = "Entry"  && [Time] < __Time ), "__Time", [Time] )
          VAR __PrevTime = MINX( __Table, [__Time] )
          VAR __Result = 
              SWITCH( TRUE(),
                  __Type = "Entry", FALSE(),
                  ISBLANK( __PrevTime ), FALSE(),
                  TRUE()
              )
      RETURN
          __Result
  • Anonymous's avatar
    Anonymous
    Not applicable

    Thanks for the reply from Ashish_Mathur and Greg_Deckler  , please allow me to provide another insight: 
    Hi  Anonymous ,

     

    Here are the steps you can follow:

    1. Create calculated column.

    Rank =
    RANKX(
        FILTER(ALL('Table'),
        'Table'[Driver]=EARLIER('Table'[Driver])),[Time],,ASC)
    Mod =
     MOD([Rank],2)
    Group =
    SUMX(
        FILTER(ALL('Table'),
        'Table'[Driver]=EARLIER('Table'[Driver])&&'Table'[Rank]<=EARLIER('Table'[Rank])),[Mod])
    Match =
    var _match_entrytime=
    MAXX(
        FILTER(ALL('Table'),  'Table'[Driver]=EARLIER('Table'[Driver])&&'Table'[Group]=EARLIER('Table'[Group])&&'Table'[Rank]=EARLIER('Table'[Rank])+1),[Time])
    return
    IF(
        _match_entrytime>=[Time] || _match_entrytime=BLANK(),TRUE(),FALSE())

    2. Result:

     

     

    Best Regards,

    Liu Yang

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