0% found this document useful (0 votes)
21 views5 pages

Understanding Calculate

Uploaded by

Abhyudya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Understanding Calculate

Uploaded by

Abhyudya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

06-05-2023

Introducing CALCULATE and CALCULATETABLE Creating filter contexts


• CALCULATE and CALCULATETABLE are the only functions required to • Contoso is a company that sells electronic products all around the
operate on the filter context. world. Some products are branded Contoso, whereas others have
different brands.
• CALCULATE and CALCULATETABLE are the only functions that can
Understanding CALCULATE and create a new filter context by manipulating the existing one. • One of the reports requires a comparison of the gross margins, both
as an amount and as a percentage, of Contoso-branded products
CALCULATETABLE • We discuss examples based on CALCULATE only, but remember that
CALCULATETABLE performs the same operation for DAX expressions
against their competitors.
• The first part of the report requires the following calculations:
returning a table. • Sales Amount := SUMX ( Sales, Sales[Quantity] * Sales[Net Price] )
• Gross Margin := SUMX ( Sales, Sales[Quantity] * ( Sales[Net Price] - Sales[Unit
Cost] ) )
• GM % := DIVIDE ( [Gross Margin], [Sales Amount] ) F05 01.pbix

05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 137 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 138 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 139

Creating filter contexts Creating filter contexts


• Although these measures work, it is easy to note that the initial elegance of
F05 02.pbix DAX is lost (lengthy codes in F05 02.pbix).
• The model already contains one measure to compute the gross margin and
another measure to compute the gross margin percentage.
Check the new measures
Added ad Contoso GM • However, because the new measures needed to be filtered (for Contoso),
And Contoso GM% we had to rewrite the expression to add the condition.
• If we could make DAX compute the Gross Margin measure by creating a
filter context programmatically, which only filters the Contoso-branded
products, then our implementation of the last two measures would be
much easier.
• This is possible by using CALCULATE.

05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 140 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 141 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 142

CALCULATE syntax CALCULATE - CONTOSO Introducing CALCULATE


• CALCULATE ( Expression, Condition1, … ConditionN ) • The code for Contoso Margin and Contoso GM % becomes much • CALCULATE is the only DAX function that can modify the filter
• CALCULATE can accept any number of parameters. The only simpler: context; and remember, when we mention CALCULATE, we also
mandatory parameter is the first one, that is, the expression to • Contoso GM := include CALCULATETABLE.
evaluate. • CALCULATE does not modify a filter context: It creates a new filter
• CALCULATE ([Gross Margin], -- Computes the gross margin
• The conditions following the first parameter are called filter • 'Product'[Brand] = “Contoso”) -- In a filter context where brand = Contoso context by merging its filter parameters with the existing filter
arguments. context.

• CALCULATE creates a new filter context based on the set of filter • Once CALCULATE ends, its filter context is discarded, and the previous
arguments.
filter context becomes effective again.
• Once the new filter context is computed, CALCULATE applies it to the
model, and it proceeds with the evaluation of the expression.

05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 143 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 144 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 145
06-05-2023

Introducing CALCULATE Introducing CALCULATE Introducing CALCULATE


• The first parameter is the expression that CALCULATE will evaluate. • It is useful to always read filter arguments as tables. This makes the • The filter parameter of FILTER in
the CALCULATE of Contoso Sales
• Before evaluating the expression, CALCULATE computes the filter behavior of CALCULATE more apparent. scans ALL(Product[Brand]);
arguments and uses them to manipulate the filter context. • A filter argument is a table, that is, a list of values. therefore, any previously existing
filter on the product brand is
• The first important thing to note about CALCULATE is that the filter • The table provided as a filter argument defines the list of values that overwritten by the new filter.
arguments are not Boolean conditions: The filter arguments are will be visible—for the column—during the evaluation of the • This is more evident when you use
the measures in a report that slices
tables. expression. by brand. F05 04.pbix
• Contoso Sales reports on all the
rows/brands the same value as
Sales Amount did for Contoso
specifically.
The two syntaxes are equivalent

