100% found this document useful (1 vote)
68 views

Introduction to DAX in Power BI

DAX (Data Analysis Expressions) is a formula language used in Power BI for data analysis, enabling users to create calculated columns, measures, and custom tables. Key concepts include row and filter context, calculated columns versus measures, and various DAX patterns for business scenarios. The document also provides a step-by-step tutorial for beginners on using DAX in Power BI, covering functions, calculations, and practical examples.

Uploaded by

Saadie Essie
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
100% found this document useful (1 vote)
68 views

Introduction to DAX in Power BI

DAX (Data Analysis Expressions) is a formula language used in Power BI for data analysis, enabling users to create calculated columns, measures, and custom tables. Key concepts include row and filter context, calculated columns versus measures, and various DAX patterns for business scenarios. The document also provides a step-by-step tutorial for beginners on using DAX in Power BI, covering functions, calculations, and practical examples.

Uploaded by

Saadie Essie
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/ 18

DAX (Data Analysis Expressions) in power Bi

 DAX (Data Analysis Expressions) is a powerful formula language used in


Power BI, Excel, and SQL Server Analysis Services (SSAS) to perform data
analysis and create calculated columns, measures, and custom tables.
Understanding DAX is essential for anyone looking to build advanced
analytics solutions within Power BI.
Key Concepts in DAX
1. Context

“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.

Total Revenue = Sales_table[Price] * Sales_table[Quantity]

 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

Total Sales by Region = CALCULATE(SUM(Sales[Amount]), Sales[Region] =


"North")

Total Sales by Product = CALCULATE(SUM(Sales[Amount]), FILTER(Sales,


Sales[Product] = "Electronics"))

Page 1 of 18
Total Sales All Products = CALCULATE(SUM(Sales[Amount]),
ALL(Sales[Product]))

Total Sales by Product Category = CALCULATE(SUM(Sales[Amount]),


RELATED(Product[Category]))
2. Calculated Columns and Measures

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:

Total Sales column = Sales[Amount] * Sales[Quantity]

When to use calculated columns:

 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:

Total Sales Measure = SUM(Sales[Amount])

When to use calculated measures:

 Cases where you need to perform simple calculations instantly.

 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

Using common DAX patterns to apply to real-world business scenarios.

Ranking: Use the RANKX function to rank items based on a specific measure or
criteria.

 Example: Rank the top 5 sales regions by total sales.


Top 5 Sales Regions = RANKX(Sales, Sales[Total Sales], , DESC)

Page 2 of 18
Running Total: Calculate running totals or cumulative sums using CALCULATE
and time intelligence functions.

 Example: Calculate the running total of sales for each region.

Running Total Sales = CALCULATE(SUM(Sales[Amount]), FILTER(ALL('Date'),


'Date'[Date] <= TODAY()))

Moving Average: Compute moving averages using AVERAGEX and time


intelligence functions.

 Example: Calculate the moving average of sales for the last 3 months.

Moving Average Sales = AVERAGEX(FILTER(ALL('Date'), 'Date'[Date] >=


TODAY() - 90), Sales[Amount])

Segmentation: Apply dynamic segmentation using CALCULATE and nested IF


statements to categorize data into segments.
 Example: Segment customers based on their purchase history.

