SlideShare a Scribd company logo
DAX Best Practices
This guide will show you how to optimise the back-end code of your Power BI reports
to make them run faster. We are recognised for our expertise in implementing
business intelligence and analytics solutions as the Microsoft Power BI Partner of the
Year for 2021.
Based on our experience, we have compiled this list of best practises, which includes:
1. Enhancing the DAX Syntax
2. DAX Functions Optimization
3. Avoiding Common Mistake
Before you start:
Tip #1: Before optimising DAX, always clear your DAX cache.
Internal Vert iPAQ queries build up your DAX cache. From within DAX Studio, you
can clear your cache. You can effectively measure performance gains by resetting
your cache.
Improving Your DAX Syntax
1. Use DAX Formatter to format your code
Code that has been formatted is easier to read and maintain. DAX Formatter is a free
programme that converts raw DAX into readable code.
2. Use the DISTINCT () and VALUES () functions consistently
If Power BI detects a referential integrity
violation, it fills the column with a blank value. Because it can’t check for violations
when running direct queries, Microsoft Power Bi Tool adds a blank value to columns.
DISTINCT () and VALUES () are two different functions:
Due to an integrity violation, DISTINCT () does not return any blanks added. A blank
is only included in the DISTINCT () function if it is part of the original data.
VALUES (): Returns both original data blanks and blanks added by Power BI as a
result of referential integrity violations.
Throughout the report, make sure to use the DISTINCT () and VALUES () functions
consistently. Otherwise, the values for blank columns will be inconsistent.
DAX Best Practices
3. Add column and measure references in your DAX expressions
You must eliminate ambiguity in your DAX to ensure that it can be understood and
used by anyone. You ensure that anyone can read your DAX immediately by
including column and measure references. Fully qualified column references should
always be used, but fully qualified measure references should never be used. You can
quickly distinguish between a column and a measure based on whether it’s fully
qualified this way.
When a measure home table is changed, adding column and measure references
ensures that expressions continue to work.
Profit = Orders [Sales] with col reference – Orders [Price]
Without the use of a col reference: [Sales] – [Cost] = Profit
Optimizing Your DAX Functions
1. Use ISBLANK () instead of =Blank () check
Instead of using the comparison operator = Blank, use the built-in function ISBLANK
() to check for any blank values (). Is Blank only checks for blanks, whereas = Blank
() returns ‘True’ for either blank values or empty strings.
2. Use = 0 instead of checking for ISBLANK () || = 0
In Power BI, the BLANK value is associated with the data type’s base value. “0” for
integers, “(empty string)” for string columns, and “1–1–1900” for date fields
correspond to the BLANK value.
ISBLANK () || = 0 performs two checks: first, it determines whether or not a column
is BLANK, and then it looks for zeroes. = 0 performs both checks at the same time,
speeding up the calculation process.
Use the IN operator to check solely for zero.
3. Use SELECTEDVALUE () instead of HASONE VALUE ()
After applying slicers and filters, it’s common to use HASONE VALUE() to see if
there’s only one value in a column. When you do this, you must also use the DAX
function VALUES(Column Name) to retrieve that single value.
Internally, SELECTED VALUE() performs the steps listed above. If there is only one
value, it retrieves it automatically, and if there are multiple values, it returns a blank.
DAX Best Practices
4. Use SELECTEDVALUE () instead of VALUES ()
If the VALUES () function encounters multiple values, it returns an error. Error
functions are frequently used to address errors, which has a negative impact on
performance. Use SELECTEDVALUE instead of VALUES () (). If it encounters
multiple values, the SELECTEDVALUE () function returns a blank (instead of an
error).
5. Use variables instead of repeating measures inside the IF branch
Ratio = IF ([Total Rows] > 10, SUM(Revenue) /[Total Rows], 0)
Measures are calculated in this case in real time, which means the [Total Rows]
expression is calculated twice: once for the condition check and then again for the true
condition expression.
Correct DAX: VAR total Rows = [Total Rows]; Ratio = IF(total Rows > 10,
SUM(Revenue) / totalRows,0)
You can save the resultant measure value in a variable rather than calculating the same
expression multiple times. Wherever a variable reference is needed, you can use it. All
instances where you call the same measure follow the same variable process.
Variables can help you avoid performing repetitive tasks.
It’s important to remember that variables are actually constants.
6. Use DIVIDE () instead of /
If the denominator is zero, / throws an exception. The DIVIDE () function performs an
internal check to see if the denominator is zero. If it is, the value specified in a third
(extra) parameter is returned.
When using the “/” operator, you must use the IF condition for “invalid denominator”
cases. Internally, the DIVIDE () function performs IF checks.
Note: It is preferable to use the “/” operator without an IF check if you are certain the
denominator value is not zero.
7. Use KEEPFILTERS () instead of FILTER(T)
Any existing set of filters on a column applied via slicers are overridden by the
FILTER function. The KEEPFILTER function does not override the set of filters that
already exist. Instead, it employs the intersection of values found in both, preserving
the current situation. When performing calculations, use it to maintain any filters
applied by slicers or at the report level.
DAX Best Practices
8. Use FILTER (all(Column Name)) instead of FILTER(values()) or
FILTER(T)
Instead of using Table or VALUE, combine the All(Column Name) and FILTER
functions to calculate measures independent of any filters applied to a column ().
Consider the following scenario: CALCULATE ([Total Sales], FILTER
(ALL(Products [Color]), Color = ‘Red’))
If you don’t need to keep the current context, use ALL with the FILTER function.
Using expressions instead of the FILTER function to apply filters has the same effect
as using the FILTER function. The ALL function in the filter is used to translate this
method internally. Consider the following scenario: CALCULATE ([Total Sales],
FILTER(ALL(Products[Color]), Color = ‘Red’))
Filters should always be applied to the desired column rather than the entire table, as
this allows for easier scaling.
pidan and sql,bi are two sources of information.
9. Use COUNTROWS instead of COUNT
You can count column values with the COUNT function or table rows with the
COUNTROWS function in Power BI. If the counted column does not contain any
BLANKs, both functions produce the same result.
For three reasons, COUNTROWS is usually the better option:
1. It’s more efficient, and will perform better
2. It doesn’t consider BLANKs
For example: Sales Orders = COUNT (Sales [Order Date]) versus
Sales Orders = COUNTROWS(Sales)3. The formula intention is clearer and self-
descriptive
10. Use SEARCH () with the last parameter
The last parameter of the SEARCH () DAX function is the value that the query must
return if the search string is not found. Instead of using Error functions in conjunction
with SEARCH (), you should always use SEARCH () ().
DAX Best Practices
11. ALL vs. ALL Except
As long as the “exempted” columns are columns on the pivot, ALLEXCEPT ()
behaves exactly like ALL (), VALUES (). On columns that aren’t on the pivot,
ALLEXCEPT () does not keep the pivot context. When using VALUES, use ALL ()
instead of ALLEXCEPT() ()
Common Mistakes to Avoid
1. Do not change BLANK values to zeros or other strings
Blanks are frequently replaced with zeros or other strings. Power BI, on the other
hand, filters out all rows with blank values. This limits the result set and improves
performance when viewing results from tables with large amounts of data.
Power BI does not filter unwanted rows when you replace blanks, which has a
negative impact on performance.
2. Use (a-b)/b along with variables instead of a/b — 1 or a/b*100–100
To avoid duplicate measure calculations, it is common to use a/b — 1 to calculate a
ratio. However, you can achieve the same results by using variables and calculating
the ratio with (a-b)/b.
If both a and b are blank, (a-b)/b returns a blank value, and Power BI filters out the
values. Because both a and b are integers, the result of a/b — 1 would be -1.
Sql, bi is a source of information.
3. Stop using IFERROR () and ISERROR ()
When using the FIND () and SEARCH() functions in Excel, the IFERROR() and
ISERROR() functions were frequently used. Because FIND () and SEARCH ()
returned errors if the query did not return the desired result, they were required. The
IFERROR () and ISERROR() functions force the Power BI engine to check each row
for errors in a step-by-step manner. There is no direct method to state which row
returned the error at the moment.
The DAX functions FIND () and SEARCH() provide an extra parameter that the
query can pass. If the search string isn’t found, the parameter is returned. The DAX
functions FIND () and SEARCH() check if more than one value is returned. They also
make certain that no fractions are divided by zero.
DAX Best Practices
You can use situationally appropriate DAX functions like DIVIDE () and
SELECTEDVALUE instead of the FIND() and SEARCH() DAX functions ().
Internally, the DIVIDE () and SELECTEDVALUE() functions check for errors and
return the expected results.
Always remember that DAX expressions can be written in such a way that they never
return an error.
4. Do not use scalar variables in SUMMARIZE ()
Traditionally, the SUMMARIZE () function has been used to group columns and
return the resulting aggregations. The SUMMARIZECOLUMNS () function, on the
other hand, is newer and more optimised. Instead, make use of that.
SUMMARIZE () should only be used for grouped table elements that don’t have any
associated measures or aggregations. Consider the following scenario: SUMMARIZE
(Table, Column1, Column2)
5. Avoid using the Add Columns () function inside measure
expressions
By default, measures are calculated iteratively. When iterative functions like
AddColumns() are used in measure definitions, Best Power Bi Reports creates nested
iterations, which slow down report performance.
6. Check if you can convert your column to a Boolean column
If a column contains only two distinct values, see if it can be converted to a Boolean
data type (true/false). When you have a large number of rows, using Boolean data
types speeds up the process.
7. Avoid filtering on string columns
Filtering should instead be done with ID columns. If you need to filter by sales
location, for example, give each one a numeric ID. This means that instead of string
columns, you filter by integer columns. You can now use the VertiPaq engine, which
uses value encoding to reduce a column’s memory footprint.
Note that value encoding is only applicable to integers.
8. Work upstream, if possible
Consider creating calculated columns or flags in the back end if certain calculations
require complex DAX formulae or if multiple filters are applied repeatedly in DAX
measures and calculations.
Ad

