Forum Discussion
Separate day shift from night shift by splitting into two rows
- 6 years ago
Please see this M code for an example of how to do this. Create a new blank query and, in the Advanced Editor, replace the text there with this:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLTNzTUNzIwMlAwtzIwACKFAF8UYUNDhHisTrSSEUTWCCJriqYJKmyJpCcWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, ShiftStart = _t, ShiftEnd = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"ShiftStart", type datetime}, {"ShiftEnd", type datetime}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "FirstShiftHours", each if Time.Hour([ShiftStart]) < 18 then if Time.Hour([ShiftEnd]) > 18 then Duration.TotalHours(#time(18,0,0) - DateTime.Time([ShiftStart])) else Duration.TotalHours(DateTime.Time([ShiftEnd]) - DateTime.Time([ShiftStart])) else null),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "SecondShiftHours", each if Time.Hour([ShiftEnd]) >= 18 then if Time.Hour([ShiftStart]) < 18 then Duration.TotalHours(DateTime.Time([ShiftEnd])-#time(18,0,0)) else Duration.TotalHours(DateTime.Time([ShiftEnd]) - DateTime.Time([ShiftStart])) else null),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Custom1", {"Index", "ShiftStart", "ShiftEnd"}, "Attribute", "Value"),
#"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Value", type number}})
in
#"Changed Type1"If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat