Forum Discussion
webbta
1 year agoRegular Visitor
Requesting help to group events by time
I have about two years worth of data with several thousand IDs. I filtered this example to just show one ID but there are many 1,000. I want to group events on this table by two-hour intervals by ID....
Anonymous
1 year agoNot applicable
Hi webbta ,
I create sample data myself, including different IDs and different days:
Then you can put the whole M code below into the Advanced Editor:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddBLDsMgDATQq0SsI8U/HJhduq/UfZT7X6OkpUgNYf3wePC+B+aFeBESm2iFCKJP2zPMYQvH/M8JlMFpxA4zKE+ve15hjtixVM7nbqNreGVmUAR31X5caivUB+En5/JitJsRS/P05UfHXj59VhuyKshHnMDxZlobR4LUo/ZsMAXn0bR/wu3K1ljWkt/Cjzc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, ID = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"ID", type text}}),
FillEarliestTime = (table as table) as table =>
let
ListOfRecords = Table.ToRecords(table),
init = {List.First(ListOfRecords)[Date]},
accFunc = (acc as list, curr as record) =>
let
lastTime = List.Last(acc),
duration = Duration.TotalMinutes(curr[Date] - lastTime)
in
if duration <= 120 then
acc & {lastTime}
else
acc & {curr[Date]},
ListOfEarliestTimes = List.Accumulate(List.Skip(ListOfRecords, 1), init, accFunc),
FilledTable = Table.FromColumns(Table.ToColumns(table) & {ListOfEarliestTimes}, Table.ColumnNames(table) & {"Earliest Time"})
in
FilledTable,
#"Grouped Table" = Table.Group(#"Changed Type", {"ID"}, {{"GroupedTable", each FillEarliestTime(_), type table [Date=nullable datetime, ID=nullable text, Earliest Time=nullable datetime]}}),
#"Expanded GroupedTable" = Table.ExpandTableColumn(#"Grouped Table", "GroupedTable", {"Date", "Earliest Time"}, {"Date", "Earliest Time"})
in
#"Expanded GroupedTable"
And the final output is as below:
Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
webbta
1 year agoRegular Visitor
Dino, Thank you tremendously! Your solution worked for me and I was able to incorporate it into my pbx.