Marco Russo
marco@[Link]
DAX Optimization Examples
Thanks to our Sponsors!
We write We teach We provide We are recognized
Books Courses Consulting BI Experts
Remote
Consulting
Power BI/SSAS
Optimization
BI Architectural
Review
On-Site
Consulting
Custom Training
& Mentoring [Link] lb [Link] m
Agenda
o Optimize reports step-by-step
o In the meantime find time to:
• Learn the basics of tools to measure performance
• Understand the way DAX solves your queries
• Learn about Formula Engine and Storage Engine
• Measure performance and detect bottlenecks
• Reproduce the issue and measure it
• Solve the problems, one at a time
• No “wow effects”… we simulate real-world examples
Open a slow report for the first time…
First look
1 2 3
4
5
Where is my problem?
o A simple report like this, is already too complex
o Three measures:
• Amount USD, Amount EUR, Open Orders
o Different ways of slicing and combining values
o Trial and error is not the way to go
DAX Studio
o [Link]
• Free add-in for Excel
• Standalone executable for
Tabular and Power BI
o It can query
• Analysis Services databases
• Power Pivot data models
• Power BI Desktop data models
• Both DAX and MDX queries
Main Features
o DAX Query Editor
• Full metadata available
• IntelliSense enabled
• Format DAX code
• Through [Link] website
o Catch Query Plan and Server Timings
o Save query plan and timings for later analysis
Demo
DAX Studio
Capturing Power BI queries using DAX Studio
o Create an empty page in Power BI
o Save Power BI file selecting empty page as the current one
o Close and open Power BI file
o Connect DAX Studio to Power BI
o Activate trace for All Queries in DAX Studio
o Switch report page in Power BI
o [Link]
Know your data model first
o Do you know..
• The size in memory of each column
• The cardinality of each table
• The cardinality of each column
• Hint: they are not the same!
o VertiPaq Analyzer
• Simplify extraction of this data from DMVs
• [Link]
Demo
VertiPaq Analyzer
Focus on DAX to search for a better way to write currency conversion
Currency Conversion
Following best practices
o Best practices are always helpful
• there are not so many in DAX
• it is easier to follow them
o Use variables as much as you can to fix values
o Avoid computing the same value multiple times
o Make it simpler, then simpler, then… simpler
Fighting with the cache
o There are multiple cache levels
• they are your enemies as of now
o Power BI cache
• Useful when authoring reports
• Avoids executing the query at all if not needed
o DAX cache
• Caches results of internal VertiPaq queries (more on this
later)
• Can be cleared from withing DAX Studio
o Avoid the cache to measure performance
Create an empty page in Power BI
o An empty page lets you start Power BI Desktop without
computing any report
o Open Power BI on the empty page
o Open DAX Studio and connect to Power BI
o Start tracing all queries
o Collect the results
o Stop the tracing
The two engines in DAX
The two engines in DAX
Differences between the engines
Formula Engine Storage Engine
• Computes any DAX • Computes simple
function expressions
• Very rich in terms of • Uses model relationships
expressivity • Multi threaded
• Single threaded • Fast side of DAX
• Slow side of DAX
Performance
What is a Data Cache?
Column Storage Data Cache
Prod SUM(Amt) SUM(Qty)
Prod Amt Qty
Storage Engine (VertiPaq)
… … …
VertiPaq query (xmSQL) … … …
… … …
… … …
SE fills data
Result cache
Prod SUM(Amt) SUM(Qty)
… … …
… … …
FE works on
… … …
data caches
… … …
xmSQL
o Storage engine uses xmSQL
• Syntax similar to SQL
• Few options (compared to SQL)
• Not intended for the user
o Simple mathematical operations
o Some (not all) aggregations
o Usage of model relationships
How FE uses Data Caches
o There is no indication of which data cache is used by
which FE operator
o Only common sense and some geeky attitude helps
o For simple plans, it is easy
o For complex plans, it is nearly impossible
o Thus: optimize simple plans!
Next step is optimizing the open orders measure
Open Orders
Open Orders
October, 2017 Open Orders = 3
Order 1
Order 2
Order 3
Order 5 Order 4
Changing the data model
October, 2017
Order 1
Order 2
Order 3
Order 5 Order 4
Conclusions
Create a repro query
Isolate MDX or DAX query
Identify DAX expression (usually a measure)
Modify DAX expression and test different timings
DAX Studio improves productivity
Use local DAX measure overriding the one defined in the data model
At [Link] you will find plenty of resources on DAX
27
References
o The Definitive Guide to DAX
By Marco Russo, Alberto Ferrari – Microsoft Press
[Link]
o Understanding DAX Query Plans
o [Link]
o Understanding Distinct Count in DAX Query Plans
o [Link]
Tools
o DAX Studio
o [Link]
o VertiPaq Analyzer
o [Link]
o OLAP PivotTable Extensions
o [Link]
Thank you!
Check our articles, whitepapers and courses on
[Link]