Forum Discussion

Ekaterina_'s avatar
Ekaterina_
Helper I
2 years ago
Solved

Dynamic week cycles

Hello everyone,    I need some help with the dates in my table which I want to group in 2-week-cycles. I have this table here in which I add new data every day. Date Application 25.03.2024 ...
  • dufoq3's avatar
    2 years ago

    Hi Ekaterina_, I'm not sure if this is what you want to achieve.

     

    You have some missing weeks so I groupped it by two with existing data.

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdC9CoAwDATgd8ksIXet1FVwlo5C6Tv4/pPiD1Sb9SM5jitFOKoFpTHKIPO+Q+pQBPCUbDXn7VKDp0xq7PVMSJ0itAma79joIKc29UVzMJyt0F1+Sul6IxxE9JDe+2dAXZylHkT6Yz0A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Application = _t]),
        ChangedType = Table.TransformColumnTypes(Source,{{"Date", type date}}, "sk-SK"),
        Ad_YearWeek = Table.AddColumn(ChangedType, "YearWeek", each Date.Year([Date]) * 100 + Date.WeekOfYear([Date]), Int64.Type),
        #"Grouped Rows" = Table.Group(Ad_YearWeek, {"YearWeek"}, {{"All", each _, type table}}),
        SortedRows = Table.Sort(#"Grouped Rows",{{"YearWeek", Order.Descending}}),
        RemovedOtherColumns = Table.SelectColumns(SortedRows,{"All"}),
        Transformed = Table.FromList(List.Transform(List.Split(List.Transform(RemovedOtherColumns[All], Table.ToRecords), 2), each Table.FromRecords(List.Combine(_))), Splitter.SplitByNothing(), type table[Combined=table], ExtraValues.Error),
        Ad_Cycle = Table.AddIndexColumn(Transformed, "Cycle", 1, 1, Int64.Type),
        ExpandedCombined = Table.ExpandTableColumn(Ad_Cycle, "Combined", {"Date", "Application", "YearWeek"}, {"Date", "Application", "YearWeek"})
    in
        ExpandedCombined