How to Use CALCULATE in Power BI

Great Uses for CALCULATE in Power BI

Calculate is one of the most versatile functions in Power BI. When you begin using anything from simple filters, time intelligence functions or even advanced formulas, often the CALCULATE formulas are leveraged to produce the desired outcome.

Let ’s use CALCULATE to filer a column in a table. CALCULATE can be used for single filter conditions or multiple filter conditions. Let’s explore the function’s syntax. If you are familiar with Tableau this equivalent would be the level of detail functions.

How to Use Calculate

CALCULATE(Expression,Filter1)

In the formula, there are two basic arguments. The expression and the filter. An expression can be anything from an aggregation or calculated measure. For these purposes, we are going to use the SUM and Average functions with our CALCULATE function.  However, please remember you are not limited to this aggregation.

CALCULATE(SUM(Table[Column],Filter1)

CALCULATE(SUM(Table[Column],Filter1)

CALCULATE(AVERAGE(Website[Sessions]),Website[type]="Organic Search")


The benefit of using CALCULATE to filter is using your newly created calculated measures add a layer of context and complexity to your visualizations. For example, overlaying the amount of website organic search visits to the total am your visuals. This can be achieved with the intricate filter on your visualization pane, however, it a be done rather quickly using these CALCULATE measures. Usually, when your data is a column field, you are restricted to the stacked graphs and filter to isolate the dimension of choice. The CALCULATE function can help circumvent this.

You can use the calculate function to isolate the dimension you want within the same table

Use Calculate with Two or More filters.

Sometimes your data may need more to be filtered by more than one conditions to provide you with the segment that you are interested in.   Imagine you have the challenge of fiding out the number of sessions for your organic search visits from females. This can be achieved by adding an additional filter to your CALCULATE function.

To use CALCULATE with multiple filters you can just simply add another filter condition.:

CALCULATE(AVERAGE(Website[Sessions]),Website[type]="Organic Search", Website[gender]=’female’)

You can have as many conditions as you want by adding more conditional filters. We will explore other conditions using some of the time intelligence function in Power BI.

Use Calculate to with Time Intelligence Functions

Calculate is a great function to compare with time intelligence functions such as SAMEPERIODLASTYEAR, PREVIOUS MONTH, PREVIOUSDAY.

For me, this is one of the best uses of CALCULATE to evaluate time series data. Let’s look at the example below which will compare prices this year versus last year.

** A lot of time intelligence will calculate will not function if you don’t have continuous dates in your table.

The formula we are using is below:

CALCULATE(SUM(Website[Sessions],SAMPERIODLASTYEAR(Website[date]))

You can also achieve the same result using the YEAR function :

CALCULATE(SUM(Website[Sessions],YEAR(Website[date)-1)

You can even go one further and compare this year price vs last year for just organic by adding an additional filter. Here is This year vs Last Year sum of total website sessions.

 

CALCULATE(SUM(Website[Sessions],SAMPERIODLASTYEAR(Date[date]), Website[type]="Organic")

 

How CALCULATE works with ALL and FILTER,

It’s very important to understand how CALCULATE filters the data that you are adding to the function. So we will export how the functions ALL and FILTER  can change the results of your data.

ALL

These functions will help you achieve different objectives. A lot of the time when dealing with table functions, simply using SUM will not give you the total of all dimensions. It will give you the total or calculation by dimension. ALL allows you to take the total or calculation across all dimensions. In other words, a true total.

The benefit of the ALL function is that it will allow you to use this in calculations to get percentages across different dimensions.

ALL Calculate = CALCULATE(SUM(Website[Sessions]),ALL(Website[ Channels))

FILTER

Filter with Calculate allows you to isolate the calculation to that one dimension. In this example, we just want the total for the single dimension that we specified.

 CALCULATE(SUM(Website[Sessions]),FILTER(Website,Website[ Channels]="Organic Search"))

 

 

 

 

 

 

 

About the Author

Leave a Reply

Be the First to Comment!

Notify of
avatar