Forum Discussion
Best Practice for Creating Optimized Date Dimension Table (Including Fiscal Year: April–March) Post
- 1 year ago
Hi GanesaMoorthyGM
We can create using Power Query.Below M code
Dynamically generating Date Dimension table based on the min and max dates from Tablelet
// Reference your table
Source = Table, // Name of the table from where min and max date to be taken.// Get the minimum and maximum dates from your fact table's date column
MinDate = List.Min(Source[OrderDate]),
MaxDate = List.Max(Source[OrderDate]),// Generate date range list
DateList = List.Dates(MinDate, Duration.Days(MaxDate - MinDate) + 1, #duration(1, 0, 0, 0)),
DateTable = Table.FromList(DateList, Splitter.SplitByNothing(), {"Date"}),// Adding Columns
AddDay = Table.AddColumn(DateTable, "Day", each Date.Day([Date]), Int64.Type),
AddMonthNum = Table.AddColumn(AddDay, "Month Number", each Date.Month([Date]), Int64.Type),
AddMonthName = Table.AddColumn(AddMonthNum, "Month Name", each Date.ToText([Date], "MMMM"), type text),
AddMonthOrder = Table.AddColumn(AddMonthName, "Month Order", each Date.Month([Date]), Int64.Type),
AddQuarter = Table.AddColumn(AddMonthOrder, "Quarter", each Date.QuarterOfYear([Date]), Int64.Type),
AddQuarterName = Table.AddColumn(AddQuarter, "Quarter Name", each "Q" & Number.ToText(Date.QuarterOfYear([Date])), type text),
AddYear = Table.AddColumn(AddQuarterName, "Year", each Date.Year([Date]), Int64.Type),
AddWeekNum = Table.AddColumn(AddYear, "Week Number", each Date.WeekOfYear([Date]), Int64.Type),
AddDayOfWeek = Table.AddColumn(AddWeekNum, "Day of Week", each Date.DayOfWeek([Date]), Int64.Type),
AddWeekdayName = Table.AddColumn(AddDayOfWeek, "Weekday Name", each Date.ToText([Date], "dddd"), type text),// Fiscal Year (April to March)
AddFiscalYear = Table.AddColumn(AddWeekdayName, "Fiscal Year", each
let
year = Date.Year([Date]),
month = Date.Month([Date])
in
if month >= 4 then Text.From(year) & "-" & Text.End(Text.From(year + 1), 2)
else Text.From(year - 1) & "-" & Text.End(Text.From(year), 2),
type text),AddFiscalQuarter = Table.AddColumn(AddFiscalYear, "Fiscal Quarter", each
let
month = Date.Month([Date])
in
if month >= 4 and month <= 6 then "Q1"
else if month >= 7 and month <= 9 then "Q2"
else if month >= 10 and month <= 12 then "Q3"
else "Q4", type text)in
AddFiscalQuarterIf this answers your questions, kindly accept it as a solution and give kudos
Hi GanesaMoorthyGM,
Thanks for reaching out to the Microsoft fabric community forum.
It looks like you are building a Date Dimension table for a Power BI model and is looking for best practices and an optimized Power Query (M) script to generate it. As MasonMA and mdaatifraza5556 both responded to your query, kindly go through their responses and check if the issue can be resolved.
I would also take a moment to thank MasonMA and mdaatifraza5556, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.
If I misunderstand your needs or you still have problems on it, please feel free to let us know.
Best Regards,
Hammad.