0% found this document useful (0 votes)
11 views

Using Basic Table Functions

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)
11 views

Using Basic Table Functions

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/ 3

27-02-2023

Introducing table functions Introducing table functions


• Table functions are regular DAX functions that—instead of returning a • For example, a simple iteration like the following uses a table as part
single value—return a table. of the calculation of the sales amount:
• DAX expression usually returns a single value, such as a string or a number. • Sales Amount := SUMX ( Sales, Sales[Quantity] * Sales[Net Price] )
• An expression that results in a single value is called a scalar expression. • FILTER is a function that filters the content of a table based on a
condition.
Using basic table functions • The primary goal of a measure is to produce results that are rendered in a
report, in a pivot table, or in a chart.
• At the end of the day, the source of all these reports is a number—in other
words, a scalar expression.
• Nevertheless, as part of the calculation of a scalar value, you are likely to
use tables.
27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 70 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 71 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 72

Introducing table functions Introducing table functions Introducing table functions


• Variables can store tables too • In a calculated column or inside an iteration, one can also use the • We cannot use the result of a table function as the value of a measure or of a
RELATEDTABLE function to retrieve all the rows of a related table. calculated column.
• Both measures and calculated columns require the expression to be a scalar value.
• Instead, we can assign the result of a table expression to a calculated table.
• A calculated table is a table whose value is determined by a DAX expression rather than
loaded from a data source.
• We can create a calculated table containing all the products with a unit price
• MultipleItemSales is a variable that stores a whole table because its • Table functions can be nested too. greater than 3,000 by using a table expression like the following:
expression is a table function. • Calculated column in the Product table computes
the product sales amount considering only sales with a
• Use variables whenever possible because they make the code easier quantity greater than one.
to read.
27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 73 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 74 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 75

Introducing EVALUATE syntax Understanding FILTER Understanding FILTER


• Query tools such as DAX Studio are useful to author complex table • FILTER ( <table>, <condition> ) • The RedSales measure iterated over a subset of the Sales table—
expressions. • FILTER receives a table and a logical condition as parameters. namely the set of sales that are related to a red product.
• A common statement used to inspect the result of a table expression • As a result, FILTER returns all the rows satisfying the condition. • FILTER adds a condition to the existing conditions.
is EVALUATE. • FILTER is both a table function and an iterator at the same time. • For example, RedSales in the Audio row shows the sales of products
• In order to return a result, it scans the table evaluating the condition that are both of Audio category and of Red color.
on a row-by-row basis. In other words, it iterates the table. • It is possible to nest FILTER in another FILTER function.
• FILTER is often used to reduce the number of rows in iterations.
• For example, if a developer wants to
compute the sales of red products, F03 02.pbix

27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 76 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 77 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 78
27-02-2023