05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 146 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 147 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 148

Introducing CALCULATE Introducing CALCULATE Introducing CALCULATE


• In every row, the report creates a • CALCULATE does not overwrite the whole original filter context. • Change the report to now slice by
filter context containing the Product[Category].
relevant brand. • It only replaces previously existing filters on the columns contained in • Now the report is filtering
Product[Category], whereas CALCULATE
• For example, in the row for Litware the filter argument. applies a filter on Product[Brand] to
the original filter context created evaluate the Contoso Sales measure.
by the report contains a filter that • The two filters do not work on the same
only shows Litware products. column of the Product table.
• Then, CALCULATE evaluates its • Therefore, no overwriting happens, and
filter argument, which returns a the two filters work together as a new
table containing only Contoso. filter context.
• The newly created filter overwrites • As a result, each cell is showing the sales
of Contoso for the given category.
the previously existing filter on the
same column.

05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 149 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 150 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 151

Introducing CALCULATE Introducing CALCULATE Introducing CALCULATE


CALCULATE, we can summarize its semantics as: • If a column already has a filter, then the new filter replaces the • CALCULATE creates a filter context out of the existing row contexts.
• CALCULATE makes a copy of the current filter context. existing one. • CALCULATE accepts filters of two types:
• CALCULATE evaluates each filter argument and produces, for each • On the other hand, if the column does not have a filter, then 1. Lists of values, in the form of a table expression. In that case, you
condition, the list of valid values for the specified columns. CALCULATE adds the new filter to the filter context. provide the exact list of values you want to make visible in the new
• If two or more filter arguments affect the same column, they are • Once the new filter context is ready, CALCULATE applies the filter filter context.
merged together using an AND operator. context to the model, and it computes the first argument: the • The filter can be a table with any number of columns.
expression. 2. Boolean conditions, such as Product[Color] = “White”. These filters
• CALCULATE uses the new condition to replace existing filters on the
columns in the model. • In the end, CALCULATE restores the original filter context, returning need to work on a single column because the result needs to be a
the computed result. list of values for a single column. This type of filter argument is also
known as predicate.
05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 152 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 153 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 154
06-05-2023

Using CALCULATE to compute percentages Using CALCULATE to compute percentages Using CALCULATE to compute percentages
• A pattern that appears often is that of percentages. When working • This gives the result we want.
F0508.pbix F0509.pbix
with percentages, it is very important to define exactly the calculation • ALL has specific semantics when
required. used as a filter argument of
• Learn how different uses of CALCULATE and ALL functions provide CALCULATE.
different results. F05 08.pbix • In fact, it does not replace the
• This file is the final result we want to obtain. filter context with all the values.
ALL removes the filter on the • Instead, CALCULATE uses ALL to
Product[Category] column from the filter
context. Thus, in any cell of the report, it remove the filter on the
ignores any filter existing on the categories. category column from the filter
F05 09.pbix context.
05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 155 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 156 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 157

Using CALCULATE to compute percentages Using CALCULATE to compute percentages Using CALCULATE to compute percentages
• The previous code removes the filter from the category, but it does • The original filter context created by • The same formula makes perfect
the report contained both a filter on sense in a different report.
not touch any other existing filter. category and a filter on color.
• Look at what happens if one
• Therefore, if the report adds other filters, the result might not be • The filter on Product[Color] is not switches the order of the columns,
exactly what one wants to achieve. F05 10.pbix overwritten by CALCULATE, which only building a report that slices by
removes the filter from color first and category second F05
• When color is added as a filter, the results are not correct. Product[Category].
12.pbix
• As a result, the final filter context only
contains the color. • The percentage shown is the
percentage of the category inside
• Therefore, the denominator of the the given color.
ratio contains the sales of all the
products of the given color—Black— • Color by color, the percentage
F05 10.pbix and of any category. always adds up to 100%.
05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 158 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 159 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 160

