Dates in Power BI. Working with Dates | by Peter Hui | Towards Data Science

In Power BI, the highly recommended approach from many experts is to create a date table when working with dates. I’ve worked without it and it’s definitely not a good idea. It gave me a headache and needless suffering.
The date table is important! Here are 3 reasons why.
Date Calculations — DAX needs a date table to make date calculations! Those SAMEPERIODLASTYEAR, LASTQUARTER or DATESBETWEEN type calculations wouldn’t work without it. Maybe it will, but it will just be very convoluted. Why hurt your head?
Weekends/Holidays — if you need to exclude holidays in your calculations, and you are doing it without a date table? It would be very difficult.
Different Granularity — Sometimes, you need to put together a comparison between a table that is based on month, while another is based on days. You need something to join them so you can report adequately.
In this article, I’ll show you how to create a calendar table in M (Power Query) and in DAX.
Let’s get started!
Let’s open Power Query (if you don’t know where to open it, take a look at the basics in this article.)
The table I want is a list of all the dates of a fiscal year, with days of the week named, month, and week number... also if possible, the holidays as well. We just want to flag those.
Something like the below would be good.
To do this in Power Query, we need to create a new query.
To create a new query — Right-click on the query pane> Select New Blank Query > Select Advance Editor (button on the top)
Once that is done — plug in this function into the advance editor.
Source = List.Dates(#date(2019,04,01),365,#duration(1,0,0,0))
Okay, what the heck does that mean?
It means I am using a List.Dates function, creating a date table from April 1, 2019, in the duration of 365 days.
Once you input this, Power Query will give us a list of all dates from 4/1/2020 to 365 days after.
Now it comes in a List format, so we have to convert it to a table in order to use our other functions. You can Select the column > Right-Click > To Table. It’ll convert it to a table for you.
Now let’s add in some other functions.
There are many other functions you can browse here. You can use these functions by taking the name of the function listed, and referring it to your date column.
For example, if I am interested in adding in the name of the day of the week, I can type in Date.DayOfWeekName([Date]). Also if I am interested in just the number, the actual day of the week, I can use Date.DayofWeek([Date]).
Many functions are listed in the link, it’s a lot to experiment, so I have used only the ones I use the most. I don’t know them all by heart.
At the end, you will end up with a table like below.
Now it is quite tedious to type all this for your PBI reports, so I have created a simple script for you. You may have to adjust the data types when you load it into PBI.
Here is the script for a basic date table — copy the below Right Click >Select New Blank Query > Select Advance Editor > Paste the below
Add in the 4 parameters according to your needs and you are good to go.
(Year as number, Month as number, Days as number, Duration as number) =>let    Source = List.Dates(#date(Year,Month,Days),Duration,#duration(1,0,0,0)),    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each Date.DayOfWeek([Column1])),    #"Renamed Columns5" = Table.RenameColumns(#"Added Custom",{{"Column1", "Date"}}),    #"Added Custom1" = Table.AddColumn(#"Renamed Columns5", "Custom.1", each Date.DayOfWeekName([Date])),    #"Renamed Columns" = Table.RenameColumns(#"Added Custom1",{{"Custom", "Day of Week"}, {"Custom.1", "Day of Week Name"}}),    #"Added Custom2" = Table.AddColumn(#"Renamed Columns", "Custom", each Date.DayOfYear([Date])),    #"Renamed Columns1" = Table.RenameColumns(#"Added Custom2",{{"Custom", "Day of Year"}}),    #"Added Custom3" = Table.AddColumn(#"Renamed Columns1", "Custom", each Date.Month([Date])),    #"Renamed Columns2" = Table.RenameColumns(#"Added Custom3",{{"Custom", "Month"}}),    #"Added Custom4" = Table.AddColumn(#"Renamed Columns2", "Custom", each Date.MonthName([Date])),    #"Renamed Columns3" = Table.RenameColumns(#"Added Custom4",{{"Custom", "Month Name"}})in    #"Renamed Columns3"
Once entered, Power Query will bring you a date table. Now you can just enter the perimeters according to your needs.
When loaded into the report, you just have to mark your table as a date table. Marking your table as a date table is explained below.
You will then have a date table.
The process to create a date table in DAX is very similar.
You have to select Table Tools > New Table in Power BI report view, once that is done, you can populate your table by this function.
Table = CALENDAR(DATE(2019,04,01),DATE(2020,03,31))
Once you use this function to create a new table , you will get the following.
Now, mark it as a date table by going to Table Tools > Mark as Date Table
Power BI will ask you to select the date column. Once selected it will verify the table as a date table.
With that being done, you can add in the other columns using the FORMAT function. Here I have added in FORMAT(Table[Date],”DD”) and others, I’ve selected a few common ones I have used, but you can add your own format style as well, check out the last column below.
There are of course other formats as well. You can get more info here
You now know how to create a simple date table using both DAX and M. Now what’s the point of all this? This is so you can connect your table (likely a fact table) with your date table.
Something like this will likely help you.
You can now create a many to 1 join between the fact table and the date table.
Since your dates are now reflected in a date table, you can now do calculations like the below.
My personal favorite.
Getting the Total Sales of between two dates.
Now these DAX date calculations only work if you have a date table and if it’s connected this way. Perhaps your fact table is small and you can get away with having a date column within your fact table, but it just won’t be pretty.
There are many many cool DAX date-time intelligence functions that are here.
I get this too. “It’s great that you’ve shown me how to build a date table in DAX and in M and a couple of measures using time intelligence but I can’t even get started because my date column in my fact table is screwed up to begin with!”
Here is what you can do — and it works almost all the time for me. Hopefully, your table is not screwed up to a point where the months and days are reversed row by row.. that may be another article in itself.
I suggest using the “column by example” in Power Query. The “AI” in Power Query over here. I wasn’t able to use the Date column here because it is in Text and even when converted to date, it throws an Error.
Start populating the date column yourself and the AI will pick up what you are doing and create the new date column for you.
Notice how it picks up what you are doing when you are adding the slash?
It gets what you are trying to do!
Now you can do your join to the Date table.
Hope you’ve enjoyed this article!
Stay encouraged, stay safe and good luck with your data journey!