Introducing ALL and ALLEXCEPT Introducing ALL and ALLEXCEPT Introducing ALL and ALLEXCEPT
• Sometimes we want to extend the number of rows to consider for a certain • Imagine we need a report, which shows on the same row both the • The parameter of ALL cannot be a table expression. It needs to be either a
calculation (opposite of FILTER). sales amount and the percentage of the given amount against the table name or a list of column names.
• In that case, DAX offers a set of functions designed for that purpose: ALL, grand total. • What is its result if we use a column.
ALLEXCEPT, ALLCROSSFILTERED, ALLNOBLANKROW, and ALLSELECTED. • Sales Amount, All Sales Amount, Sales Pct F03 04.pbix • In that case, ALL returns all the distinct values of the column in the entire table.
• ALL returns all the rows of a table or all the values of one or more columns, • To compute the percentage, we divide the sales amount by the grand total. • Categories = ALL ( 'Product'[Category] )
depending on the parameters used. Thus, the formula must compute the grand total of sales even when the • ALL returns all the distinct values of the column in the entire table. F0305.pbix
• ProductCopy = ALL ( 'Product’ ) report is deliberately filtering one given category.
• We can specify multiple columns from the same table in the parameters of
• ALL is not necessary in a calculated table because there are no report filters • This can be obtained by using the ALL function. the ALL function.
influencing it. However, ALL is useful in measures. • ALL returns all the existing combinations of values in those columns F03 06.pbix
• ALL is extremely useful whenever we need to compute percentages or • Categories = ALL ( 'Product'[Category], 'Product'[Subcategory])
ratios because it ignores the filters automatically introduced by a report.

27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 79 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 80 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 81

Understanding VALUES, DISTINCT, and the Understanding VALUES, DISTINCT, and the
Introducing ALL and ALLEXCEPT
blank row blank row
• If we want to include most, but not all the columns of a table in an • F03 08.pbix • The engine automatically creates a blank row in any table that is on
ALL function call, we can use ALLEXCEPT instead. • ALL always returns all the distinct values of a column. On the other hand, VALUES the one-side of a relationship in case the relationship is invalid.
returns only the distinct visible values. • To demonstrate the behavior, remove all the silver colored products
• The syntax of ALLEXCEPT requires a table followed by the columns we • NumOfAllColors := COUNTROWS ( ALL ( 'Product'[Color] ) )
want to exclude. from the Product table. F03 09.pbix
• NumOfColors := COUNTROWS ( VALUES ( 'Product'[Color] ) ) • VALUES considers the blank row as a valid row, and it returns it.
• ALLEXCEPT is a way to write a DAX expression that will automatically • NumOfAllColors counts all the colors of the Product table, whereas NumOfColors • Only one blank row is added to the Product table, despite the fact that
include in the result any additional columns that could appear in the counts only the ones that—given the filter in the report—are visible. multiple different products referenced in the Sales table no longer have a
table in the future. • Use VALUES or DISTINCT in a calculated column or in a calculated table, then their corresponding ProductKey in the Product table.
• ALL ( 'Product'[Product Name], 'Product'[Brand], 'Product'[Class] ) behavior is identical to that of ALL because there is no active filter. • On the other hand, DISTINCT does not return it.
• ALLEXCEPT ( 'Product', 'Product'[ProductKey], 'Product'[Color] ) • On the other hand, when used in a measure, these two functions compute their • NumOfDistinctColors := COUNTROWS ( DISTINCT ( 'Product'[Color] ) )
result considering the existing filters, whereas ALL ignores any filter. • F03 10.pbix

27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 82 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 83 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 84

Understanding VALUES, DISTINCT, and the Understanding VALUES, DISTINCT, and the
Using tables as scalar values
blank row blank row
• A well-designed model should not present any invalid relationships. • ALL function always returns the blank row, if present. • A table with a single row and a single column can be used as if it were
• In case you need to remove the blank row from the result, then a scalar value.
• When dealing with invalid relationships, you need to be aware of this
behavior because otherwise you might end up writing incorrect ALLNOBLANKROW is the function. • F03 13.pbix the number of brands sliced by category and subcategory.
calculations. • The functions VALUES and DISTINCT only accept a single column as a • One might also want to see the names of the brands beside their
parameter. number.
• VALUES and DISTINCT also accept a table as an argument. In that • One possible solution is to use VALUES to retrieve the different brands and,
case, they exhibit different behaviors: instead of counting them, return their value. F03 14.pbix
• If one wants to list all the brands?
• DISTINCT returns the distinct values of the table, removing duplicates and not
considering the blank row. • An option is to iterate over the VALUES of Product[Brand] and use the
CONCATENATEX function, which produces a good result even if there are
• VALUES returns all the rows of the table, without removing duplicates, plus multiple values F03 16.pbix
the additional blank row if present.

27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 85 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 86 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 87
27-02-2023

Introducing ALLSELECTED Introducing ALLSELECTED Conclusions


• ALLSELECTED is useful when retrieving the list of values of a table, or • If one uses the slicer to reduce the number of categories shown, the • FILTER, ALL, VALUES and ALLSELECTED are extremely common
a column, as visible in the current report and considering all and only report still computes the percentage against all the sales. F03 18.pbix functions that appear in many DAX formulas.
the filters outside of the current visual. F03 17.pbix (Sales Pct) • If you want the percentage to be computed not against the grand • In the next chapters, we introduce evaluation contexts and the
• Because the denominator uses the ALL function, it always computes total of sales but rather only on the selected values, then CALCULATE function.
the grand total of all sales, regardless of any filter. ALLSELECTED becomes useful F03 19.pbix

27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 88 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 89 27-02-2023 Data Vizualization using Power BI. (c) Dr. Achint Nigam 90

You might also like