One of the most important tables in any data model is the Date/Calendar table. Here I will show how quickly and easily this can be done inside Power Query by using M code.
Step 1: Create a Blank Query
I continue with the same report I created in the last tutorial where I showed how to import data and create a data model in Power BI Desktop which can be found here. Inside the Power Query Editor, I go to New Source and click on Blank Query.
Step 2: Paste the M code
Then I click on the Advanced Editor button which can be found under the Home tab in the Query group. Inside the Advanced Editor there are already four lines of code. I start by selecting all and deleting them after which I will paste the following M code and click Done.
Step 3: Enter Parameters
The next step is to select a Start and End Date and a Financial Start Month. In the data sample I am working with historical sales, and I can quickly verify that the first purchase date was in January 2015 going all the way to January 2018. So as the Start Date I choose 1/1/2015 and for the End Date I will go all the way to 31/12/2019. For the financial start month, I will type 1 for January because for many Danish companies the fiscal year follows the calendar year.
Step 4: Rename the Query
As a default the new query will be placed in the Other Queries folder and named “Invoked Function“. I double click on it and rename it to “Dates” and move it into the Dimensions Tables folder group. Then click on Close and Apply.
Step 5: Create Relationship with the Fact table
The last step is to create the relationship between the Dates table and the Fact table. For this, I navigate to the Model canvas and identify the columns that match. I drag the Date column from the Dates table to the Purchase Date column in the Fact Sales.
Now the Data Model is complete and ready to start writing DAX formulas and discover insights.
Leave a Review