More Related Content

What's hot (20)

Power BI Data Modeling.pdf
Power BI Data Modeling.pdfPower BI Data Modeling.pdf
Power BI Data Modeling.pdf
VishnuGone
 
Calculated Fields in Tableau
Calculated Fields in TableauCalculated Fields in Tableau
Calculated Fields in Tableau
Kanika Nagpal
 
50 MS Excel Tips and Tricks
50 MS Excel Tips and Tricks 50 MS Excel Tips and Tricks
50 MS Excel Tips and Tricks
BurCom Consulting Ltd.
 
Introduction to MongoDB and CRUD operations
Introduction to MongoDB and CRUD operationsIntroduction to MongoDB and CRUD operations
Introduction to MongoDB and CRUD operations
Anand Kumar
 
DAX (Data Analysis eXpressions) from Zero to Hero
DAX (Data Analysis eXpressions) from Zero to HeroDAX (Data Analysis eXpressions) from Zero to Hero
DAX (Data Analysis eXpressions) from Zero to Hero
Microsoft TechNet - Belgium and Luxembourg
 
Power BI - Power Query
Power BI - Power QueryPower BI - Power Query
Power BI - Power Query
Jerric Lyns John
 
Advanced Microsoft Excel
Advanced Microsoft ExcelAdvanced Microsoft Excel
Advanced Microsoft Excel
Eric Metelka
 
