# Dynamically Calculating The Previous Highest Result Using DAX Functions

DAX functions greatly expand dynamic calculations. They also broaden the other important capabilities in Power BI.  In this tutorial, we’ll discuss how you can dynamically calculate the previous highest result in Power BI. We’ll also be learning the right combination of formula and techniques for this particular calculation. You may watch the full video of this tutorial at the bottom of this blog.

By doing a dynamic calculation, we can run the calculation of a selected data set, while their previous highest result continuously updates and is being displayed visually.

I’ll be showing you how you can actually do this. The formula for this does not need to be too complex at all. It just requires a really good understanding of iterating functions to make it work.

## Getting Data Into A Table

Let’s duplicate our current visualization here and we’ll have a look at it as a table.

Always make sure you get your data into a table. This allows you to actually have a look at the results.

Before anything else, let’s change the format of the Date column into this format that I usually prefer.

## Combination Of DAX Functions For Previous High Result Calculation

Here is the combination of DAX functions that we will use to calculate the previous highest result in your Power BI reports.

Take note that it is essential to get inside tables to fully understand what data you’re looking at.

Basically, we need to look back across every previous day. Then, identify what the maximum revenue date on any day was. Moreover, as we go down the list, the dates that we need to work through are going to be larger and larger.

This is because we’ve got to historically look back over every previous day for every single result in our table. That is exactly what I have done in this formula.

## Using The MAXX DAX Function

In this formula, I used an iterating function called MAXX.

This allows you to create the previously mentioned iteration. This iteration is essential for calculating the previous highest result.

All we need to do is to place the revised virtual table for every single result that we need to iterate through using this logic.

Now, let’s try to work through an example for this calculation.

## Using FILTER And ALLSELECTED DAX Functions

Let’s have a look at 6th of January, 2018 which will serve as our current date.

This FILTER function within the formula will look back through every single date in the currently applied context or filter. This is made possible with the help of the ALLSELECTED function.

The ALLSELECTED function only looks at dates within the current context (in this case, 2018).

If we used the ALL function instead of ALLSELECTED, it would look back through every single date from the beginning of time.

## Evaluating Through Every Single Dates

After looking through the dates in the current context, it’s going to iterate through every single day within your table. Then, it’s going to filter out the days that are less than or equal to the current date in the particular context.

The current date is calculated using the MAX function.

In this context, the current date is the 6th of January. Therefore, if the date is less than or equal to the current date, that will equate to TRUE.

Furthermore, that is also going to be the virtual table of dates that we will have within this iterating function.

Additionally, we will iterate through only those particular dates. Then, we will identify the maximum revenue day (Total Revenue).

Consequently, that’s what it does to every single row in this table. Every single calculation is done exactly the same.

Obviously, this virtual table gets bigger and bigger as you move down the list. And when you set it up exactly like this, it becomes completely dynamic.

By doing that, you can select anything from the filters. You can even change the date and time frame as well.

In addition, you’ll see that the Previous High visualization continuously reevaluates to calculate the highest value on an ongoing basis.

In other words, it’s like a live MAX value. If we add more and more filters, we’d still get updated results.

This formula is obviously not too complex. However, it can produce and add a few insights for your reports. Once you get the correct calculations into the table, you can finally change it into a Line and stacked column chart.

## Conclusion

To sum up, the key for this particular insight is to make sure you understand iterating functions. It will also allow you to place virtual tables inside them as well.

This highlights the outstanding capabilities of Power BI in terms of creating simple formulas, and the dynamic results that you can easily get.

This is quite a unique calculation which holds a lot of value. It can also add a little bit of color to your insights.

Good luck in trying to implement this into your own models.

For more information on how you can combine important DAX functions to find complex and valuable data insights in Power BI, you can check out this module at Enterprise DNA Online.

Sam

Sam McKay, CFA
Sam is Enterprise DNA's CEO & Founder. He helps individuals and organizations develop data driven cultures and create enterprise value by delivering business intelligence training and education.

## How To Merge Queries In Power BI

Merging is another powerful transformation to optimize our...

## Unpacking The Evolution Of Power BI – From Launch to Now

Microsoft Power BI is a dynamic business intelligence platform that has revolutionized the way...

## Finding Patterns In Your Data Using Internal Logic in Power BI

Today I’m going to showcase how you can discover patterns in your data when using Power BI. This is an...

## Report Builder Tutorial: Sort & Filter Data In Paginated Reports

In this Report Builder tutorial, you’ll learn how to sort and filter the data in your paginated...

## Creating A Pareto Chart In Power BI – Advanced DAX

This blog tutorial showcases a really interesting example around Pareto analysis. I’m going to teach...

## Power BI Tooltips On Report Images

With the recent Power BI challenges that we run in Enterprise DNA, I've become a huge fan of the Power...

## Best Practices In Using The Q&A Feature In Power BI

The Q&A feature or the natural language feature is seriously one of the most incredible features...

## Perpetual Holiday Calendar – POTW #12 (Power Query Solution)

In this blog post, we'll explore my solution to Problem of the Week #12. For this challenge, we...

## What Is DAX Studio In Power BI Desktop

This tutorial will discuss what DAX Studio is and how it can be used to optimize your DAX codes. DAX...

## DAX Queries Using Variables And Important Functions

This tutorial will discuss about variables, including the FILTER and CALCULATE functions. You’ll...

## Dynamic Tooltip In Power BI With Embedded Charts

I want to show you an awesome trick for the visualizations in your reports. You can use a dynamic...

## Power Query Features: An Overview

This tutorial will discuss about the available features inside the Power Query Editor. You'll learn how...