Forum Discussion
How to detect consecutive groups of records within date range?
- Anonymous5 years ago
Finally, we have an example that I can use to show us the wonderful functionality of the GroupKind.Local parameter of the Table.Group function. Your dates are already sorted by date within the name--perfect. We need to mark the instances of absences--add a column that checks for the values you specified, including "VAC", if true, True, else False, and let's call it AbsenceCodes. Add another column that marks the row as one of the codes specified besides "VAC", if true, True, else False, and let's call it NonVacCode. Now, add the Group command, grouping our new AbsenceCode column. Add Max aggregations for your NonVacCode, and both Min and Max for A/AType Start Date, and then add and All Rows Aggregation named Details.
Now if you were doing this via the GUI, you'd get two rows--one for True and One for False. But if we go back in right before the final parentheses, add a comma, and then GroupKind.Local, then its a whole different ballgame. In fact, GroupKind.Local groups values only as long as they are contiguous. As soon as the value changes, new group. Now you can filter on Count > 12, and both the AbsenceCode and NonVacCode = True. Check out the code below, just copy and paste into the advanced editor. Let me know how you like it.
let
Source = Excel.Workbook(File.Contents("C:\Users\xx\xx\Suncor Base Plant – Sample Data - Power BI Help Request.xlsx"), null, true),
Table_1_Table = Source{[Item="Table_1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table_1_Table,{{"Org. Text", type text}, {"WS rule", type text}, {"Name", type text}, {"A/AType", type text}, {"Pers.No.", Int64.Type}, {"Absence/Attendance hours", Int64.Type}, {"Planned working hours", Int64.Type}, {"Start Date A/A Type", type date}, {"Created on", type date}, {"Time", type time}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "AbsentCode", each List.Contains({"MUA", "LNP", "VAC", "DBH","TRV"}, [#"A/AType"]), type text),
Custom2 = Table.AddColumn(#"Added Custom", "AbsentNonVAC", each List.Contains({"MUA", "LNP", "DBH","TRV"}, [#"A/AType"]), type text),
#"Grouped Rows" = Table.Group(Custom2, {"AbsentCode"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"NonVacDaysPresent", each List.Max([AbsentNonVAC]), type text}, {"Start Date", each List.Min([#"Start Date A/A Type"]), type nullable date}, {"End Date", each List.Max([#"Start Date A/A Type"]), type nullable date}, {"Details", each _, type table [Org. Text=nullable text, WS rule=nullable text, #"Pers.No."=nullable number, Name=nullable text, #"Start Date A/A Type"=nullable date, #"A/AType"=nullable text, #"Absence/Attendance hours"=nullable number, Planned working hours=nullable number, Created on=nullable date, Time=nullable time, AbsentCOde=text, AbsentNonVAC=text]}}, GroupKind.Local),
#"Filtered Rows2" = Table.SelectRows(#"Grouped Rows", each [Count] > 12),
#"Filtered Rows1" = Table.SelectRows(#"Filtered Rows2", each ([AbsentCode] = true) and ([NonVacDaysPresent] = true))
in
#"Filtered Rows1"--Nate
Finally, we have an example that I can use to show us the wonderful functionality of the GroupKind.Local parameter of the Table.Group function. Your dates are already sorted by date within the name--perfect. We need to mark the instances of absences--add a column that checks for the values you specified, including "VAC", if true, True, else False, and let's call it AbsenceCodes. Add another column that marks the row as one of the codes specified besides "VAC", if true, True, else False, and let's call it NonVacCode. Now, add the Group command, grouping our new AbsenceCode column. Add Max aggregations for your NonVacCode, and both Min and Max for A/AType Start Date, and then add and All Rows Aggregation named Details.
Now if you were doing this via the GUI, you'd get two rows--one for True and One for False. But if we go back in right before the final parentheses, add a comma, and then GroupKind.Local, then its a whole different ballgame. In fact, GroupKind.Local groups values only as long as they are contiguous. As soon as the value changes, new group. Now you can filter on Count > 12, and both the AbsenceCode and NonVacCode = True. Check out the code below, just copy and paste into the advanced editor. Let me know how you like it.
let
Source = Excel.Workbook(File.Contents("C:\Users\xx\xx\Suncor Base Plant – Sample Data - Power BI Help Request.xlsx"), null, true),
Table_1_Table = Source{[Item="Table_1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table_1_Table,{{"Org. Text", type text}, {"WS rule", type text}, {"Name", type text}, {"A/AType", type text}, {"Pers.No.", Int64.Type}, {"Absence/Attendance hours", Int64.Type}, {"Planned working hours", Int64.Type}, {"Start Date A/A Type", type date}, {"Created on", type date}, {"Time", type time}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "AbsentCode", each List.Contains({"MUA", "LNP", "VAC", "DBH","TRV"}, [#"A/AType"]), type text),
Custom2 = Table.AddColumn(#"Added Custom", "AbsentNonVAC", each List.Contains({"MUA", "LNP", "DBH","TRV"}, [#"A/AType"]), type text),
#"Grouped Rows" = Table.Group(Custom2, {"AbsentCode"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"NonVacDaysPresent", each List.Max([AbsentNonVAC]), type text}, {"Start Date", each List.Min([#"Start Date A/A Type"]), type nullable date}, {"End Date", each List.Max([#"Start Date A/A Type"]), type nullable date}, {"Details", each _, type table [Org. Text=nullable text, WS rule=nullable text, #"Pers.No."=nullable number, Name=nullable text, #"Start Date A/A Type"=nullable date, #"A/AType"=nullable text, #"Absence/Attendance hours"=nullable number, Planned working hours=nullable number, Created on=nullable date, Time=nullable time, AbsentCOde=text, AbsentNonVAC=text]}}, GroupKind.Local),
#"Filtered Rows2" = Table.SelectRows(#"Grouped Rows", each [Count] > 12),
#"Filtered Rows1" = Table.SelectRows(#"Filtered Rows2", each ([AbsentCode] = true) and ([NonVacDaysPresent] = true))
in
#"Filtered Rows1"
--Nate