I'm going to share the dynamic date dimension table I built, hopefully it is useful to someone! I named it DateTable for this tutorial, and my fact table FactTable.
The table is built using the following formula:
DateTable = CALENDAR (MINX('FactTable', [Created Date]), NOW())
Basically, it looks for the oldest date you have for the column [created date], and builds a sequential date range until the time of refresh - so it always has calendar information for all data points in the set.
I added the following columns:
Date = This is built when you create the table
Year = YEAR([Date])
MonthofYear = MONTH([Date])
QuarterofYear = ROUNDUP (MONTH([Date])/3,0)
OrdinalDate = DATEDIFF([Year]&",1,1",[Date],DAY)+1
DayofWeek = WEEKDAY([Date],2)
ISOWeekofYear = IF ((([OrdinalDate] - [DayofWeek] + 10)/7) >= 53, 1, TRUNC(([OrdinalDate] - [DayofWeek] + 10)/7))
DayofMonth = DAY([Date])
Month = FORMAT([Date], "MMM") & " " & [Year])
Quarter = "Q" & [Quarter] & " " & [Year]
Week Ending = [Date] + (7- [DayofWeek])
Day = Format([Date],"DDDD")
With both the numeric and text values, you can use "Sort by" to properly order them in visuals. If you want your week numbers to start on a Sunday instead of Monday, change the "1" to a "2" in ISOWeekofYear column - however ISO Standard is a Monday Start.
I'm still pretty new to PowerBI, but I'll help if I can! If you have suggestions or modifications, I'd love to hear them.
EDIT: I changed how the week number is calculated, so I can use ISO Week Numbers. This also helped fix the "week Ending column, which broke when spanning multiple years that included a leap year. Let me know if you find any problems!
EDIT2: MS just released the timeline slicer for PowerBI. That probably, mostly makes this table useless. Read more here: http://blogs.msdn.com/b/powerbi/archive/2015/11/03/visual-awesomeness-unlocked-the-timeline-slicer.a...
Thanks for this
What would you do if you wanted more than one calendar. Say an Invoice Date plus an Order Date Calendar
Would you need two new tables or could one be set up (called DateTable) and filter by date type in the measure somehow?
Is this possible
EDIT. To do the above
@konstantinos : Why would you use a switch and type in all the names, rather than use the format function? Is it a perfromance difference?
@RJ : Check out http://community.powerbi.com/t5/Desktop/Transitive-Relationships/m-p/8563#U8563 . It's an interesting concept, and apparently having inactive relationships is fine - the software is still smart enough to filter right through. It wasn't exactly what I ended up using, but that's because I wasn't using a slicer, I wanted a clustered column chart.
@ALeef Apologies..SWITCH is slower of course. To be honest I wasn't aware that FORMAT can return months and days due to always importing Date Table..
Thanks, always learning..
TODAY() only returns a day, and sets the time to 12:00am. For this instance, I don't think it matters, but I use timestamps regularly, so that is what I'm in the habit of.
I am new to PowerBI and I don't understand how to actually create this table.
I tried creating a New Source -> Blank Query and inserting your query, but that doesn't seem to work.
Could you please add some more detailed steps on how to add it to the queries, for newbies like me?
@bernitorres You can create/import tables with 2 ways. One is importing with "Get Data" and the other is through DAX data modelling.
You need to go to the data model view and in the ribbon go to modeling tab & select "New Table". thus using DAX language.
The "Get data" uses M language which is used only for importing data to data model.
Keep in mind that you need to already have import some data in order to see the :Modelling" tab..