Skip to main content

How to create a Date Table using DAX

In order to utilise the time-based functionality of PowerBI, you'll need to add a Date Table to the data model. This can be done through a few different methods. The easiest way to do this is to use DAX to generate the table and associated columns. One way of doing this is described below.

To download the file created in this example, click here.
  • Click 'Modelling' in the menu bar
  • Click 'New Table' in the menu
  • When the formula bar appears, enter DateTable = CALENDAR (date(2010, 1, 1) , today()) as shown below
  • This will now create a table with one row per date, from the 1st of January, 2010 to the current date. To see this table, click the 'Data' icon on the left hand side of the screen
  • You will then see the following table, with a list of dates as described previously
  • In order to develop your date table to provide additional columns, such as day number, week number, day name, month, etc, one method is to use the GENERATE function. In order to add an additional column containing the day number (i.e. 1st of Jan = 1, 2nd of Jan = 2, 31st of Jan = 31, 1st of Feb = 1) modify the DAX to the following
  • To add additional columns to the date table, insert additional rows between rows 5 and 6, below "DayNo..." using the syntax "Column Title", function
  • An example of a more comprehensive date table could look like the following. The DAX Code to create this is available as text at the end of this post

DateTable = 
Generate(
    CALENDAR (date(2010, 1, 1) , today()),
        row(
            "DayNo",day([Date]), // Date for calculations
            "Day ddd", Format([Date], "ddd"), //day name, Mon, Tue, Wed, etc 
            "WeekNo", WEEKNUM([Date]), // Gregorian week number
            "WeekDay", if (or (Format([Date], "ddd") = "Sat", Format([Date], "ddd") = "Sun"), 0, 1), // 1 if the date is a week day
            "WeekDayNum", WEEKDAY([Date], 2 ), // Number from 1-7 based on the week day
            "Week", [Date] + 7 - Weekday([Date], 2), // Last date of the week
            "MonthNum", MONTH ( [Date] ), // Number of the gregorian month
            "MonthName", FORMAT ( [Date], "mmmm" ), // Long name of current Month
            "MonthEnd", EOMONTH( [Date], 0), // Date of the last day of the month
            "Year", Year([Date]) // Year of the base date
           )
)
















Comments

Popular posts from this blog

How to combine multiple files with Power Query (with no VBA and just 10 mouse clicks!)

The need to combine information from multiple files is one that most users of Excel will have come across at some point in the use of Excel. I've personally spent far too many hours aggregating data from multiple files, that are identical in structure, so that I can analyse larger datasets and provide insights into products and processes. For anyone who has also done this and not yet discovered Power Query you'll probably be amazed how simple the process has become. I realise there might be some who will say "just use VBA, its easy once you learn how to code..." and they would be right. The method using Power Query provides a zero code solution that is an evolution of the Excel interface that many will already be familiar with. In this example, I've created a sample file and created a number of duplicates of the file which I've saved in a folder. The folder contains only these files and i'd recommend you do the same if you're looking to try out this pr...

Extracting data from Word (.docx) files into Power Query

Word and Excel don’t usually get along too well so it's no surprise that Power Query isn't directly compatible with its estranged cousin Word either. If you are presented with the need to import data from Word into Power Query you'll be please to hear it is possible however it requires a couple of manual steps to make it work.  The manual steps could fairly easily be completed by a batch file which would automate the process further. Here is the Excel data pasted 'as values' in a Word file which i'll use for the first example Here is the Excel data pasted with 'keep source formatting' which i'll reference a couple of times in the article. Although the steps I've covered below aren't complex, this whole process has some unknowns around it so you may find the result in your instance varies from mine. The Word file I've used contains the contents of a range of excel cells that I deliberately pasted as values into Word to create a test file f...

How to automate the import of all files in a Google Drive folder to PowerBI, now updated, please read the first paragraphs!! (PowerQuery)

Update!!! This method no longer works although there is a new Google Sheets connector for PowerQuery that is currently in beta. If you're using PowerBI, you'll need to enable the preview features to enable it. https://docs.microsoft.com/en-us/power-query/connectors/googlesheets In the interest if demonstrating how it 'did' work, the original post is provided below. If you attempt to replicate this, you'll quickly realise the website doesn't behave as it used to. (here is the original article)  Using PowerQuery to access multiple files within the same folder on a local or network drive is a game-changing feature that will almost certainly save many people hours of effort. This functionality is great if your data exists in a place that is easily accessible but what do you do if your data is somewhere less accessible like Sharepoint, OneDrive or even Google Docs? I have previously connected to data on SharePoint and found it fairly straight forward which raises the...