Microsoft Power BI 101
Microsoft Power BI 101Microsoft Power BI 101
Microsoft Power BI 101
Sharon Weaver
 
Tableau Visual analytics complete deck 2
Tableau Visual analytics complete deck 2Tableau Visual analytics complete deck 2
Tableau Visual analytics complete deck 2
Arun K
 
PowerBI Training
PowerBI Training PowerBI Training
PowerBI Training
Knowledge And Skill Forum
 
MICROSOFT POWER BI PPT.pptx
MICROSOFT POWER BI PPT.pptxMICROSOFT POWER BI PPT.pptx
MICROSOFT POWER BI PPT.pptx
ridazulquarnain
 
Introduction to Microsoft Power BI
Introduction to Microsoft Power BIIntroduction to Microsoft Power BI
Introduction to Microsoft Power BI
Exilesoft
 
DAX and Power BI Training - 002 DAX Level 1 - 3
DAX and Power BI Training - 002 DAX Level 1 - 3DAX and Power BI Training - 002 DAX Level 1 - 3
DAX and Power BI Training - 002 DAX Level 1 - 3
Will Harvey
 
Power query
Power queryPower query
Power query
Marco Pozzan
 
Introduction to DAX
Introduction to DAXIntroduction to DAX
Introduction to DAX
Ike Ellis
 
