Introduction to DAX in Power BI
Introduction to DAX in Power BI
“Context Matters” especially in the case of DAX functions. There are mostly two
types of contexts: row context and filter context
Row Context: In row context, we take into account a specific row that has
been filtered in a DAX expression. This context only performs the operation
on the current row.
Filter Context: In filter context, a set of filters can be applied on the data
model and that will determine what calculations get through for the visual.
Examples
Page 1 of 18
Total Sales All Products = CALCULATE(SUM(Sales[Amount]),
ALL(Sales[Product]))
Let’s delve a little into the implementation of DAX and see how Power BI DAX
formulae are used in Measures and Calculated Columns calculations.
Calculated Column
It's a created column whose values are calculated based on a DAX function. The
calculated column is part of the table and is stored in the data model.
Example:
Cases where you need to write multiple steps because of the complexity of
logic/calculations.
Cases where you need to create a new column that will be used for filtering,
grouping, or sorting.
Calculated Measure:
A measure is a calculation that is defined using a DAX formula, but it is not stored in
the data model. Instead, it is calculated on the fly when it is used in a report.
Example:
Cases where you create a calculation that just should be used inside the
report, and not in filters, or sorting/grouping options.
3. DAX Patterns
Ranking: Use the RANKX function to rank items based on a specific measure or
criteria.
Page 2 of 18
Running Total: Calculate running totals or cumulative sums using CALCULATE
and time intelligence functions.
Example: Calculate the moving average of sales for the last 3 months.
ABC Analysis: Conduct ABC analysis using RANKX and SWITCH functions to
classify items into different groups.
Page 3 of 18
AVERAGE computes the mean of a column’s values, and its counterpart,
DAY(<date>) The DAY function returns the day of the month from a
specified date.
Page 4 of 18
Total Sales by Region = CALCULATE(SUM(Sales[Amount]), Sales[Region] =
"North")
SUMMARIZE(<table>,
<groupBy_columnName>[<groupBy_columnName>]…[, <name>,
<expression>]) Returns a summary table for the requested totals over a set of
groups.
Page 5 of 18
AND(<logical 1>, <logical 2>) Checks whether both arguments are TRUE,
and returns TRUE if both arguments are TRUE. Otherwise, it returns FALSE.
Is High Value and Recent = AND(Sales[Amount] > 100, Sales[Date] > TODAY() - 30)
OR(<logical 1>, <logical 2>) Checks whether one of the arguments is TRUE to
return TRUE. The function returns FALSE if both arguments are FALSE.
Is High Value or Recent = OR(Sales[Amount] > 100, Sales[Date] > TODAY() - 30)
Page 6 of 18
Step-by-step tutorial for beginners, learn how to use DAX (Data Analysis
Expressions) in Microsoft Power BI.
https://www.youtube.com/watch?v=waG_JhBgUpM
0:00 Introduction
32:38 If function
36:39 Wrap up
Start of the Exercise
Step 1: have the Dax for cookies dataset containing three tables
Cookie types, customers, orders.
Page 7 of 18
https://www.kaggle.com/datasets/antishar/dax-for-cookies-dataset
Next step > start creating some measures. to run a calculation across the model.
Click report view then Select order> right click> and select new measure.
Page 8 of 18
Delete the measure function and customize with new function.
Page 9 of 18
Select > total number of units sold (the new measure we have created)
Tick the check box for new measure created (Total number of units sold)and the
visual bar graph will appear.
Next click on >customer table> select name.> then click Matrix visualization from
graph icons.
Page 10 of 18
Next remove fraction numbers to whole values.> Total number of units sold > to
activate the measure tools > then general format to whole number.
Page 11 of 18
count of orders = COUNTROWS (orders) click new function count of orders.> result
is 700.
Next choose > still on order >right click select measure > insert another measure.
Page 12 of 18
How to add a comment on measure >click select> Distinct Customers> to activate
Distinct measure function > then select + enter key to insert new line space. //
Page 13 of 18
Next > click home> to insert Quick measure> Calculation > select functions
Page 14 of 18
How to add Profit margin%
Profit margin is a financial metric that indicates the percentage of revenue that
exceeds the costs of goods sold (COGS) and operating expenses. It is a measure of a
company's profitability and efficiency in managing its expenses relative to its
revenue.
Select order > choose new measure> on the function formula type:
Next> select cookie type and profit margin% > click path margin % to activate
measure tool > then convert profit margin values to %.
Page 15 of 18
iterator functions
Iterator functions
In Power BI, iterator functions are used to perform row-by-row calculations over a table.
These functions are particularly useful in DAX (Data Analysis Expressions) for
performing complex aggregations or calculations that depend on the context of each
row.
Common Iterator Functions
1. SUMX: Sums the result of an expression evaluated for each row in a table.
o Example:
dax
Next step: select cookie type table > right click select new measure
Type
Page 16 of 18
Results total profit 2 for cookie rows.
Page 17 of 18
Using IF function
Type
Page 18 of 18