Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
dcrow5378
Helper III
Helper III

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: 

Shift Break Points.png

The desired output would be as follows:

TimeCard.png

 

Any ideas on this one?

5 REPLIES 5
v-yangliu-msft
Community Support
Community Support

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:

vyangliumsft_0-1712132429830.png

 

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

 

@v-yangliu-msft 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.

Thanks @v-yangliu-msft!  I will give this a shot today.  Appreciate your time here.

ToddChitt
Super User
Super User

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?




Did I answer your question? If so, mark my post as a solution. Also consider helping someone else in the forums!

Proud to be a Super User!





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

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.