Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Calculate time difference to nearest date by ID

 

hey peeps, 

 

How do I calculate the time duration by the same ID over difference dates? 

 

Transaction DateDate & TimeIDIn 1 / Out 0
9/5/20189/5/2018 5:18:20 PM229851
6/2/20176/2/2017 12:00:00 PM15721
7/2/20177/2/2017 4:00:00 PM15720
8/2/20178/2/2017 10:00:00 AM15721
11/2/201711/2/2017 12:00:00 PM15720

 

Expected Output as below

 

Example

1572 clocks in on 6/2 12pm, and clocks out on 7/2 4pm, = 24 + 4 = 28 hours 

1572 clocks in on 8/2 10am, and clocks out on 11/2 12pm, = 24 + 24 +24 + 4 = 78 hours

Total for 1572 = 28 + 78 = 106 hours

 

22985 clocks in once on 9/5, but there's no nearest clock out, hence = 0. 

 

FINIn CountOut CountTotal Duration
15722228 + 74 = 102 hours
22985100 cause no nearest out
  • Hi Anonymous,

     

    First new a calculated column:

    Duration =
    DATEDIFF (
        IF (
            Test2[In 1 / Out 0] = 0,
            CALCULATE (
                MAX ( Test2[Date & Time] ),
                FILTER (
                    ALLEXCEPT ( Test2, Test2[ID] ),
                    Test2[Date & Time] < EARLIER ( Test2[Date & Time] )
                        && Test2[In 1 / Out 0] = 1
                )
            )
        ),
        Test2[Date & Time],
        HOUR
    )

     

    Then, create below measures:

    In Count = CALCULATE(COUNT(Test2[In 1 / Out 0]),FILTER(Test2,Test2[In 1 / Out 0]=1))+0
    
    Out Count = CALCULATE(COUNT(Test2[In 1 / Out 0]),FILTER(Test2,Test2[In 1 / Out 0]=0))+0
    
    Total Duration = SUM(Test2[Duration])+0

    Use a Table visual o display data.

     

    Best regards,

    Yuliana Gu

2 Replies

  • v-yulgu-msft's avatar
    v-yulgu-msft
    Icon for Microsoft Employee rankMicrosoft Employee

    Hi Anonymous,

     

    First new a calculated column:

    Duration =
    DATEDIFF (
        IF (
            Test2[In 1 / Out 0] = 0,
            CALCULATE (
                MAX ( Test2[Date & Time] ),
                FILTER (
                    ALLEXCEPT ( Test2, Test2[ID] ),
                    Test2[Date & Time] < EARLIER ( Test2[Date & Time] )
                        && Test2[In 1 / Out 0] = 1
                )
            )
        ),
        Test2[Date & Time],
        HOUR
    )

     

    Then, create below measures:

    In Count = CALCULATE(COUNT(Test2[In 1 / Out 0]),FILTER(Test2,Test2[In 1 / Out 0]=1))+0
    
    Out Count = CALCULATE(COUNT(Test2[In 1 / Out 0]),FILTER(Test2,Test2[In 1 / Out 0]=0))+0
    
    Total Duration = SUM(Test2[Duration])+0

    Use a Table visual o display data.

     

    Best regards,

    Yuliana Gu

    • Anonymous's avatar
      Anonymous
      Not applicable

      Brilliant!!!