Using CALCULATE to compute percentages Using CALCULATE to compute percentages Using CALCULATE to compute percentages
• The goal is to fix the calculation so • If the user wants to create a Further problems:
that it computes the percentage • One might want to analyze the
against a filter on either the category percentage that removes all the percentages by also slicing columns from
or the color. filters on the Product table, they different tables. F05 15.pbix.
could still use the ALL function • The measure at the denominator
• One possible solution is to let removes any filter from the Product table,
CALCULATE remove the filter from passing a whole table as an but it leaves the filter on
both the category and the color. argument. F05 14.pbix Customer[Continent] intact.
• Adding multiple filter arguments to • Therefore, the denominator computes
CALCULATE • ALL on the Product table the total sales of all products in the given
continent.
• It produces the right percentage with removes any filter on any • As in the previous scenario, the filter can
color and category F05 13.pbix column of the Product table. be removed from multiple tables by
• It will fail as soon as one adds other putting several filters as arguments of
columns to the report. CALCULATE: F0516.pbix

05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 161 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 162 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 163
06-05-2023

Using CALCULATE to compute percentages Using CALCULATE to compute percentages Using CALCULATE to compute percentages
• One possible solution when they want to remove the filter from any • F05 17.pbix
table that might affect the calculation is to remove any filter from the • The Calendar Year belongs to the
fact table itself. Date (different) table, which is
• In our model the fact table is Sales. not used in the measure.
• This measure leverages relationships • Nevertheless, the filter on Date
to remove the filter from any table is removed as part of the
removal of filters from Sales.
that might filter Sales.
F0516.pbix

05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 164 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 165 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 166

Using CALCULATE to compute percentages Using CALCULATE to compute percentages Using CALCULATE to compute percentages
• What if the goal is to compute a • VALUES returns the list of values • CALCULATE has two filter arguments: ALL ( Sales ) and VALUES ( Date[Calendar
percentage over the grand total of a column in the current filter Year] ).
of only the current year? context. • ALL ( Sales ) removes the filter from the Sales table.
• VALUES ( Date[Calendar Year] ) evaluates the VALUES function in the original
• This requires two actions: • Because the result of VALUES is a filter context, still affected by the presence of CY 2007 on the columns.
• Removing all filters from the fact table, it can be used as a filter • As such, it returns the only year visible in the current filter context—that is, CY 2007.
table argument for CALCULATE. • The two filter arguments of CALCULATE are applied to the current filter context,
• Restoring the filter for the year resulting in a filter context that only contains a filter on Calendar Year.
• As a result, CALCULATE applies a
• The two conditions are applied filter on the given column, • The denominator computes the total sales in a filter context with CY 2007 only.
at the same time. restricting its values to those • Using ALL over a table followed by VALUES over a column is a technique used to
replace the filter context with a filter over that same column.
returned by VALUES.

05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 167 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 168 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 169

Introducing KEEPFILTERS Introducing KEEPFILTERS Introducing KEEPFILTERS


• CALCULATE overwrites the existing filters on the columns where a • Instead of overwriting an existing filter over the same column, it adds the
new filter is applied. new filter to the existing ones.
• All the remaining columns of the filter context are left intact. • Therefore, only the cells where the filtered category was already included
in the filter context will produce a visible result.
• In case you do not want to overwrite existing filters, you can wrap the
filter argument with KEEPFILTERS. • Because KEEPFILTERS avoids overwriting, the new filter generated by the
filter argument of CALCULATE is added to the context.
• For example, if you want to show the amount of Audio sales when
Audio is present in the filter context and a blank value if Audio is not • If we look at the cell for the Audio Sales KeepFilters measure in the Cell
Phones row, there the resulting filter context contains two filters: one
present in the filter context, you can write the following measure: filters Cell Phones; the other filters Audio.
• Audio Sales KeepFilters := CALCULATE ([Sales Amount], KEEPFILTERS ( • The intersection of the two conditions results in an empty set, which
'Product'[Category] = "Audio" ))F05 21.pbix produces a blank result.

