You can use either Power Query or DAX to create an automatically rolling calendar table depending on the latest date in your fact table. Here's how you can accomplish it:
1. Using Power Query (M Code)
In this method, the calendar table is extended dynamically depending on the maximum date available in your fact table.
let
Source = FactTable,
MinDate = Date.From(List.Min(Source[DateColumn])),
MaxDate = Date.From(List.Max(Source[DateColumn])),
DateRange = List.Dates(MinDate, Number.From(MaxDate - MinDate) + 1, #duration(1,0,0,0)),
CalendarTable = Table.FromList(DateRange, Splitter.SplitByNothing(), {"Date"}),
FinalTable = Table.AddColumn(CalendarTable, "Year", each Date.Year([Date]), Int64.Type)
in
FinalTable
How It Works:
- It extracts the minimum and maximum data from your fact table.
- It generates a continuous list of dates.
- Convert the list into a structured table.
- Adds a Year column (you can add more like Month, Quarter, etc.)
- This way, a calendar that updates automatically with the new data load is ensured.
2. DAX Approach (Calculated Table)
If you prefer DAX solutions, use this formula for creating a dynamic calendar table:
CalendarTable =
VAR MinDate = MIN(FactTable[DateColumn])
VAR MaxDate = MAX(FactTable[DateColumn])
RETURN
ADDCOLUMNS(
CALENDAR(MinDate, MaxDate),
"Year", YEAR([Date]),
"Month", FORMAT([Date], "MMM"),
"Quarter", "Q" & FORMAT([Date], "Q")
)
How Is It Functions:
MIN(FactTable[DateColumn]) for the beginning date and MAX(FactTable[DateColumn]) for the end date to get the date range for that fact table. Create a full continuous date table using CALENDAR(). Create extra columns dynamically for Year, Month, and Quarter.