Power-BI-DAX-Essentials_AMZ_Manual_Snippet
Power-BI-DAX-Essentials_AMZ_Manual_Snippet
Contents
Chapter 1: Getting to Know Data Analysis Expression (DAX) language 6
What is DAX? 6
Perquisites: 7
Where is DAX used? 8
What are the Data types in DAX 9
Numerical data types: 9
Non-numerical data types: 9
Variant Data Type: 9
DAX type handling/operator overloading 10
Addition of two “string” inputs: 10
Concatenation of two “numbers”: 10
DAX as Formula Language 11
Vertipaq engine in DAX 12
Value Encoding: 12
Dictionary or Hash Encoding: 12
Run Length Encoding (RLE): 12
Factors Affecting Model Compression: 12
Difference between DAX and EXCEL functions 14
Difference between DAX and M 15
Best Practices in DAX 16
Dos, and Don'ts and Best Practices in DAX 17
Chapter 2: Data Modeling Recap 19
What is Data Model 20
Tables in a Data Model 21
Fact Table / Data Table 21
Dimension Table / Lookup Table: 21
Fact vs Dimensions 22
Schemas in Data Model 23
Star Schema 23
Snowflake Schema 26
Why star Schema is Preferred 27
Filter Propagation 28
Interpreting Mechanism for Filtering 29
Using Multiple Filters together 30
Data Denormalization 31
Relationship Characteristics 32
Cardinality 32
Filter Direction 32
Page | 1
TABLE OF CONTENTS
Page|2
TABLE OF CONTENTS
Page|3
TABLE OF CONTENTS
Page|4
TABLE OF CONTENTS
Page|5
TABLE OF CONTENTS
Page|6
CHAPTER 5: EVALUATION CONTEXT IN DAX
Page|7
CHAPTER 5: EVALUATION CONTEXT IN DAX
Row Context:
When you write an expression in a calculated column, the expression is evaluated for
each row of the table, creating a row context for each row. In a similar manner, when you use
an iterator like FILTER(), SUMX(), AVERAGEX(), ADDCOLUMNS(), or any one of the DAX
functions that iterates over a table or a table expression, it creates a row context.
A row context is evaluated whenever an expression iterates over a table. Row context
does not follow relationships and does not create a filter context. DAX functions are used to
covert a row context into a filter context.
A row context exists in either a calculated column or an iterating function such as
SUMX(). It does not exist in a regular measure. This is because regular measures cannot be
created using naked columns. Regular measures only operate on columns and tables so the
row filtering does not come into play using measures unless the iterator function is used.
Filter Context:
Filter context is evaluated based on filtering applied on the data model in DAX. The
Initial/Incoming filter context comes from
✔ Slicers
Page|8
CHAPTER 5: EVALUATION CONTEXT IN DAX
Page|9
CHAPTER 5: EVALUATION CONTEXT IN DAX
Notice that no discount has been provided using the selected filter values.
Notice the change in values. This means that the Unit price discount was applicable
for purchases through resellers only.
P a g e | 10
CHAPTER 5: EVALUATION CONTEXT IN DAX
P a g e | 11
CHAPTER 5: EVALUATION CONTEXT IN DAX
P a g e | 12
CHAPTER 5: EVALUATION CONTEXT IN DAX
✔ RELATED(): This function expands the row context of the current row and allows
you to perform VLOOKUP to include values in a column.
✔ FILTER(): This function allows you to specify a subset of data to include in the
current context.
✔ ALL(): This function sets a new context by ignoring the existing filter context.
✔ ALLEXCEPT(): This function lets you remove all the filter except the specified one.
Thereby, changing the existing filter context.
✔ EARLIER() or EARLIEST(): These functions let you perform calculations in loop by
referencing to a value in the inner loop.
P a g e | 13
CHAPTER 5: EVALUATION CONTEXT IN DAX
Rules of Evaluation
When a DAX expression is evaluated there are a series of rules that are executed in
order. They form the foundation of how you will use DAX functions.
The Rules for evaluation of DAX Expression are:
Evaluate the result after catering the effect of the above mentioned filters.
P a g e | 14
CHAPTER 6: RELATED FUNCTIONS AND CONTEXT TRANSITION
P a g e | 15
CHAPTER 6: RELATED FUNCTIONS AND CONTEXT TRANSITION
Using RELATED()
Now that you’re familiar with the basic DAX functions, lets look at some functions
which can help you fetch data from other tables.
Continue using the previous file for the exercise or open the file
Related_Functions_and_Context_Transition_1.pbix..
RELATED() takes value from the one side of the one-to-many relationship and
populates it to the many side of the relationship. To use RELATED() for bringing the list
price value from the Product table into the Sales table:
1. Right click on the Sales table in the Fields Pan.
2. Click on New Column.
Column tools open.
3. Type the following formula in the formula bar.
Notice that the intellesence detects that currently List Price does not exist in Sales
Table. So intellisense does not give option to select a column from the Product table. We will
use RELATED() function to activate the relationship.
4. Type RELATED in the formula bar after =.
5. Select the List Price from the Product table.
Your DAX expression should look similar to the expression below.
In this Expression:
⮚ A calculated column with each row containing the respective List Price from
the Product table has been created.
6. Click on Data tab in the left Ribbon.
7. Click on Sales table in the Fields.
P a g e | 16
CHAPTER 6: RELATED FUNCTIONS AND CONTEXT TRANSITION
P a g e | 17
CHAPTER 6: RELATED FUNCTIONS AND CONTEXT TRANSITION
Using RELATEDTABLE()
RELATEDTABLE() takes values from the many side of the one-to-many relationship
and populates the one side of the relationship.
Continue using the previous file for the exercise or open the file
Related_Functions_and_Context_Transition_2.pbix..
Since there are multiple values for RALTEDTABLE() takes the table name as the
input argument.
In this Expression:
⮚ The rows in the Sales table have been counted.
4. Click on Data tab in the left Ribbon.
5. Click on Customer table in the Fields pan.
As you can see, row context does not follow relationship. RELATEDTABLE() allows
us to traverse the chain of relationship.
6. Click on the Number of Sales (CC) column.
Column tool box appears containing the formula bar.
7. Add RELATEDTABLE() before the sales in the formula.
Your DAX expression should look similar to the expression below.
P a g e | 18
CHAPTER 6: RELATED FUNCTIONS AND CONTEXT TRANSITION
In this Expression:
⮚ The related number of rows from the sales table are counted based on the
incoming filter context.
Notice the change of values in the Number of Sales (CC) column.
The Number of Sales (CC) gives the number of Sales transactions by each customer.
P a g e | 19
CHAPTER 6: RELATED FUNCTIONS AND CONTEXT TRANSITION
P a g e | 20
CHAPTER 6: RELATED FUNCTIONS AND CONTEXT TRANSITION
In this Expression:
⮚ The unique product keys from the Sales table are counted.
4. Click on Data tab in the left Ribbon.
5. Click on Customer table in the Fields pan.
6. Click on the drop box at the top on Number of Products Purchased (CC).
7. Click on Sort Descending.
The result is not correct because all of the customers cannot buy the same number of
products. However, CALCULATE() allows us to get the result. Calculate converts Row
context into filter context. This is called context transition. CALCULATE() will be
discussed in more detail later in the course.
8. Click on the Number of Products Purchased (CC) column.
Column tool box appears containing the formula bar.
9. Add CALCULATE() before the DISTINTCOUMT() in the formula and press
Enter.
Your DAX expression should look similar to the expression below.
P a g e | 21
CHAPTER 6: RELATED FUNCTIONS AND CONTEXT TRANSITION
In this Expression:
⮚ The filter context is modified by CALCULATE().
10. Click on the drop box at the top on Number of Products Purchased (CC).
11. Click on Sort Descending.
In the example below, you can see Nancy Chapman had 57 sales but only bought 22
different products.
In this Expression:
⮚ The MAX() function finds the latest order date in the Sales Table.
⮚ CALCULATE() modifies the filter context so that the last order date for each
customer can be filtered.
15. Format the Last Order Date (CC) column to (m/d/yy).
16. Click on Data tab in the left Ribbon.
17. Click on Customer table in the Fields pan.
P a g e | 22
CHAPTER 6: RELATED FUNCTIONS AND CONTEXT TRANSITION
It is important to format the newly created columns to the format used in your organization.
For this course we have followed the m/d/yy format for the date.
P a g e | 23
CHAPTER 6: RELATED FUNCTIONS AND CONTEXT TRANSITION
Row context does not follow relationships but filter context does follow relationships.
So CALCULATE() take the current row, transform it into a filter context and after
CALCULATE transitions to the new filter context (before the expression in CALCULATE is
evaluated) the CustomerKey value will filter records in the Sales table.
P a g e | 24