Okay so you didn’t provide the accompanying data, I made an export for that…
Loaded that into a new Query called tCalendar and created a FileLocation Parameter for that so save the xlsx and copy the full filepath+filename into that parameter and you’re good to go.
Here’s what I dd to create the custom offsets for you:
-
CurrentDate is to determine Today
-
IdentifyCurrentDate filters down your query to the record matching the CurrentDate
-
CurrentPeriod extracts the value from that record for the Year-Period
-
BufferTable loaded a one column table with all distinct Year-Periods into memory
-
InsertPeriodOffset counted the number of rows in that BufferTable after filtering it down
.
let
Source = Excel.Workbook(File.Contents(FileLocation), null, true),
tCalendar_Table = Source{[Item="tCalendar",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(tCalendar_Table,{{"Date Period Key", type text}, {"Date", type date}, {"Period", Int64.Type}, {"FMnth", type text}, {"Year", Int64.Type}, {"P Begin Date", type date}, {"P End Date", type date}, {"Today", type date}, {"Day Offset", Int64.Type}, {"Year-Period", Int64.Type}, {"Year-Period Index", Int64.Type}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Today", "Day Offset"}),
// Added steps here
CurrentDate = DateTime.Date(DateTime.FixedLocalNow()),
IdentifyCurrentDate = Table.SelectRows(#"Removed Columns", each ([Date] = CurrentDate)),
CurrentPeriod = IdentifyCurrentDate{0}[#"Year-Period"],
BufferTable = Table.Buffer(Table.Distinct( #"Removed Columns"[[#"Year-Period"]])),
InsertPeriodOffset = Table.AddColumn(#"Removed Columns", "Period Offset", each
if _[#"Year-Period"] > CurrentPeriod = false then -Table.RowCount( Table.SelectRows( BufferTable, (IT) => IT[#"Year-Period"] > _[#"Year-Period"] and IT[#"Year-Period"] < CurrentPeriod))
else if _[#"Year-Period"] = CurrentPeriod then 0
else Table.RowCount( Table.SelectRows( BufferTable, (IT) => IT[#"Year-Period"] < _[#"Year-Period"] and IT[#"Year-Period"] > CurrentPeriod )), type number)
in
InsertPeriodOffset
.
With this result.
.
I hope this is helpful. Here are the files.
Period Offset Data.xlsx (50.9 KB)
Period Offset Model.pbix (50.4 KB)