Power BI

Create a DateIntKey

10min

Introduction

Aimplan expects that you will store planning data within the context of a data dimension. Every single number should be connected to a 'point in time'. In a Power BI model you normally have a so called 'date dimension' table containing a record for each date. And each field within this table is describing which month, quarter, year etc to which the date is associated.

Aimplan expects the 'key' field in this date table to be in the format YYYYMMDD and of type integer. I.e. an ISO-date. This is a common way how a date dimension key in a data warehouse is structured. But what if your date dimension do not have such integer date key?

But how about if you do not want to store your planning on day-granualrity? I.e. you plan your sales or other per month, quarter or year. You still will need to store each planning value on one single day. A common approach is to store these values on the first day of the period. Or the last. And Power BI have built in DAX functions to resolve this.

If you do not have a date-dimension in your Power BI model - you should for sure add one. This is best practice in Power BI. If you do not have one, you see the section below how to create a calculated date dimension table.

Creating a DateIntKey Calculated Column in a date dimension table

In Power BI you can create so called 'Calculated Column' in your date dimension table. Perform the following steps:

  • In Power BI desktop, click the middle 'Data' button on the left side to manage your tables and data
  • Select your date dimension in the list of tables on the right
  • Click the 'New Column' button in the toolbar
  • Add a column similar using this DAX statement
Text

  • Replace 'DimDate'[Date] with the name of your date dimension table and the date field you want to convert
  • Press enter to save the new column to the date table
  • Select the new calculated column (click on the column header), and in the Power BI toolbar->Column tools, change the data type to "Whole number"

You now have a DateIntKey that you can use as a relationship to the Aimplan planning-data table DateInteKey.

Createing a FirstDateMonthIntKey

But to be able to store data you want to have other "Keys" in your date dimension. Depending on what granularity in the date dimension you want to store data on, you can create "FirstDateMonthIntKey" or "FirstDateQuaryterIntKey" etc.

Create another calculated column (as above) for "FirstDateMonthIntKey"

Text


You should now have a new calculated column in your existing date table called FirstDateMonthIntKey containing the first day of the month for each given date as YYYYMMDD as integers. If you want to do planning/input on a month-level granularity - this is a good field to use when configuring the Aimplan Planning and Reporting Visual. But important - you should not create any relationship between your date dimension and the fact table using this field.

But what if you want to plan on week, quarter or year-level? Then you can add the following calculated columns to your date dimension.

Text


Note: These fields should not be used to build relationships between the date table any fact tables in your model. You should continue to use your DateKey or Date or whatever field you previously used to join your fact tables with your date dimension. Use the above created "DateInteKey" to join the Aimplan planning data table. And use the calculated "FirstDate...IntKey"-fields in the configuration of the Aimplan Planning and Reporting visual. See below.



Using FirstDateMonthIntKey in a visual

The field 'DateIntKey' is a mandatory field when the Planning and Reporting visual will store data back to the underlying database. The name of the field must be exact 'DateIntKey'. In Power BI you can for a specific visual on a page rename the field with an alias. This makes it possible to use for example the "FirstDateMonthIntKey" above as an input to the visual.



Document image


In the picture above we have added Year, Year-Month and a few other date fields to the visual to be used to group data per year, quarter, month etc (yellow). But - very important - we have also added the "FirstDateMonthIntKey" and by dubbel clicking on it renamed it to "DateIntKey" (green). I.e. we have renamed the field for this specific visual and the visual. When saving the data in the visual to the database it will be stored to the first date of each month.



DAX Example of a Power BI generated date dimension

It's good practice in Power BI to use a date dimension table. If you do not have a date dimension table already in your model there are build in functionality in Power BI and DAX to generate such table without loading it from some database or similar.

In Power BI it's possible to create 'Calculated tables'. And there are special functions to generate date dimension tables starting from a specific date, and ending at a specific date. If you google the Internet you will find many such DAX examples.

Here is one such example that both returns a table with every day from a 2020-01-01 to 2024-12-31 (can be changed in the script below).

Text


This date dimension returns a field for "DateIntKey" which returns the first day of month, and is used to join to the DateIntKey field in the Aimplan fact-planning table.

Don't forget to mark this new table as a "date table". This will make Power BI know that this is a date dimension and you can use time intelligence functions such as "PREVIOUSYEAR" and others.

In your data model, right click your table header, and select "Mark as date table".



Document image