For this tutorial, we’ll branch out to some examples of time intelligence calculations using a combination of DAX functions.
This kind of calculation is useful when you want to compare your business data over a certain period. You can do it by days, months, quarters, or years.
Aside from the CALCULATE function, using other DAX functions can greatly help in building great time-related analysis.
I’ll introduce you to some of the most common DAX functions used for this kind of calculation.
Using Time Intelligence DAX Functions
In this example, I’ll teach you how to create a new measure to compare the current sales to last year. Specifically, we will compare the sales of a specific day from the previous and current year.
Let’s call this measure as Sales LY and start it off with the CALCULATE function. The CALCULATE function can change the context of any calculation.
Next, we need to put an expression, which in this case, is the Total Sales. Since we need to change the context to compare the sales, we’ll use the SAMEPERIODLASTYEAR function. This function returns a set of dates in the current selection from the previous year.
Lastly, we need to reference the Dates column from the Date table.
In the table, we can already see the current sales data from the Total Sales column. When we bring the Sales LY measure to the table, we’ll be able to see the total sales from last year. The Sales LY table still calculates the total sales, however, the date context is changed to the previous year.
For instance, on the 13th of October, 2018, the Total Sales is $153,058.20.
If we check the Sales LY data for the 13th of October, 2019, we’ll see that it is also $153,058.20.
With the help of the CALCULATE function, the formula changes the initial context to look back on the sales of Oct. 13, 2018. This is a simple example of a time intelligence DAX calculation.
Creating A New Measure Group
If we look at our Key Measures group in the model, you’ll notice that our measures are starting to pile up. Since we’re branching out into a new calculation, it’s best to create a new measure group. This way, we can optimize and clean our Power BI.
To do that, click the Home ribbon, and then click Enter Data.
Since the measures are about time intelligence calculations, we can name the new measure group as Time Comparison. After renaming, click Load.
Then, we can now move the related time intelligence DAX measures into the new table.
Also, we can make the Sales LY measure as the main measure. To do that, click the Modeling ribbon, and then click Home Table. From here, we can change the default home table from Key Measures to Time Comparison.
We can see that the Sales LY measure is now placed under the Time Comparison measure group.
Furthermore, we have to make sure that the Time Comparison table is placed near the Key Measures table.
This is the ideal set up of the data model since the two additional tables are just for organizational purposes. They are not part of the main model structure for data relationships.
Using Advanced Time Intelligence DAX Functions
We now have the Total Sales and Sales LY data in our table. We can branch out to advanced time intelligence calculations using these core measures and other DAX functions.
In this example, we will calculate for Sales Difference Year on Year. In the formula, we simply have to find the difference between the Total Sales and Sales LY.
To see the results, we just have to bring the Sales Diff. YoY measure into the table. We can also turn this table into a visualization like this.
These are just simple visualizations. However, if we add slicers for months or weeks, the visualization will look more interesting.
In this other example, I’ll teach you another time intelligence calculation using a different DAX function. This time, let’s use the DATEADD function to calculate Sales Last Year.
Using the original formula, we just need to change the SAMEPERIODLASTYEAR to DATEADD. After referencing the Date table, enter -1 as the number of interval.
The DATEADD function is very flexible because we can go by day, month, quarter, or year.
Since we’re going by year, the formula will calculate the total sales from the previous year. Obviously, the results will be the same as before because we just used a different formula.
Lastly, we can simply copy and paste the existing measure and just change the name. For example, from Sales LY to Profits LY. Instead of Total Sales, we’ll reference the Total Profits measure.
That’s how we can see the profits from last year.
We can branch out to more analysis from here, like the current year’s profit. We can even work out the percentage difference year on year.
The key to a great time intelligence calculation is to use a combination of DAX functions.
***** Related Links *****
Common Time Intelligence Patterns Used In Power BI
The DATEADD Function: The Best And Most Versatile Time Intelligence Function in Power BI
Time Intelligence In Power BI: How to Calculate The Number of Transactions Made in the Last N Days
Conclusion
These time intelligence examples may be simple, but it’s a great application of all the things that we have discussed so far. We applied the concepts of initial context, iterating functions, aggregating functions, measure branching, and the CALCULATE function.
I hope you’ve learned a lot of techniques and DAX functions. It will be great if you can apply them to your reports as soon as you can.
Sam