Forum Discussion
SadStatue
5 years agoHelper II
Identifying Broken Shifts
Hi Everyone, I have a list of shifts and the Employees. I want to identify all the shifts that has less than 8 hours gap between them for each Employee. sample: I want to calculate the last two ...
- Anonymous5 years ago
Just put this into the Advanced Editor in Power Query and investigate the steps one by one... This does what you want. How to extract the relevant shift id's from it? That's what I leave to you as an exercise 🙂
// Shifts let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZBBCsQgDEWvIq4L/YlVa3ejxyi9/zVGKBRnkkUlu/DJ4yfvPD2DacW2cnLIR8AB+GVMKfboTglIyKFPH38tA5sd4dka0/DDUioqGzWW6bnm7t0ku6vs7pjHXhSKki0OSf7b0/LXq7Jab5E3J8lOea4Gz5J977kaPFeDZ8FOeK4Gz83gWbLvPTeD52bwLNgJz529vg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Start Time" = _t, #"End Time" = _t, ShiftID = _t, EmpID = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Time", type datetime}, {"End Time", type datetime}, {"ShiftID", Int64.Type}, {"EmpID", type text}}), #"Sorted Rows" = Table.Sort(#"Changed Type",{{"EmpID", Order.Ascending}, {"Start Time", Order.Ascending}}), #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type), #"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"Index", "Start Time", "End Time", "ShiftID", "EmpID"}), #"Added Custom" = Table.AddColumn(#"Reordered Columns", "PrevShiftEndTime", each List.First( Table.SelectRows( #"Reordered Columns", (inner) => inner[Index] + 1 = [Index] and inner[EmpID] = [EmpID] )[End Time], null ) ), #"Added Custom1" = Table.AddColumn(#"Added Custom", "Delta < 8 hrs", each [Start Time] - [PrevShiftEndTime] < #duration(0, 8, 0, 0) ), #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Shift Broken? (1)", each if not ( [#"Delta < 8 hrs"] is null ) and [#"Delta < 8 hrs"] then "Y" else null ), #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Shift Broken?", each let NextRowShiftBrokenStatus = List.First( Table.SelectRows( #"Added Custom2", (inner) => inner[Index] = [Index] + 1 and inner[EmpID] = [EmpID] )[#"Shift Broken? (1)"], null ), Output = if ( NextRowShiftBrokenStatus = "Y" or [#"Shift Broken? (1)"] = "Y" ) then "Y" else null in Output ), #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Index", "PrevShiftEndTime", "Delta < 8 hrs", "Shift Broken? (1)"}) in #"Removed Columns"
Anonymous
5 years agoNot applicable
Just put this into the Advanced Editor in Power Query and investigate the steps one by one... This does what you want. How to extract the relevant shift id's from it? That's what I leave to you as an exercise 🙂
// Shifts
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZBBCsQgDEWvIq4L/YlVa3ejxyi9/zVGKBRnkkUlu/DJ4yfvPD2DacW2cnLIR8AB+GVMKfboTglIyKFPH38tA5sd4dka0/DDUioqGzWW6bnm7t0ku6vs7pjHXhSKki0OSf7b0/LXq7Jab5E3J8lOea4Gz5J977kaPFeDZ8FOeK4Gz83gWbLvPTeD52bwLNgJz529vg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Start Time" = _t, #"End Time" = _t, ShiftID = _t, EmpID = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Time", type datetime}, {"End Time", type datetime}, {"ShiftID", Int64.Type}, {"EmpID", type text}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"EmpID", Order.Ascending}, {"Start Time", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
#"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"Index", "Start Time", "End Time", "ShiftID", "EmpID"}),
#"Added Custom" = Table.AddColumn(#"Reordered Columns", "PrevShiftEndTime",
each
List.First(
Table.SelectRows(
#"Reordered Columns",
(inner) => inner[Index] + 1 = [Index] and inner[EmpID] = [EmpID]
)[End Time],
null
)
),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Delta < 8 hrs",
each [Start Time] - [PrevShiftEndTime] < #duration(0, 8, 0, 0)
),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Shift Broken? (1)",
each if not ( [#"Delta < 8 hrs"] is null ) and [#"Delta < 8 hrs"] then "Y" else null
),
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "Shift Broken?",
each
let
NextRowShiftBrokenStatus = List.First(
Table.SelectRows(
#"Added Custom2",
(inner) => inner[Index] = [Index] + 1 and inner[EmpID] = [EmpID]
)[#"Shift Broken? (1)"],
null
),
Output =
if ( NextRowShiftBrokenStatus = "Y" or [#"Shift Broken? (1)"] = "Y" )
then "Y" else null
in
Output
),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Index", "PrevShiftEndTime", "Delta < 8 hrs", "Shift Broken? (1)"})
in
#"Removed Columns"
SadStatue
5 years agoHelper II
Hi Anonymous
thanks for your quick help!
This inner join worked well. I haven't used it in M.
It seems that works well.
Cheers,