Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now
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?
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
@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.
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?
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".
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
93 | |
85 | |
83 | |
72 | |
49 |
User | Count |
---|---|
142 | |
139 | |
110 | |
69 | |
55 |