Power BI Interview Questions and Answers | Power BI Certification | Power BI ...
Power BI Interview Questions and Answers | Power BI Certification | Power BI ...Power BI Interview Questions and Answers | Power BI Certification | Power BI ...
Power BI Interview Questions and Answers | Power BI Certification | Power BI ...
Edureka!
 
Power bi introduction
Power bi introductionPower bi introduction
Power bi introduction
Bishwadeb Dey
 
Lesson 15 Using What If Analysis
Lesson 15   Using What If AnalysisLesson 15   Using What If Analysis
Lesson 15 Using What If Analysis
guevarra_2000
 
Microsoft Word Advance Features
Microsoft Word Advance FeaturesMicrosoft Word Advance Features
Microsoft Word Advance Features
AkashMeghwar2
 
Power BI - Bring your data together
Power BI - Bring your data togetherPower BI - Bring your data together
Power BI - Bring your data together
Stéphane Fréchette
 
Power BI Data Modeling.pdf
Power BI Data Modeling.pdfPower BI Data Modeling.pdf
Power BI Data Modeling.pdf
VishnuGone
 
Calculated Fields in Tableau
Calculated Fields in TableauCalculated Fields in Tableau
Calculated Fields in Tableau
Kanika Nagpal
 
Introduction to MongoDB and CRUD operations
Introduction to MongoDB and CRUD operationsIntroduction to MongoDB and CRUD operations
Introduction to MongoDB and CRUD operations
Anand Kumar
 
Advanced Microsoft Excel
Advanced Microsoft ExcelAdvanced Microsoft Excel
Advanced Microsoft Excel
Eric Metelka
 
Microsoft Power BI 101
Microsoft Power BI 101Microsoft Power BI 101
Microsoft Power BI 101
Sharon Weaver
 
Tableau Visual analytics complete deck 2
Tableau Visual analytics complete deck 2Tableau Visual analytics complete deck 2
Tableau Visual analytics complete deck 2
Arun K
 
MICROSOFT POWER BI PPT.pptx
MICROSOFT POWER BI PPT.pptxMICROSOFT POWER BI PPT.pptx
MICROSOFT POWER BI PPT.pptx
ridazulquarnain
 
Introduction to Microsoft Power BI
Introduction to Microsoft Power BIIntroduction to Microsoft Power BI
Introduction to Microsoft Power BI
Exilesoft
 
DAX and Power BI Training - 002 DAX Level 1 - 3
DAX and Power BI Training - 002 DAX Level 1 - 3DAX and Power BI Training - 002 DAX Level 1 - 3
DAX and Power BI Training - 002 DAX Level 1 - 3
Will Harvey
 
Introduction to DAX
Introduction to DAXIntroduction to DAX
Introduction to DAX
Ike Ellis
 
Power BI Interview Questions and Answers | Power BI Certification | Power BI ...
Power BI Interview Questions and Answers | Power BI Certification | Power BI ...Power BI Interview Questions and Answers | Power BI Certification | Power BI ...
Power BI Interview Questions and Answers | Power BI Certification | Power BI ...
Edureka!
 
Power bi introduction
Power bi introductionPower bi introduction
Power bi introduction
Bishwadeb Dey
 
Lesson 15 Using What If Analysis
Lesson 15   Using What If AnalysisLesson 15   Using What If Analysis
Lesson 15 Using What If Analysis
guevarra_2000
 
Microsoft Word Advance Features
Microsoft Word Advance FeaturesMicrosoft Word Advance Features
Microsoft Word Advance Features
AkashMeghwar2
 
Power BI - Bring your data together
Power BI - Bring your data togetherPower BI - Bring your data together
Power BI - Bring your data together
Stéphane Fréchette
 

Similar to Dax best practices.pdf (20)

Excel Useful Tips
Excel Useful TipsExcel Useful Tips
Excel Useful Tips
Parul_100in
 
Dax en
Dax enDax en
Dax en
Marco Pozzan
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tips
Mohsin Azad
 
Excel Useful Tips
Excel Useful TipsExcel Useful Tips
Excel Useful Tips
Srinath Maharana
 
