Marco Russo: DAX Optimization Examples
Marco Russo: DAX Optimization Examples
Power BI/SSAS
Optimization
BI Architectural
Review
On-Site
Consulting
Custom Training
& Mentoring www.sq lb i.co 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
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 http://daxstudio.codeplex.com
• 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
DAX Studio
Capturing Power BI queries using DAX Studio
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
• http://www.sqlbi.com/tools/vertipaq-analyzer
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
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
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)
27
References
o DAX Studio
o http://daxstudio.codeplex.com
o VertiPaq Analyzer
o http://www.sqlbi.com/tools/vertipaq-analyzer