Using Basic Table Functions
Using Basic Table Functions
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
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