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

Power BI Data Modeling.pdf
Power BI Data Modeling.pdfPower BI Data Modeling.pdf
Power BI Data Modeling.pdfVishnuGone
 
Business Intelligence (BI) and Data Management Basics
Business Intelligence (BI) and Data Management  Basics Business Intelligence (BI) and Data Management  Basics
Business Intelligence (BI) and Data Management Basics amorshed
 
Power bi overview
Power bi overview Power bi overview
Power bi overview Kiki Noviandi
 
Power BI Overview
Power BI Overview Power BI Overview
Power BI Overview Gal Vekselman
 
Data Analytics For Beginners | Introduction To Data Analytics | Data Analytic...
Data Analytics For Beginners | Introduction To Data Analytics | Data Analytic...Data Analytics For Beginners | Introduction To Data Analytics | Data Analytic...
Data Analytics For Beginners | Introduction To Data Analytics | Data Analytic...Edureka!
 
Power BI Training | Getting Started with Power BI | Power BI Tutorial | Power...
Power BI Training | Getting Started with Power BI | Power BI Tutorial | Power...Power BI Training | Getting Started with Power BI | Power BI Tutorial | Power...
Power BI Training | Getting Started with Power BI | Power BI Tutorial | Power...Edureka!
 
Power BI for Big Data and the New Look of Big Data Solutions
Power BI for Big Data and the New Look of Big Data SolutionsPower BI for Big Data and the New Look of Big Data Solutions
Power BI for Big Data and the New Look of Big Data SolutionsJames Serra
 
Business analytics and data visualisation
Business analytics and data visualisationBusiness analytics and data visualisation
Business analytics and data visualisationShwetabh Jaiswal
 
Power BI Dashboard | Microsoft Power BI Tutorial | Data Visualization | Edureka
Power BI Dashboard | Microsoft Power BI Tutorial | Data Visualization | EdurekaPower BI Dashboard | Microsoft Power BI Tutorial | Data Visualization | Edureka
Power BI Dashboard | Microsoft Power BI Tutorial | Data Visualization | EdurekaEdureka!
 
Improve power bi performance
Improve power bi performanceImprove power bi performance
Improve power bi performanceAnnie Xu
 
Business Intelligence and Business Analytics
Business Intelligence and Business AnalyticsBusiness Intelligence and Business Analytics
Business Intelligence and Business Analyticssnehal_152
 
Tableau: A Business Intelligence and Analytics Software
Tableau: A Business Intelligence and Analytics SoftwareTableau: A Business Intelligence and Analytics Software
Tableau: A Business Intelligence and Analytics SoftwareExtentia Information Technology
 
Data analytics and powerbi intro
Data analytics and powerbi introData analytics and powerbi intro
Data analytics and powerbi introBerkovich Consulting
 
What Is Power BI? | Introduction To Microsoft Power BI | Power BI Training | ...
What Is Power BI? | Introduction To Microsoft Power BI | Power BI Training | ...What Is Power BI? | Introduction To Microsoft Power BI | Power BI Training | ...
What Is Power BI? | Introduction To Microsoft Power BI | Power BI Training | ...Edureka!
 

What's hot (20)

Power BI Data Modeling.pdf
Power BI Data Modeling.pdfPower BI Data Modeling.pdf
Power BI Data Modeling.pdf
 
Excel to Power BI
Excel to Power BIExcel to Power BI
Excel to Power BI
 
Business Intelligence (BI) and Data Management Basics
Business Intelligence (BI) and Data Management  Basics Business Intelligence (BI) and Data Management  Basics
Business Intelligence (BI) and Data Management Basics
 
Power BI visuals
Power BI visualsPower BI visuals
Power BI visuals
 
Power bi overview
Power bi overview Power bi overview
Power bi overview
 
Power BI Overview
Power BI Overview Power BI Overview
Power BI Overview
 
ETL
ETLETL
ETL
 
Data Analytics For Beginners | Introduction To Data Analytics | Data Analytic...
Data Analytics For Beginners | Introduction To Data Analytics | Data Analytic...Data Analytics For Beginners | Introduction To Data Analytics | Data Analytic...
Data Analytics For Beginners | Introduction To Data Analytics | Data Analytic...
 
Power BI Training | Getting Started with Power BI | Power BI Tutorial | Power...
Power BI Training | Getting Started with Power BI | Power BI Tutorial | Power...Power BI Training | Getting Started with Power BI | Power BI Tutorial | Power...
Power BI Training | Getting Started with Power BI | Power BI Tutorial | Power...
 
Power bi
Power biPower bi
Power bi
 
Data analytics
Data analyticsData analytics
Data analytics
 
Power BI for Big Data and the New Look of Big Data Solutions
Power BI for Big Data and the New Look of Big Data SolutionsPower BI for Big Data and the New Look of Big Data Solutions
Power BI for Big Data and the New Look of Big Data Solutions
 
Business analytics and data visualisation
Business analytics and data visualisationBusiness analytics and data visualisation
Business analytics and data visualisation
 
Power BI Dashboard | Microsoft Power BI Tutorial | Data Visualization | Edureka
Power BI Dashboard | Microsoft Power BI Tutorial | Data Visualization | EdurekaPower BI Dashboard | Microsoft Power BI Tutorial | Data Visualization | Edureka
Power BI Dashboard | Microsoft Power BI Tutorial | Data Visualization | Edureka
 
Improve power bi performance
Improve power bi performanceImprove power bi performance
Improve power bi performance
 
Business Intelligence and Business Analytics
Business Intelligence and Business AnalyticsBusiness Intelligence and Business Analytics
Business Intelligence and Business Analytics
 
Tableau: A Business Intelligence and Analytics Software
Tableau: A Business Intelligence and Analytics SoftwareTableau: A Business Intelligence and Analytics Software
Tableau: A Business Intelligence and Analytics Software
 
Data analytics and powerbi intro
Data analytics and powerbi introData analytics and powerbi intro
Data analytics and powerbi intro
 
What Is Power BI? | Introduction To Microsoft Power BI | Power BI Training | ...
What Is Power BI? | Introduction To Microsoft Power BI | Power BI Training | ...What Is Power BI? | Introduction To Microsoft Power BI | Power BI Training | ...
What Is Power BI? | Introduction To Microsoft Power BI | Power BI Training | ...
 
Data Modeling with Power BI
Data Modeling with Power BIData Modeling with Power BI
Data Modeling with Power BI
 

Similar to Optimise DAX Code for Faster Power BI Reports

Similar to Optimise DAX Code for Faster Power BI Reports (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

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

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

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 . pdfQucHHunhnh
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
CĂłdigo Creativo y Arte de Software | Unidad 1
CĂłdigo Creativo y Arte de Software | Unidad 1CĂłdigo Creativo y Arte de Software | Unidad 1
CĂłdigo Creativo y Arte de Software | Unidad 1
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Optimise DAX Code for Faster Power BI Reports

  • 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.