Forum Discussion

jaryszek's avatar
jaryszek
Icon for Super User rankSuper User
2 years ago
Solved

How to use List.Generate to find continous ranges?

Hi Guys,   my input data: NameVacation Date Smith 09.03.2023 00:00:00 Smith 06.08.2023 00:00:00 Smith 07.08.2023 00:00:00 Smith 08.08.2023 00:00:00 Smith 09.08.2023 00:00:00 ...
  • AlienSx's avatar
    2 years ago

    Hello, jaryszek you can do everything in 1 step using Table.Group. I would not bother with List.Generate. Here is basic idea: sort by names and dates, then group as long as names are equal and dates are back to back. The latter is a little bit tricky but doable: add index column and compare duration in days between first and current row and difference in values of index column. Key is GroupKind.Local argument of Table.Group. Sorting order is also super important. I grouped rows for you that each row is a single vacation event. Do the rest yourself (add end date as max date, working days calculation etc as you usually do in Table.Group). Hope this was helpful.

    let
        sort = Table.Sort(#"Table1 (3)",{{"Name", Order.Ascending}, {"Vacation Date", Order.Ascending}}),
        idx = Table.AddIndexColumn(sort, "Index", 1, 1, Int64.Type),
        g = Table.Group(
            idx, {"Name", "Vacation Date", "Index"}, 
            {"all", each _}, GroupKind.Local,
            (s, c) => Byte.From(
                (s[Name] <> c[Name]) or 
                (Duration.Days(c[Vacation Date] - s[Vacation Date]) <> (c[Index] - s[Index]))
            )
        )
    in
        g