Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Cumulative time difference summarized by person and date

Good morning,

I have a dataset of employees the clock in and out every day.

I need to summarise the data so that I can calculate how many hours each of them worked each day.

A sample of the data looks like this

I need a new table that is like this

An additional issue here is the the clocking in not showed in chronological order and need to be ordered before calculating the time differences.

 

Many thanks for your help.

  • Anonymous Well, can we make the assumption that there are always 4 clock-in and outs per day? Or, is there a flag that says whether it was a clock in or a clock out? Another option would be to only calculate the difference if it is the max time and the minimum time that is greater than the absolute minimum time for the day (tricky but doable). Let me know if any of those seem feasible.

16 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    Anonymous Well, seems like you could just subtract the clock out time from the clock in time but impossible to tell with everything blacked out what the data looks like. 


    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/339586.
    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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you for your quick reply.

      I think those measure are not very helpful in this instance as I need brand new summarized table that will be used to perform other calculations later on. ðŸ˜”

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Greg,

      unfortunately the clocking direction can't be used in this instance as this field is not constantly used, therefore, is unreliable. I had to redact the picture as there are informations not useful or can't be published.

      The only field I can use are those three and the only way to correctly subtract the In/Out time is to order it in ascending way. But it need to be done only after filtering for each employee and each day.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Greg_Deckler 

      this is what I get when I apply your code.

      Is there a way to order the Time and then calculate the differences?

      I have addedd also the other variables results (Previous, PreviousDate and Current) to show what I get from those Variables.

       

      • Greg_Deckler's avatar
        Greg_Deckler
        Icon for Community Champion rankCommunity Champion

        Anonymous Give me the first 8 rows as text, just the employ column, date column and time column. Just use the table tool to post the data. It will take me three times as long to type it out versus writing the DAX.

  • tamerj1's avatar
    tamerj1
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous 
    Please try

    Hours worked =
    MAX ( 'Table'[Time] ) - MIN ( 'Table'[Time] )

    Or

     

    Hours worked =
    VAR TotalMinutes =
        SUMX ( 'Table', DATEDIFF ( 'Table'[ClockIn], 'Table'[ClockOut], MINUTE ) )
    VAR Minutes =
        FORMAT ( MOD ( TotalMinutes, 60 ), "00" )
    VAR Hours =
        FORMAT ( QUOTIENT ( TotalMinutes, 60 ), "00" )
    RETURN
        Hours & ":" & Minutes