Excel tips
Excel tipsExcel tips
Excel tips
Ashish Patel
 
35 Useful Excel Tips
35 Useful Excel Tips35 Useful Excel Tips
35 Useful Excel Tips
Mukunda Adhikari
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tips
anmolbansal09
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tips
sujayramshankar
 
Excel tips
Excel tipsExcel tips
Excel tips
Ashish Patel
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
asadnaeemkhan
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
Muhammad Farid Ajmal
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
Muhammad Farid Ajmal
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
kktv
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
Kritika
 
Microsoft Excel Tips
Microsoft Excel TipsMicrosoft Excel Tips
Microsoft Excel Tips
mifarooqui
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
Spicy Flavours
 
Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...
Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...
Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...
TechMit
 
Excel tips
Excel tipsExcel tips
Excel tips
Ramaswamy Ramakrishnan
 
Excel tips 172
Excel tips 172Excel tips 172
Excel tips 172
vimalpfizer
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
ashish83mech
 
Ad

More from deepneuron (8)

full stack data science training
full stack data science trainingfull stack data science training
full stack data science training
deepneuron
 
SQL Server part 1 (6).pptx
SQL Server part 1 (6).pptxSQL Server part 1 (6).pptx
SQL Server part 1 (6).pptx
deepneuron
 
project 02.pdf
project 02.pdfproject 02.pdf
project 02.pdf
deepneuron
 
project01.pdf
project01.pdfproject01.pdf
project01.pdf
deepneuron
 
Data model Assignment.pdf
Data model Assignment.pdfData model Assignment.pdf
Data model Assignment.pdf
deepneuron
 
Data model Assignment.pdf
Data model Assignment.pdfData model Assignment.pdf
Data model Assignment.pdf
deepneuron
 
Data model Assignment.pdf
Data model Assignment.pdfData model Assignment.pdf
Data model Assignment.pdf
deepneuron
 
Measuresof spread
Measuresof spreadMeasuresof spread
Measuresof spread
deepneuron
 
full stack data science training
full stack data science trainingfull stack data science training
full stack data science training
deepneuron
 
SQL Server part 1 (6).pptx
SQL Server part 1 (6).pptxSQL Server part 1 (6).pptx
SQL Server part 1 (6).pptx
deepneuron
 
project 02.pdf
project 02.pdfproject 02.pdf
project 02.pdf
deepneuron
 
Data model Assignment.pdf
Data model Assignment.pdfData model Assignment.pdf
Data model Assignment.pdf
deepneuron
 
Data model Assignment.pdf
Data model Assignment.pdfData model Assignment.pdf
Data model Assignment.pdf
deepneuron
 
Data model Assignment.pdf
Data model Assignment.pdfData model Assignment.pdf
Data model Assignment.pdf
deepneuron
 
Measuresof spread
Measuresof spreadMeasuresof spread
Measuresof spread
deepneuron
 
Ad

Recently uploaded (20)

Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 

