Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Create DAX

calculations in
Power BI Desktop
aka.ms/PL300-4

© Copyright Microsoft Corporation. All rights reserved.


Learning objectives:
• DAX calculations
Agenda • Advanced DAX
• Optimize DAX performance

© Copyright Microsoft Corporation. All rights reserved.


DAX calculations

© Copyright Microsoft Corporation. All rights reserved.


What is DAX?

• Data Analysis Expressions


• Library of functions and operators
• Build formulas and expressions
• Create calculated measures, columns,
and tables

© Copyright Microsoft Corporation. All rights reserved.


Calculated measures

Use DAX to create measures


Measures are only calculated when used.

Results are not stored in the data model.

Responsive to interactions, like filters.

Identified with calculator icon.

© Copyright Microsoft Corporation. All rights reserved.


Implicit vs. explicit vs. quick measures

© Copyright Microsoft Corporation. All rights reserved.


Calculated columns

© Copyright Microsoft Corporation. All rights reserved.


Calculated tables

© Copyright Microsoft Corporation. All rights reserved.


Columns vs. measures

Calculated columns: Measures:


• Create a value for each row in a table. • Calculate on demand.
• Store values in the Power BI .pbix file. • Calculated based on filters.

© Copyright Microsoft Corporation. All rights reserved.


DEMO
Create measures using DAX in Power BI

© Copyright Microsoft Corporation. All rights reserved.


Lab: Create DAX calculations in Power BI Desktop (45 minutes)
Create DAX calculations | GitHub Exercise

In this lab you’ll create calculated tables, calculated


columns, and simple measures using Data Analysis
Expressions (DAX).

In this lab you learn how to:


• Create calculated tables
• Create calculated columns
• Create measures

© Copyright Microsoft Corporation. All rights reserved.


Knowledge check: DAX calculations

Are measures or calculated columns calculated on demand?

Why would you choose a calculated column instead of a measure?

© Copyright Microsoft Corporation. All rights reserved.


Advanced DAX

© Copyright Microsoft Corporation. All rights reserved.


Understanding filter context

© Copyright Microsoft Corporation. All rights reserved.


CALCULATE() function

Total Sales for 2015 =


CALCULATE (
    SUM ( 'Sales
OrderDetails'[Total Price] ),
    YEAR ( 'Sales
OrderDetails’[OrderDate] ) =
2015
)

© Copyright Microsoft Corporation. All rights reserved.


Use inactive relationships

Sales by Ship Date =


CALCULATE (
    Sales[TotalPrice],
    USERELATIONSHIP ( 'Calendar'[Date], Sales[ShipDate] )
)

© Copyright Microsoft Corporation. All rights reserved.


Semi-additive measures

Last Sales Quantity =


CALCULATE(
    SUM(Sales[Quantity]),
    LASTDATE('Date'[Date])
)

© Copyright Microsoft Corporation. All rights reserved.


Create a common date table

© Copyright Microsoft Corporation. All rights reserved.


Time intelligence Total Sales Previous Month =
CALCULATE ( SUM ( Sales[Total
Price] ), PREVIOUSMONTH
( 'Date'[Date] ) )

© Copyright Microsoft Corporation. All rights reserved.


Lab: Advanced DAX calculations in Power BI Desktop (45 min)
Advanced DAX | GitHub Exercise

In this lab, you’ll create measures with DAX


expressions involving filter context
manipulation.
In this lab you learn how to:
• Use the CALCULATE() function to
manipulate filter context
• Use Time Intelligence functions

© Copyright Microsoft Corporation. All rights reserved.


Knowledge check: Advanced DAX

What DAX function lets you use inactive relationships?

Which type of functions specifically use dates for calculations?

What kind of measure sums for one dimension and uses another aggregation for a
different dimension?

© Copyright Microsoft Corporation. All rights reserved.


Optimize DAX performance

© Copyright Microsoft Corporation. All rights reserved.


Variables improve performance and troubleshooting

Without variable:
Sales YoY Growth =
DIVIDE (
( [Sales] - CALCULATE ( [Sales], PARALLELPERIOD ( 'Date'[Date], -12, MONTH ) ) ),
       CALCULATE ( [Sales], PARALLELPERIOD ( 'Date'[Date], -12, MONTH ) ) )

With variable:
Sales YoY Growth =
VAR SalesPriorYear = CALCULATE ( [Sales], PARALLELPERIOD ( 'Date'[Date], -12, MONTH ) )
VAR SalesVariance = DIVIDE ( ( [Sales] - SalesPriorYear ), SalesPriorYear )
RETURN
  SalesVariance

© Copyright Microsoft Corporation. All rights reserved.


Performance tuning reports

© Copyright Microsoft Corporation. All rights reserved.


Analyze query plans

Count Customers = Count Customers =


CALCULATE ( CALCULATE (
    DISTINCTCOUNT ( Order[ProductID] ),     DISTINCTCOUNT ( Order[ProductID] ),
    FILTER ( Order, Order[OrderQty] >= 5 )     KEEPFILTERS (Order[OrderQty] >= 5 )
) )

© Copyright Microsoft Corporation. All rights reserved.


DEMO
Optimize model performance in Power BI Desktop

© Copyright Microsoft Corporation. All rights reserved.


• DAX calculations
Recap • Advanced DAX
• Optimizing DAX performance

© Copyright Microsoft Corporation. All rights reserved.


References
Write DAX formulas for Power BI Desktop models
Add measures to Power BI Desktop models
Use DAX time intelligence functions in Power BI Desktop models

© Copyright Microsoft Corporation. All rights reserved.

You might also like