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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
Fabcon_Europe_Social_Bogo

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.