Dax best practices.pdf

  • 1. DAX Best Practices This guide will show you how to optimise the back-end code of your Power BI reports to make them run faster. We are recognised for our expertise in implementing business intelligence and analytics solutions as the Microsoft Power BI Partner of the Year for 2021. Based on our experience, we have compiled this list of best practises, which includes: 1. Enhancing the DAX Syntax 2. DAX Functions Optimization 3. Avoiding Common Mistake Before you start: Tip #1: Before optimising DAX, always clear your DAX cache. Internal Vert iPAQ queries build up your DAX cache. From within DAX Studio, you can clear your cache. You can effectively measure performance gains by resetting your cache. Improving Your DAX Syntax 1. Use DAX Formatter to format your code Code that has been formatted is easier to read and maintain. DAX Formatter is a free programme that converts raw DAX into readable code. 2. Use the DISTINCT () and VALUES () functions consistently If Power BI detects a referential integrity violation, it fills the column with a blank value. Because it can’t check for violations when running direct queries, Microsoft Power Bi Tool adds a blank value to columns. DISTINCT () and VALUES () are two different functions: Due to an integrity violation, DISTINCT () does not return any blanks added. A blank is only included in the DISTINCT () function if it is part of the original data. VALUES (): Returns both original data blanks and blanks added by Power BI as a result of referential integrity violations. Throughout the report, make sure to use the DISTINCT () and VALUES () functions consistently. Otherwise, the values for blank columns will be inconsistent.
  • 2. DAX Best Practices 3. Add column and measure references in your DAX expressions You must eliminate ambiguity in your DAX to ensure that it can be understood and used by anyone. You ensure that anyone can read your DAX immediately by including column and measure references. Fully qualified column references should always be used, but fully qualified measure references should never be used. You can quickly distinguish between a column and a measure based on whether it’s fully qualified this way. When a measure home table is changed, adding column and measure references ensures that expressions continue to work. Profit = Orders [Sales] with col reference – Orders [Price] Without the use of a col reference: [Sales] – [Cost] = Profit Optimizing Your DAX Functions 1. Use ISBLANK () instead of =Blank () check Instead of using the comparison operator = Blank, use the built-in function ISBLANK () to check for any blank values (). Is Blank only checks for blanks, whereas = Blank () returns ‘True’ for either blank values or empty strings. 2. Use = 0 instead of checking for ISBLANK () || = 0 In Power BI, the BLANK value is associated with the data type’s base value. “0” for integers, “(empty string)” for string columns, and “1–1–1900” for date fields correspond to the BLANK value. ISBLANK () || = 0 performs two checks: first, it determines whether or not a column is BLANK, and then it looks for zeroes. = 0 performs both checks at the same time, speeding up the calculation process. Use the IN operator to check solely for zero. 3. Use SELECTEDVALUE () instead of HASONE VALUE () After applying slicers and filters, it’s common to use HASONE VALUE() to see if there’s only one value in a column. When you do this, you must also use the DAX function VALUES(Column Name) to retrieve that single value. Internally, SELECTED VALUE() performs the steps listed above. If there is only one value, it retrieves it automatically, and if there are multiple values, it returns a blank.
  • 3. DAX Best Practices 4. Use SELECTEDVALUE () instead of VALUES () If the VALUES () function encounters multiple values, it returns an error. Error functions are frequently used to address errors, which has a negative impact on performance. Use SELECTEDVALUE instead of VALUES () (). If it encounters multiple values, the SELECTEDVALUE () function returns a blank (instead of an error). 5. Use variables instead of repeating measures inside the IF branch Ratio = IF ([Total Rows] > 10, SUM(Revenue) /[Total Rows], 0) Measures are calculated in this case in real time, which means the [Total Rows] expression is calculated twice: once for the condition check and then again for the true condition expression. Correct DAX: VAR total Rows = [Total Rows]; Ratio = IF(total Rows > 10, SUM(Revenue) / totalRows,0) You can save the resultant measure value in a variable rather than calculating the same expression multiple times. Wherever a variable reference is needed, you can use it. All instances where you call the same measure follow the same variable process. Variables can help you avoid performing repetitive tasks. It’s important to remember that variables are actually constants. 6. Use DIVIDE () instead of / If the denominator is zero, / throws an exception. The DIVIDE () function performs an internal check to see if the denominator is zero. If it is, the value specified in a third (extra) parameter is returned. When using the “/” operator, you must use the IF condition for “invalid denominator” cases. Internally, the DIVIDE () function performs IF checks. Note: It is preferable to use the “/” operator without an IF check if you are certain the denominator value is not zero. 7. Use KEEPFILTERS () instead of FILTER(T) Any existing set of filters on a column applied via slicers are overridden by the FILTER function. The KEEPFILTER function does not override the set of filters that already exist. Instead, it employs the intersection of values found in both, preserving the current situation. When performing calculations, use it to maintain any filters applied by slicers or at the report level.
  • 4. DAX Best Practices 8. Use FILTER (all(Column Name)) instead of FILTER(values()) or FILTER(T) Instead of using Table or VALUE, combine the All(Column Name) and FILTER functions to calculate measures independent of any filters applied to a column (). Consider the following scenario: CALCULATE ([Total Sales], FILTER (ALL(Products [Color]), Color = ‘Red’)) If you don’t need to keep the current context, use ALL with the FILTER function. Using expressions instead of the FILTER function to apply filters has the same effect as using the FILTER function. The ALL function in the filter is used to translate this method internally. Consider the following scenario: CALCULATE ([Total Sales], FILTER(ALL(Products[Color]), Color = ‘Red’)) Filters should always be applied to the desired column rather than the entire table, as this allows for easier scaling. pidan and sql,bi are two sources of information. 9. Use COUNTROWS instead of COUNT You can count column values with the COUNT function or table rows with the COUNTROWS function in Power BI. If the counted column does not contain any BLANKs, both functions produce the same result. For three reasons, COUNTROWS is usually the better option: 1. It’s more efficient, and will perform better 2. It doesn’t consider BLANKs For example: Sales Orders = COUNT (Sales [Order Date]) versus Sales Orders = COUNTROWS(Sales)3. The formula intention is clearer and self- descriptive 10. Use SEARCH () with the last parameter The last parameter of the SEARCH () DAX function is the value that the query must return if the search string is not found. Instead of using Error functions in conjunction with SEARCH (), you should always use SEARCH () ().
  • 5. DAX Best Practices 11. ALL vs. ALL Except As long as the “exempted” columns are columns on the pivot, ALLEXCEPT () behaves exactly like ALL (), VALUES (). On columns that aren’t on the pivot, ALLEXCEPT () does not keep the pivot context. When using VALUES, use ALL () instead of ALLEXCEPT() () Common Mistakes to Avoid 1. Do not change BLANK values to zeros or other strings Blanks are frequently replaced with zeros or other strings. Power BI, on the other hand, filters out all rows with blank values. This limits the result set and improves performance when viewing results from tables with large amounts of data. Power BI does not filter unwanted rows when you replace blanks, which has a negative impact on performance. 2. Use (a-b)/b along with variables instead of a/b — 1 or a/b*100–100 To avoid duplicate measure calculations, it is common to use a/b — 1 to calculate a ratio. However, you can achieve the same results by using variables and calculating the ratio with (a-b)/b. If both a and b are blank, (a-b)/b returns a blank value, and Power BI filters out the values. Because both a and b are integers, the result of a/b — 1 would be -1. Sql, bi is a source of information. 3. Stop using IFERROR () and ISERROR () When using the FIND () and SEARCH() functions in Excel, the IFERROR() and ISERROR() functions were frequently used. Because FIND () and SEARCH () returned errors if the query did not return the desired result, they were required. The IFERROR () and ISERROR() functions force the Power BI engine to check each row for errors in a step-by-step manner. There is no direct method to state which row returned the error at the moment. The DAX functions FIND () and SEARCH() provide an extra parameter that the query can pass. If the search string isn’t found, the parameter is returned. The DAX functions FIND () and SEARCH() check if more than one value is returned. They also make certain that no fractions are divided by zero.
  • 6. DAX Best Practices You can use situationally appropriate DAX functions like DIVIDE () and SELECTEDVALUE instead of the FIND() and SEARCH() DAX functions (). Internally, the DIVIDE () and SELECTEDVALUE() functions check for errors and return the expected results. Always remember that DAX expressions can be written in such a way that they never return an error. 4. Do not use scalar variables in SUMMARIZE () Traditionally, the SUMMARIZE () function has been used to group columns and return the resulting aggregations. The SUMMARIZECOLUMNS () function, on the other hand, is newer and more optimised. Instead, make use of that. SUMMARIZE () should only be used for grouped table elements that don’t have any associated measures or aggregations. Consider the following scenario: SUMMARIZE (Table, Column1, Column2) 5. Avoid using the Add Columns () function inside measure expressions By default, measures are calculated iteratively. When iterative functions like AddColumns() are used in measure definitions, Best Power Bi Reports creates nested iterations, which slow down report performance. 6. Check if you can convert your column to a Boolean column If a column contains only two distinct values, see if it can be converted to a Boolean data type (true/false). When you have a large number of rows, using Boolean data types speeds up the process. 7. Avoid filtering on string columns Filtering should instead be done with ID columns. If you need to filter by sales location, for example, give each one a numeric ID. This means that instead of string columns, you filter by integer columns. You can now use the VertiPaq engine, which uses value encoding to reduce a column’s memory footprint. Note that value encoding is only applicable to integers. 8. Work upstream, if possible Consider creating calculated columns or flags in the back end if certain calculations require complex DAX formulae or if multiple filters are applied repeatedly in DAX measures and calculations.