Forum Discussion
M-code for date dimension table
- 1 year ago
Hi SK_KE , Could you try this please
let
StartDate = #date(2020, 1, 1), // Change this to your date
// Define the end date as today
EndDate = DateTime.LocalNow(),
// Calculate the number of days between start and end date
NumberOfDays = Duration.Days(Duration.From(EndDate - StartDate)),
DateList = List.Dates(StartDate, NumberOfDays + 1, #duration(1, 0, 0, 0)),
DateTable = Table.FromList(DateList, Splitter.SplitByNothing(), {"Date"}),// Add additional columns for year, month, day, etc.
ExpandedTable = Table.TransformColumns(DateTable, {{"Date", each DateTime.Date(_), type date}}),
DateWithColumns = Table.AddColumn(ExpandedTable, "Year", each Date.Year([Date]), Int64.Type),
DateWithMonth = Table.AddColumn(DateWithColumns, "Month", each Date.Month([Date]), Int64.Type),
DateWithDay = Table.AddColumn(DateWithMonth, "Day", each Date.Day([Date]), Int64.Type),
DateWithQuarter = Table.AddColumn(DateWithDay, "Quarter", each Date.QuarterOfYear([Date]), Int64.Type),
DateWithMonthName = Table.AddColumn(DateWithQuarter, "MonthName", each Date.MonthName([Date]), type text),
DateWithWeekDay = Table.AddColumn(DateWithMonthName, "DayName", each Date.DayOfWeekName([Date]), type text)
in
DateWithWeekDay
If this post helped please do give a kudos and accept this as a solutionThanks In Advance
Hi SK_KE
You may use the following code. Change the date_from from the date that you want to and it will show the incrementally add whenever you refresh the table.
let
date_from = #date(2025,1,1),
step_num = Duration.Days(Date.From(DateTime.LocalNow()) - date_from) + 1,
Source = Table.FromList(List.Dates(date_from,step_num,#duration(1,0,0,0)), Splitter.SplitByNothing(), {"Date"}) in Source
Hope this helps, do give a kudos if it worked for you!
Thank you Samantha - is the shared code complete - I'm trying to use it in my query editor but is showing an error. SK
- SamanthaPuaXY1 year agoHelper II
It is a complete one, may I know the error you are facing?
let
date_from = #date(2025,1,1),
step_num = Duration.Days(Date.From(DateTime.LocalNow()) - date_from) + 1,
Source = Table.FromList(List.Dates(date_from,step_num,#duration(1,0,0,0)), Splitter.SplitByNothing(),{"Date"})
in
Source