Understanding Calculate
Understanding Calculate
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
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
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
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
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
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
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
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