let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"position", type text}, {"date", type datetime}, {"shift start", type time}, {"shift end", type time}}),
OverNight = Table.AddColumn(#"Changed Type", "OverNightShift", each [shift end]<[shift start]),
GetHrs = Table.AddColumn(OverNight, "Hours Worked", each let
Hrs1 = if [OverNightShift] = false then {{Time.Hour([shift start]) .. Time.Hour([shift end])}, {}} else
{ {Time.Hour([shift start]) .. 23}
, {0 .. Time.Hour([shift end]) } }
in
Hrs1),
HrsOnDate = Table.AddColumn(GetHrs, "HrsOnDate", each [Hours Worked]{0}),
HrsOnNextDate = Table.AddColumn(HrsOnDate, "HrsOnNextDate", each [Hours Worked]{1}),
AddNextDate = Table.AddColumn(HrsOnNextDate, "NextDate", each if [OverNightShift] = true then Date.AddDays([date],1) else null, Date.Type),
ExtractHrs1 = Table.TransformColumns(AddNextDate, {"HrsOnNextDate", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
ExtractHrs2 = Table.TransformColumns(ExtractHrs1, {"HrsOnDate", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
Remove1 = Table.RemoveColumns(ExtractHrs2,{"Hours Worked", "HrsOnDate", "date"}),
Day2Table = Table.Buffer(Table.RenameColumns(Remove1,{{"NextDate", "date"}, {"HrsOnNextDate", "HrsOnDate"}})),
Day1Table = Table.Buffer(Table.RemoveColumns(ExtractHrs2,{"Hours Worked", "HrsOnNextDate", "NextDate"})),
TableD1D2 = Table.Combine({Day1Table,Day2Table}),
#"Filtered Rows" = Table.SelectRows(TableD1D2, each ([date] <> null)),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Filtered Rows", {{"HrsOnDate", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "HrsOnDate"),
#"Grouped Rows" = Table.Group(#"Split Column by Delimiter", {"date"}, {{"AllDay", each _, type table [name=nullable text, position=nullable text, date=datetime, shift start=nullable time, shift end=nullable time, OverNightShift=logical, HrsOnDate=nullable text]}}),
GetUniqueHrsPerDay = Table.AddColumn(#"Grouped Rows", "Unique Hrs Worked", each List.Sort(List.Distinct([AllDay][HrsOnDate]))),
RemoveGroupBy = Table.RemoveColumns(GetUniqueHrsPerDay,{"AllDay"}),
#"Expanded Unique Hrs Worked" = Table.ExpandListColumn(RemoveGroupBy, "Unique Hrs Worked"),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Unique Hrs Worked",{{"Unique Hrs Worked", Int64.Type}, {"date", type date}})
in
#"Changed Type1"