Forum Discussion
Fiscal Calendar Date Table
- 3 years ago
Please try out this approach that dynamically gets the start and end dates dynamically from your fact table. The first is the M/Power Query version (note this may cause performance problems as it will effectively load your fact table a second time to get these dates). The 2nd is the DAX version of same.
let FactDates = List.Buffer(List.Distinct(FactQuery[DateColumn])), StartDate = List.Min(FactDates), EndDate = List.Max (FactDates), DateList = List.Dates(StartDate, Number.From(EndDate - StartDate)+1, #duration(1,0,0,0)), InitialTable = Table.FromColumns({DateList}, {"Date"}), #"Changed Type" = Table.TransformColumnTypes(InitialTable,{{"Date", type date}}), AddFY = Table.AddColumn(#"Changed Type", "FY", each Date.Year(Date.AddMonths([Date], 6))), AddFQ = Table.AddColumn(AddFY, "FQ", each Date.QuarterOfYear(Date.AddMonths([Date], 6))), AddFM = Table.AddColumn(AddFQ, "FM", each Date.Month(Date.AddMonths([Date], 6))), #"Added Custom" = Table.AddColumn(AddFM, "FW", each let FYstart = #date([FY]-1, 7, 1), result = Number.RoundUp((Duration.TotalDays([Date] - FYstart)+1)/7, 0) in result), #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"FY", Int64.Type}, {"FQ", Int64.Type}, {"FM", Int64.Type}, {"FW", Int64.Type}}) in #"Changed Type1"FiscalDates = ADDCOLUMNS ( CALENDAR ( MIN ( Sales[SaleDate] ), MAX ( Sales[SaleDate] ) ), "FY", YEAR ( EOMONTH ( [Date], 6 ) ), "FQ", QUARTER ( EOMONTH ( [Date], 6 ) ), "FM", MONTH ( EOMONTH ( [Date], 6 ) ), "FW", ROUNDUP ( ( INT ( [Date] - DATE ( YEAR ( EOMONTH ( [Date], 6 ) ) - 1, 7, 1 ) ) + 1 ) / 7, 0 ) )Pat
Anonymous Well, that's a good question. I know that Enterprise DNA has a really good date table that Melissa de Korte put together: Extended Date Table Power Query M Function - Enterprise DNA. Using The M Function To Create An Extended Power BI Date Table Code | Enterprise DNA.
You can also do it in DAX: DAX Custom 445 Calendar - Microsoft Power BI Community
I suppose the DAX approach would have the advantage of being able to specify MAX and MIN of the dates in your fact table although you might be able to do something similar in Power Query with a little work. Essentially, create queries for grabbing max and then min and use them as parameters in your Power Query M function more or less.