Calculate

The CALCULATE function is an extremely powerful tool in the arsenal of any DAX author. This is because the CALCULATE function can be used to ignore, overwrite, or change the existing filter context. You may be asking yourself why—why would anyone want to ignore the default behavior of Power BI? Let's take a look at an example.

Let's assume you want to return the total sales of each country as a percentage of all countries. This is a very basic percent of total calculation: Total Sales per country divided by Total Sales for all countries. However, how do you get the total sales of all the countries so that you can perform this calculation? This is where the CALCULATE function comes into the picture. Take a look at the following screenshot: 

Figure14-Calculating total sales of all the countries

To do the percent of total calculation, you need to get Total Sales all Countries on the same row as Total Sales. This means you need to create a new calculated measure that ignores any filters that come from the country attribute. Create a new calculated measure on your Internet Sales table using the following DAX formula:

Figure 15-Create a new calculated measure on Internet sales table using DAX formula

The preceding calculation will return all sales for all countries, explicitly ignoring any filters that come from the Country column. Let's briefly discuss why this works.

The first parameter of the CALCULATE function is an expression, and you can think of this as an aggregation of some kind. In this example, the aggregation is simply Total Sales. The second parameter is a filter that allows the current filter context to be modified in some way. In the preceding example, the filter context is modified by ignoring any filters that come from the country attribute. Let's take a look at the definition for the ALL function used in the second parameter of the CALCULATE function:

ALL: Returns all the rows in a table, or all the values in a column, ignoring any filters that may have been applied.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.59