SlideShare a Scribd company logo
1 of 6
Download to read offline
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.

More Related Content

What's hot

What's hot (20)

Data Modeling with Power BI
Data Modeling with Power BIData Modeling with Power BI
Data Modeling with Power BI
 
Business intelligence 3.0 and the data lake
Business intelligence 3.0 and the data lakeBusiness intelligence 3.0 and the data lake
Business intelligence 3.0 and the data lake
 
OLAP Cubes in Datawarehousing
OLAP Cubes in DatawarehousingOLAP Cubes in Datawarehousing
OLAP Cubes in Datawarehousing
 
Power BI Full Course | Power BI Tutorial for Beginners | Edureka
Power BI Full Course | Power BI Tutorial for Beginners | EdurekaPower BI Full Course | Power BI Tutorial for Beginners | Edureka
Power BI Full Course | Power BI Tutorial for Beginners | Edureka
 
Advanced Excel Demo
Advanced Excel DemoAdvanced Excel Demo
Advanced Excel Demo
 
Sap business one keyboard shortcuts
Sap business one keyboard shortcutsSap business one keyboard shortcuts
Sap business one keyboard shortcuts
 
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
 
Types of connections in Power BI
Types of connections in Power BITypes of connections in Power BI
Types of connections in Power BI
 
Business intelligence
Business intelligenceBusiness intelligence
Business intelligence
 
Building a Dashboard in an hour with Power Pivot and Power BI
Building a Dashboard in an hour with Power Pivot and Power BIBuilding a Dashboard in an hour with Power Pivot and Power BI
Building a Dashboard in an hour with Power Pivot and Power BI
 
Vertica Analytics Database general overview
Vertica Analytics Database general overviewVertica Analytics Database general overview
Vertica Analytics Database general overview
 
Tableau ppt
Tableau pptTableau ppt
Tableau ppt
 
Power BI Made Simple
Power BI Made SimplePower BI Made Simple
Power BI Made Simple
 
How to use pivot table
How to use pivot tableHow to use pivot table
How to use pivot table
 
Sql joins inner join self join outer joins
Sql joins inner join self join outer joinsSql joins inner join self join outer joins
Sql joins inner join self join outer joins
 
Introduction to DAX
Introduction to DAXIntroduction to DAX
Introduction to DAX
 
Tableau Developer Roles & Responsibilities | Become A Tableau Developer | Tab...
Tableau Developer Roles & Responsibilities | Become A Tableau Developer | Tab...Tableau Developer Roles & Responsibilities | Become A Tableau Developer | Tab...
Tableau Developer Roles & Responsibilities | Become A Tableau Developer | Tab...
 
What are Tableau Functions? Edureka
What are Tableau Functions? EdurekaWhat are Tableau Functions? Edureka
What are Tableau Functions? Edureka
 
Power BI: Types of gateways in Power BI
Power BI: Types of gateways in Power BIPower BI: Types of gateways in Power BI
Power BI: Types of gateways in Power BI
 
Business intelligence
Business intelligenceBusiness intelligence
Business intelligence
 

Similar to Dax best practices.pdf

Excel Useful Tips
Excel Useful TipsExcel Useful Tips
Excel Useful Tips
Parul_100in
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
kktv
 
Microsoft Excel Tips
Microsoft Excel TipsMicrosoft Excel Tips
Microsoft Excel Tips
mifarooqui
 

Similar to Dax best practices.pdf (20)

Excel Useful Tips
Excel Useful TipsExcel Useful Tips
Excel Useful Tips
 
Dax en
Dax enDax en
Dax en
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tips
 
Excel tips
Excel tipsExcel tips
Excel tips
 
Excel tips
Excel tipsExcel tips
Excel tips
 
Excel Useful Tips
Excel Useful TipsExcel Useful Tips
Excel Useful Tips
 
35 Useful Excel Tips
35 Useful Excel Tips35 Useful Excel Tips
35 Useful Excel Tips
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tips
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tips
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
 
Microsoft Excel Tips
Microsoft Excel TipsMicrosoft Excel Tips
Microsoft Excel Tips
 
Excel tips
Excel tipsExcel tips
Excel tips
 
Excel tips 172
Excel tips 172Excel tips 172
Excel tips 172
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
 
The 4 Step Excel 2013 Functions Guide
The 4 Step Excel 2013 Functions GuideThe 4 Step Excel 2013 Functions Guide
The 4 Step Excel 2013 Functions Guide
 

More from deepneuron (8)

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

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

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.