Advanced DAX Power BI Power Pivot
Advanced DAX Power BI Power Pivot
Course Notes
Leila Gharani
XelPlus.com
XelPlus.com
©Note: Copyright 2022, XelPlus.com, Leila Gharani, MA
Information
Course Notes for Advanced DAX for Excel Power Pivot
These course notes are accompanying documentation for my online course
Advanced DAX for Excel Power Pivot Course. Please do not reproduce or
transmit in any form without permission.
We have taken every effort to ensure the accuracy of this manual. In case
you discover any discrepancies, please send us a quick email to:
[email protected].
XelPlus.com 2
(Power) Pivot Table Tips
Update Pivot Table Default Layout: File / Options / Data / Make Changes to
the Default Pivot Tables
(Possible in Excel for Office 365)
Keep in Mind
XelPlus.com 3
DAX Engine Architecture
When you import tables to the Data Model, the data is stored in a Columnar
Database: Each column is stored separately and compressed. This is also
called the VertiPaq Storage Engine.
XelPlus.com 4
DAX Compression
Compressing data has several advantages:
The reduced size of the data occupies less memory in the model.
The time to scan the data is reduced.
Different compression techniques (i.e., algorithms) are applied to the
columns based on the column’s data type, data pattern, etc.
XelPlus.com 5
DAX Iterator Functions
Iterator functions are functions that operate on each row of a table.
Examples of iterator functions include DAX aggregation functions, such as:
• SUMX
• AVERAGEX
• COUNTX
Using the “X” versions of these functions is required when you want to
process (i.e., iterate) through each row in a table, performing a calculation,
then aggregating all results into a single result.
An example of this is multiplying two columns (ex, sales and quantity) against
one another, then summing the results into a single total.
XelPlus.com 6
Evaluation Context
Understanding Evaluation Context is Crucial for Your Success
Context is used to determine how a formula evaluates data and produces a
result. There are two types of Contexts:
• Filter Context
• Row Context
Filter Context
The value presented is the result of having filtered the “Sales” table using
three separate filters.
[Department] = “Creative Arts”
[Month] = “February”
[State/Region] = “California”
Filters “transfer” from the one side of the relationship to the many side.
Not the other way round!
XelPlus.com 7
Row Context
Row Context is the knowledge of the current row.
The question is, “Which row?”
The row in question can come from the underlying tables or from a table
constructed by a formula.
Row Context exists in Calculated Columns & Measures that include
iterator functions.
XelPlus.com 8
Common DAX Functions
RELATED: This function gives you access to related columns. Related
columns are columns in your lookup / dimension tables that have a
relationship to your fact table.
The Related Function is commonly used in iterator functions that need to
access columns from other tables.
It’s also used in calculated columns.
DIVIDE: It’s safer to use the DIVIDE function than the division operator.
DIVIDE can automatically handle division by zero or blank. You can also
define an alternate value directly in the function.
VALUES: This function returns a unique list of values based on the filter
context of your Pivot Table. This is a great function to use inside other
functions to perform calculations. For example, you can calculate average
daily sales with:
ALL: The purpose of the ALL function is to return all rows from a table, or all
rows in a column, ignoring any filters that might have been applied. This
function is also typically used as a “remove filters” function inside
CALCULATE.
XelPlus.com 9
FILTER Function
FILTER serves as a TABLE function but also as an iterator function
FILTER scans a table and performs row-by-row evaluations.
The FILTER function can take a full or partial table and reduce its rows based
on criteria. It filters a table.
FILTER SYNTAX
=FILTER(<table>, < filter>)
The <table> can be a full table from the Data Model or a table that is the
result of an expression (i.e., formula).
The <filter> is a Boolean expression that is evaluated on each row of the
supplied table. For example, [Sales] > 500 or [Country] = “Austria”.
XelPlus.com 10
FILTER Function & Context
How does this function work?
COUNTROWS (
FILTER (
ALL ( dProduct ),
dProduct[RetailPrice]
> MAX ( dProduct[RetailPrice] )))
The red portion of the function is based on the “initial” filter context
created by the Pivot Table.
This means the Product Table is filtered to Sporting Goods and then the
Max retail price is calculated. This returns 103.98.
The ALL(dProduct) part of the function expands the Products Table and
dProduct[RetailPrice] is the row by row iteration. Each value is
compared to 103.98. There is only 1 value greater than this: 689.00.
XelPlus.com 11
CALCULATE Function
CALCULATE returns a Single Value
CALCULATE is the Only DAX function that can completely change the filters
coming from the Pivot Table.
CALCULATE is NOT an ITERATOR Function (although you can use Iterator
functions inside CALCULATE).
CALCULATE uses Relationships to access related tables.
CALCULATE SYNTAX
<expression> is calculated LAST. First all filters are applied and THEN the
expression is calculated.
XelPlus.com 12
CALCULATE Evaluation Steps
These are the evaluation steps for CALCULATE:
1 Filters Created by the Pivot Table are saved: Initial Filter Context.
4 Once this “invisible Pivot Table” fields are added to the Pivot Table,
the first argument in CALCULATE is calculated.
XelPlus.com 13
Context Transition
Context Transition is an important DAX Concept
Context Transition is the process by which all existing Row Contexts are
transformed into an equivalent Filter Context.
XelPlus.com 14
Use Variables for More Clarity
The VAR keyword precedes the definition of a variable.
You can use multiple variables in a measure but when using variables, you
must use a RETURN statement to specify the final output to be returned.
Where:
<name> The name you want to use for the variable.
<expression>: A DAX expression which returns a scalar or table value.
<output_expression>: A DAX expression which returns a scalar or table
value.
A variable
“stores the result of an expression as a named variable,
which can then be passed as an argument to other
measure expressions. Once calculated, the resultant
values for a variable expression do not change, even if the
variable is referenced in another expression.”
XelPlus.com 15
DAX Studio
DAX Studio is a free add-in you can download and install to:
Install DAX Studio: DAX Studio - The ultimate client tool for
working with DAX queries
XelPlus.com 16
THANK YOU!
Please take a few seconds to
leave a review for the course.
Your support is very much
appreciated.
XelPlus.com 17
More Learning…
If you’d like to learn Power Pivot & DAX from scratch and learn
how to create dashboards along the way, check out the
complete Power Pivot & DAX course at www.XelPlus.com
XelPlus.com 18