Customer Segments = CALCULATE(IF(COUNT(Sales[Customer]) > 5, "Frequent


Buyer", IF(COUNT(Sales[Customer]) > 2, "Occasional Buyer", "New Customer"))

ABC Analysis: Conduct ABC analysis using RANKX and SWITCH functions to
classify items into different groups.

 Example: Classify products based on their sales revenue.

Product Classification = SWITCH(RANKX(Sales, Sales[Revenue], , DESC), 1, "A", 2,


"B", 3, "C")

Budget Allocation: Allocate budgets or targets across different periods or


dimensions using CALCULATE and time intelligence functions.

 Example: Allocate a budget across different regions.

Budget Allocation = CALCULATE(SUM(Sales[Amount]), FILTER(ALL('Date'),


'Date'[Date] <= TODAY()))
5 Most frequently used DAX functions in Power BI
1. Statistical Functions

Statistical functions provide insights into data distribution and totals.

COUNT tallies the number of cells containing numerical or non-blank data

Number of Sales = COUNT(Sales[Amount])

DISTINCTCOUNT counts the unique values.

Disinct Number of Sales = DISTINCTCOUNT(Sales[Amount])

Page 3 of 18
AVERAGE computes the mean of a column’s values, and its counterpart,

Average Order Quantity= AVERAGE(Sales[Quantity])

SUM computes the mean of a column’s values.

Total Order Quantity = SUM(Sales[Quantity])

DIVIDEspecifically developed for safe division, handles division by zero elegantly


by returning an alternative result or BLANK.

Average Order Value= DIVIDE(Total Revenue,Total Order Quantity)


2. Date & Time Intelligence Functions

 CALENDAR(<start_date>, <end_date>) The CALENDAR function generates


a table with a single column of dates, starting from a specified start date and
ending at a specified end date.

Calendar Table = CALENDAR(DATE(2020, 1, 1), DATE(2020, 12, 31))

 DATE(<year>, <month>, <day>) Returns the specified date in datetime


format.

Date Value = DATE(2020, 6, 15)

 DATEDIFF(<date_1>, <date_2>, <interval>) Returns the number of units


between two dates as defined in <interval>.

Days Since Last Purchase = DATEDIFF(Sales[Date], TODAY())

 DAY(<date>) The DAY function returns the day of the month from a
specified date.

Day of Month = DAY(DATE(2020, 6, 15))

 MONTH(<date>) Returns a number from 1 to 12 representing a month.

Month of Year = MONTH(DATE(2020, 6, 15))

 TOTALYTD(<expression>, <dates>[, <filter>][, <year_end_date>]) Evaluates


the year-to-date value of the expression in the current context.

Total Sales YTD = TOTALYTD(SUM(Sales[Amount]), 'Date'[Date])


3. Filter Functions

 FILTER(<table>, <filter>) Returns a table that is a subset of another table or


expression.

Filtered Sales = FILTER(Sales, Sales[Amount] > 100)

 CALCULATE(<expression>[, <filter1> [, <filter2> [, …]]]) Evaluates an


expression in a filter context.

Page 4 of 18
Total Sales by Region = CALCULATE(SUM(Sales[Amount]), Sales[Region] =
"North")

 ALL([<table> | <column>[<column>[<column>[,…]]]]) Returns all the rows


in a table, or all the values in a column, ignoring any filters that might have
been applied.

All Sales = ALL(Sales)

 RELATED(<table>[column]): Returns related tables

Total Sales by Product Category = CALCULATE(SUM(Sales[Amount]),


RELATED(Product[Category]))
4. Table Manipulation Functions

 SUMMARIZE(<table>,
<groupBy_columnName>[<groupBy_columnName>]…[, <name>,
<expression>]) Returns a summary table for the requested totals over a set of
groups.

Summarized Sales = SUMMARIZE(Sales, Sales[Region], "Sum of Sales",


SUM(Sales[Amount]))

 DISTINCT(<table>) Returns a table by removing duplicate rows from another


table or expression.

Distinct Regions = DISTINCT(Sales[Region])

 ADDCOLUMNS(<table>, <name>, <expression>[, <name>,


<expression>]…) Adds calculated columns to the given table or table
expression.

Sales with Margin = ADDCOLUMNS(Sales, "Margin", Sales[Amount] * 0.2)

 GROUPBY(<table> [, <groupBy_columnName>[, [<column_name>]


[<expression>]]…) Create a summary of the input table grouped by specific
columns.

Grouped Sales = GROUPBY(Sales, Sales[Region])

 UNION(<table>, <table>[, <table> [,…]]) Returns the union of tables with


matching columns.

Combined Sales = UNION(Sales, Sales2)


5. Logical Functions

 IF(<logical_test>, <value_if_true>[, <value_if_false>]) Checks a condition,


and returns a certain value depending on whether it is true or false.

Is High Value = IF(Sales[Amount] > 100, "High", "Low")

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)

 NOT(<logical>) Changes TRUE to FALSE and vice versa.

Is Not High Value = NOT(Sales[Amount] > 100)

 SWITCH(<expression>, <value>, <result>[, <value>, <result>]…[,


<else>]) Evaluates an expression against a list of values and returns one of
possible results

Filtered Sales = FILTER(Sales, SWITCH(Sales[Region], "North", "North Region",


"South", "South Region", "Other", "Unknown") == "North Region")

 ISBLANK(<value>) // ISERROR(<value>) Returns whether the value is


blank // an error.

IFERROR(Sales[Amount], 0) //iserror returns 0 in case of any errors


IF(ISBLANK(Sales[Region]), "Unknown", Sales[Region]) //isblank returns unknown
if it finds blank values
6. Other Functions

1. CONCATENATE(): Concatenates two or more strings.

Full Name = CONCATENATE(Customer[First Name], " ", Customer[Last Name])

2. FORMAT(): Formats a value as a string.

Formatted Date = FORMAT(Sales[Date], "MMM dd, yyyy")

3. LEN(): Returns the length of a string.

Length of Product Name = LEN(Product[Name])

4.LOWER(): Converts a string to lowercase.

Lowercase Product Name = LOWER(Product[Name])

5.UPPER(): Converts a string to uppercase.

Uppercase Product Name = UPPER(Product[Name])

Page 6 of 18
Step-by-step tutorial for beginners, learn how to use DAX (Data Analysis
Expressions) in Microsoft Power BI.

 DAX is a formula language with functions. If you’ve ever written


formulas in Excel, you’ll find that DAX is very familiar.
How to use Power BI DAX - Tutorial

https://www.youtube.com/watch?v=waG_JhBgUpM

 0:00 Introduction

 0:49 Import data & create data model

 3:25 Define relationships

 5:14 Create new measure using sum function

 10:22 Format measures

 11:44 Edit & delete measures

 12:33 Create new measure using count function

 14:30 Create new measure using distinct function

 16:02 Add comments to measures

 16:56 Create new measure with operators

 19:08 Quick Measure tool

 20:36 Create calculated column

 21:38 Measure vs. calculated column

 22:07 Measure that references another measure

 24:19 Iterator functions

 27:32 Time & date functions

 30:12 Find function

 32:38 If function

 33:54 Calculate 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

check on the right side the 3 datasets are loaded.

Click model tab to check relationship

Next step > start creating some measures. to run a calculation across the model.

how many total cookies did we sell or how many units?

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.

Distinct Customers = DISTINCTCOUNT (Orders [Customer ID])

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:

profit margin%=[total profit] /sum(orders[orders[Revenue])

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 Syntax: SUMX(<table>, <expression>)

o Example:

dax

Total Sales = SUMX (Sales, Sales [Quantity] * Sales [Unit Price])

Next step: select cookie type table > right click select new measure

Type

Total profit 2 = SUMX ('Cookie Types’, ‘Cookie Types'[Units Sold] *'Cookie


Types'[Revenue Per Cookie]-'Cookie Types'[Cost Per Cookie])

Page 16 of 18
Results total profit 2 for cookie rows.

Next function to learn is: Find function


Insert new column using order table >home ribbon> table tools>new
column.
Type
has chocolate = find("chocolates”, Orders[Product],1,0)

Page 17 of 18
Using IF function

To filter which product has chocolate or No chocolate.

Type

has chocolate = if(find("chocolate",Orders[Product],1,0)>0,"has chocolate","No


chocolate")

Page 18 of 18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy