Dynamic 13 Period Calendar

Hi @Melissa

I am trying to build on your wonderful extended date table and previous topics on 13 Period Calendar. My challenge is similar to it, and a fiscal year calendar but has the following attributes;

The year runs from April to March every year
The week starts from Saturday and ends on a Friday

The calendar organizes the year into periods of 4 weeks, making up 13 periods in a year. Hence 52 weeks in total. Each of these periods is numbered from 1 to 13.

I have attached an excel file showing the columns, dates and also a pdf of the calendar i saw on the interenet.

Can you please help me build this code and also look at incoporating it into the extended date table ?

Thank you very much
Calendar23-25.xlsx (15.4 KB)
2023 - 2025 Rail Weeks Calendar.pdf (4.1 MB)

Hi @r-b-osei,

See how you get on with this query.

let
    fxFY = ( years as list ) as table => [
        t = List.Transform( years, each [
            n = _ - List.First(years),
            FYs = List.Repeat( {_}, 52),
            FYID = List.Repeat( {n+1}, 52),
            Weeks = {1..52},
            FWID = List.Transform( Weeks, (x)=> (52 * n) +x ),
            FYPeriod = List.Transform({0..51}, (x)=> Number.IntegerDivide(x, 4) +1),
            FPID = List.Transform(FYPeriod, (x)=> (13 * n) +x ),
            StartDate = List.Transform( {0..51}, (x)=> Date.StartOfWeek( Date.AddDays( #date(_, 4, 1), x *7), Day.Saturday )),
            EndDate = List.Transform( {0..51}, (x)=> Date.EndOfWeek( Date.AddDays( #date(_, 4, 1), x *7), Day.Saturday )),
            Dates = List.Transform( StartDate, (x)=> List.Dates(x, 7, Duration.From(1))),
            t = Table.FromColumns(
                {
                    FYs, FYID, Dates, Weeks, FWID, StartDate, EndDate, FYPeriod, FPID
                },  type table [
                    FY=Int64.Type, FY ID=Int64.Type, Date={date}, Week=Int64.Type, FW ID=Int64.Type, StartDate=date, EndDate=date, FY Period=Int64.Type, FP ID=Int64.Type
                ]
            )
        ][t] ),
        r = Table.ExpandListColumn( Table.Combine(t), "Date")
    ][r],
    // Invoke custom function on a list of year numbers
    Source = fxFY( {2020..2025} ), 
    // Use UI to extend this custom date table with CY attributes
    InsertYear = Table.AddColumn(Source, "CY", each Date.Year([StartDate]), Int64.Type)
in
    InsertYear

Remarks:

  • The fxFY is a custom function that operates on a list of year numbers.
  • The Source step, illustrates how to invoke this custom function
  • You can use Add Column, From Date options to extend this date table further, as demonstrated by the InsertYear step of this query
  • In my view ID columns are just as powerful as Offsets. They easily convert to Offsets, if you require them. Follow the instructions illustrated here.

I hope this is helpful

4 Likes

Nice solution Melissa. I ran through through data mentor, to learn and review from the code

Output was informative.

1 Like

That is awesome!
What were key learnings for you @SamMcKay ?

I really like the logic visualizer

It’s helping me to understand code much better and much quicker also.

1 Like

Thank you very much Melissa. The query is gold and works like magic. Perfect!

3 Likes

The Data Mentor is a nice platform Sam. Also just used it to inteperate the query and gave me a good understanding of the code structure. Thank you

1 Like