05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 170 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 171 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 172
06-05-2023

Introducing KEEPFILTERS CALCULATE modifiers Understanding USERELATIONSHIP


• The behavior of KEEPFILTERS is • CALCULATE accepts several different modifiers that change how the • CALCULATE can activate a relationship during the evaluation of its
clearer when there are multiple new filter context is prepared. expression by using this modifier.
elements selected in a column.
• However, the most important of all these modifiers is a function that • A data model might contain both active and inactive relationships.
• For example, consider the
following measures; they filter you already know very well: ALL. • One might have inactive relationships in the model because there are
Audio and Computers with and • When ALL is directly used in a CALCULATE filter argument, it acts as a several relationships between two tables, and only one of them can be
without KEEPFILTERS: F0523.pbix active.
CALCULATE modifier instead of being a table function.
• KEEPFILTERS only computes the • As an example, one might have order date and delivery date stored in the
sales amount values for Audio and • Other important modifiers include USERELATIONSHIP, CROSSFILTER, Sales table for each order.
for Computers, leaving all other and ALLSELECTED, which have separate descriptions.
categories blank. • Typically, the requirement is to perform sales analysis based on the order date, but
• The Total row only takes Audio and • The ALLEXCEPT, ALLSELECTED, ALLCROSSFILTERED and one might need to consider the delivery date for some specific measures.
Computers into account. ALLNOBLANKROW modifiers have the same precedence rules of ALL. • In that scenario, an option is to create two relationships between Sales and Date:
one based on Order Date and another one based on Delivery Date.

05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 173 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 174 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 175

Understanding USERELATIONSHIP Understanding USERELATIONSHIP Understanding USERELATIONSHIP


• Only one of the two relationships can • The relationship between Delivery Date and Date is activated during
be active at a time.
the evaluation of Sales Amount.
• For example, in this demo model the
relationship with Order Date is active, • In the meantime, the relationship with Order Date is deactivated.
whereas the one linked to Delivery
Date is kept inactive. • When using USERELATIONSHIP to activate a relationship, you need to
• To author a measure that shows the be aware of an important aspect: Relationships are defined when a
delivered value in a given time period, table reference is used, not when RELATED or other relational
the relationship with Delivery Date functions are invoked.
needs to be activated for the duration
of the calculation.
F05 37.pbix
• In this scenario, USERELATIONSHIP is
of great help as in the following code:
05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 176 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 177 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 178

Understanding USERELATIONSHIP Understanding USERELATIONSHIP Understanding USERELATIONSHIP


• To compute all amounts • A much better formulation of the same • One important note is the fact that USERELATIONSHIP does not
measure relies on default filter context
delivered in 2007, the following propagation rather than relying on introduce any filter by itself.
RELATED.
formula will not work. • Indeed, USERELATIONSHIP is not a filter argument. It is a CALCULATE
• When you use USERELATIONSHIP in a
• CALCULATE would inactivate the CALCULATE statement, all the filter modifier. It only changes the way other filters are applied to the
arguments are evaluated using the
row context generated by the relationship modifiers that appear in the model.
same CALCULATE statement—regardless
FILTER iteration. of their order. • If you carefully look at the definition of Delivered Amount in 2007 v3,
• Thus, inside the CALCULATE • For example, in the Delivered Amount you might notice that the filter argument applies a filter on the year
2007 v3 measure, the USERELATIONSHIP
expression, one cannot use the modifier affects the predicate filtering 2007, but it does not indicate which relationship to use.
RELATED function at all. Calendar Year, although it is the previous
parameter within the same CALCULATE • Is it using Order Date or Delivery Date? The relationship to use is
function call.
defined by USERELATIONSHIP.
05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 179 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 180 05-05-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 181

You might also like