Use variables to improve performance and troubleshooting in PowerBI
Photo by Domenico Loia on Unsplash
You can incorporate variables in your DAX formulas to create less complex and more efficient calculations. While beginners in Power BI Desktop often overlook variables, they are highly effective and should be a default practice when creating measures.
Many expressions involve numerous nested functions and reuse of expression logic, making them time-consuming to process and difficult to read and troubleshoot. Using variables can reduce query processing time, enhancing the performance of a semantic model.
Benefits of using variables in your semantic model include:
Improved performance: Variables make measures more efficient by eliminating the need for Power BI to repeatedly evaluate the same expression. This can halve the original processing time for a query.
Improved readability: Variables, with their concise and descriptive names, replace ambiguous multi-word expressions, making formulas easier to read and understand.
Simplified debugging: Variables facilitate formula debugging and expression testing, aiding in troubleshooting.
Reduced complexity: Variables eliminate the need for EARLIER or EARLIEST DAX functions, which were complex and introduced new filter contexts. Using variables allows you to write simpler formulas without these functions.
Use variables to improve performance
To demonstrate how a variable can enhance measure efficiency, the following table presents a measure definition in two formats. The formula calculates "same period last year" in two ways: the first instance employs the standard DAX calculation method, while the second utilizes variables.
The improved measure definition is shown in the second row of the table. It uses the VAR keyword to introduce a variable named SalesPriorYear, assigning the "same period last year" result to this variable. The variable is then used twice in the DIVIDE expression.
Without variable
DAXCopy
Sales YoY Growth =
DIVIDE (
( [Sales] - CALCULATE ( [Sales], PARALLELPERIOD ( 'Date'[Date], -12, MONTH ) ) ),
CALCULATE ( [Sales], PARALLELPERIOD ( 'Date'[Date], -12, MONTH ) )
)
With variable
DAXCopy
Sales YoY Growth =
VAR SalesPriorYear =
CALCULATE ( [Sales], PARALLELPERIOD ( 'Date'[Date], -12, MONTH ) )
VAR SalesVariance =
DIVIDE ( ( [Sales] - SalesPriorYear ), SalesPriorYear )
RETURN
SalesVariance
In the first measure definition in the table, the formula is inefficient because it requires Power BI to evaluate the same expression twice. The second definition is more efficient because, due to the variable, Power BI only needs to evaluate the PARALLELPERIOD expression once.
If your semantic model has multiple queries with multiple measures, the use of variables could cut the overall query processing time in half and improve the overall performance of the semantic model. Furthermore, this solution is a simple one; imagine the savings as the formulas get more complicated, for instance, when you are dealing with percentages and running totals.
Use variables to improve readability
Besides enhancing performance, using variables can also make your code simpler to read.
When naming variables, it is best practice to use descriptive names. In the previous example, the variable is named SalesPriorYear, clearly indicating its purpose. Using generic names like X, temp, or variable1 would obscure the variable's intent.
Employing clear, concise, and meaningful names makes it easier for you to understand your calculations and simplifies future maintenance of the report by other developers.
Use variables to troubleshoot multiple steps
You can use variables to debug a formula and pinpoint issues. Variables simplify troubleshooting DAX calculations by allowing you to evaluate each one separately and recall them after the RETURN expression.
In the following example, you test an expression assigned to a variable. To debug, you temporarily rewrite the RETURN expression to return the variable. The measure definition returns only the SalesPriorYear variable, as specified after the RETURN expression.
DAXCopy
Sales YoY Growth % =
VAR SalesPriorYear = CALCULATE([Sales], PARALLELPERIOD('Date'[Date], -12, MONTH))
VAR SalesPriorYear% = DIVIDE(([Sales] - SalesPriorYear), SalesPriorYear)
RETURN SalesPriorYear%
The RETURN expression will display the SalesPriorYear% value only. This technique allows you to revert the expression when you have completed the debugging. It also makes calculations simpler to understand due to reduced complexity of the DAX code.
SWETA SARANGI
12-06-2024