Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Sum total time between timestamps from logging table

Hello

 

I have two tables below, one with employees and one table with transactions where they start and stop their clock. I need to sum the total of hours between the start and stop for each logging. I'm stuck and would appreciate your help 

 

There is no ID to the transaction, so I need to calculate the time between the first <Start> and the first <Stop>, then move to the second <start> against the second <stop>, and so on. There can be thousands of transactions in the real table

 

Employees  
IDEmployee Name
1Mike 
2Steve 
   
   
Timelog  
EmployeeIDActivityTimestamp
1Start2018-11-20 10:00
1Stop2018-11-20 11:00
1Start2018-11-20 14:00
1Stop2018-11-20 15:00
2Start2018-11-20 15:00
2Stop2018-11-20 16:00
   
   
End result  
EmployeeTotal time (hours)
Mike2 
Steve1 
  • Hi Anonymous,

     

    The solution of PattemManohar should be useful.

     

    By  my tests, you could create a calculated column with the formula below then you could get your desired output.

     

    Hour =
    VAR a =
        CALCULATE (
            MAX ( 'Timelog'[Timestamp] ),
            FILTER (
                ALL ( 'Timelog' ),
                'Timelog'[Timestamp] < EARLIER ( 'Timelog'[Timestamp] )
                    && 'Timelog'[EmployeeID] = EARLIER ( 'Timelog'[EmployeeID] )
                    && 'Timelog'[Activity] <> EARLIER ( 'Timelog'[Activity] )
                    && 'Timelog'[Activity] = "Start"
            )
        )
    RETURN
        DATEDIFF ( a, 'Timelog'[Timestamp], HOUR )
    

    Here is the output.

     

     

    Best  Regards,

    Cherry

3 Replies

  • v-piga-msft's avatar
    v-piga-msft
    Icon for Resident Rockstar rankResident Rockstar

    Hi Anonymous,

     

    The solution of PattemManohar should be useful.

     

    By  my tests, you could create a calculated column with the formula below then you could get your desired output.

     

    Hour =
    VAR a =
        CALCULATE (
            MAX ( 'Timelog'[Timestamp] ),
            FILTER (
                ALL ( 'Timelog' ),
                'Timelog'[Timestamp] < EARLIER ( 'Timelog'[Timestamp] )
                    && 'Timelog'[EmployeeID] = EARLIER ( 'Timelog'[EmployeeID] )
                    && 'Timelog'[Activity] <> EARLIER ( 'Timelog'[Activity] )
                    && 'Timelog'[Activity] = "Start"
            )
        )
    RETURN
        DATEDIFF ( a, 'Timelog'[Timestamp], HOUR )
    

    Here is the output.

     

     

    Best  Regards,

    Cherry

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks both of you! Got me started