Forum Discussion

dcrow5378's avatar
dcrow5378
Resolver I
2 years ago

Breaking Punch In and Punch Out DateTime columns into shift categories

I have a dataset that has Employee, Date, Punch In, and Punch Out (DateTime) fields.  I would like to break the punch in and punch out span into shift categories (Day, Evening, and Night Shift) based on number of hours in each shift. 

This is the shift break times: 

The desired output would be as follows:

 

Any ideas on this one?

5 Replies

  • Instead of a matrix result like you have, I suggest you shoot for a classification of each punch as follows:

    1) Isolate the TIME portion from the Date. 

    2) Create a calculated column that looks at TIME and write a nested IF statement in DAX to determine if it is DAY, EVENING or NIGHT. 

    Hope that helps.

    Question, how would you classify someone that punched IN at 1000 and OUT at 1800?

    • dcrow5378's avatar
      dcrow5378
      Resolver I

      In the case of the 1000 in and 1800 out, they would have 4 Hours in "Day" and 4 Hours in "Evening".

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi  dcrow5378 ,

     

    Thanks for the reply from ToddChitt , please allow me to provide another insight:  

     

    Here are the steps you can follow:

    1. Create calculated column.

    Day Hours =
    VAR _daystart =
        MINX (
            FILTER ( ALL ( 'shift break times' ), 'shift break times'[Shift] = "Day" ),
            [Start]
        )
    VAR _datend =
        MINX (
            FILTER ( ALL ( 'shift break times' ), 'shift break times'[Shift] = "Day" ),
            [End]
        )
    VAR _value =
        SWITCH (
            TRUE (),
            AND (
                [In] >= _daystart
                    && [In] < _datend,
                [Out] > _daystart
                    && [Out] <= _datend
            ), [Out] - [In],
            AND ( [In] >= _daystart && [In] < _datend, [Out] > _datend ), _datend - _daystart
        )
    RETURN
        IF (
            _value = BLANK (),
            BLANK (),
            VALUE ( LEFT ( _value, LEN ( _value ) - 2 ) )
                + DIVIDE ( VALUE ( RIGHT ( _value, 2 ) ), 60 )
        )
    
    Evening Hours =
    VAR _eveningstart =
        MINX (
            FILTER ( ALL ( 'shift break times' ), 'shift break times'[Shift] = "Evening" ),
            [Start]
        )
    VAR _eveningend =
        MINX (
            FILTER ( ALL ( 'shift break times' ), 'shift break times'[Shift] = "Evening" ),
            [End]
        )
    VAR _daystartday =
        MINX (
            FILTER ( ALL ( 'shift break times' ), 'shift break times'[Shift] = "Day" ),
            [Start]
        )
    VAR _dayendday =
        MINX (
            FILTER ( ALL ( 'shift break times' ), 'shift break times'[Shift] = "Day" ),
            [End]
        )
    VAR _value =
        SWITCH (
            TRUE (),
            AND ( [In] >= _eveningstart && [In] < _eveningend, [Out] <= _eveningend ), [Out] - [In],
            AND ( [In] >= _eveningstart && [In] < _eveningend, [Out] > _eveningend ), _eveningend - [In],
            AND ( [In] < _eveningstart, [Out] <= _eveningend && [Out] > _dayendday ), [Out] - _eveningstart
        )
    RETURN
        IF (
            _value = BLANK (),
            BLANK (),
            VALUE ( LEFT ( _value, LEN ( _value ) - 2 ) )
                + DIVIDE ( VALUE ( RIGHT ( _value, 2 ) ), 60 )
        )
    
    Night Hours =
    VAR _nightstart =
        MINX (
            FILTER ( ALL ( 'shift break times' ), 'shift break times'[Shift] = "Night" ),
            [Start]
        )
    VAR _nightend =
        MINX (
            FILTER ( ALL ( 'shift break times' ), 'shift break times'[Shift] = "Night" ),
            [End]
        )
    VAR _eveningstart =
        MINX (
            FILTER ( ALL ( 'shift break times' ), 'shift break times'[Shift] = "Evening" ),
            [Start]
        )
    VAR _eveningend =
        MINX (
            FILTER ( ALL ( 'shift break times' ), 'shift break times'[Shift] = "Evening" ),
            [End]
        )
    VAR _value =
        SWITCH (
            TRUE (),
            [In] >= _nightstart
                && [Out] <= _nightend,
                2400 - [In] + [Out],
            AND (
                [In] < _nightstart
                    && [In] >= _eveningstart,
                [Out] >= _nightend
                    && [Out] > _eveningend
            ), [Out] - _nightstart
        )
    RETURN
        IF (
            _value = BLANK (),
            BLANK (),
            VALUE ( LEFT ( _value, LEN ( _value ) - 2 ) )
                + DIVIDE ( VALUE ( RIGHT ( _value, 2 ) ), 60 )
        )
    

    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

     

    • dcrow5378's avatar
      dcrow5378
      Resolver I

      Thanks Anonymous!  I will give this a shot today.  Appreciate your time here.

    • dcrow5378's avatar
      dcrow5378
      Resolver I

      Anonymous I think this will work, I am just having an issue with transforming the time section into 24 hour text as in your example.  I should have mentioned that the punch in/out times were real date time not categories, not broken apart, as I have them in the example.