Introduction
Nobody likes seeing errors in their reports and one of the most common causes for errors is a divide by zero. This then leads to the use of elaborate work arounds, all starting from people not using the Dax Divide Function in Power BI.
If you come from an Excel world, with experience in creating Excel based reports, then you will know how frustrating it is to an error because something is divided by zero. If you know of this problem, you most likely know how to fix it through an 'If Error' function or maybe even conditional formatting for errors.
However, understanding the DAX Divide Function is crucial for solving this in Power BI calculations. While this guide will help you master the function, remember that Sontai's expert team can handle all your Power BI needs, ensuring precision and saving you valuable time.
What is the DAX Divide Function?
The DAX Divide function performs the division calculation and handles potential division by zero errors. It ensures that your calculations do not produce errors, which can disrupt your data analysis, whilst returning a value instead of the error.
How to Use the Divide Function?
Let's consider to start with how the calculation that you may create using a simple divide. For example, if you want to work out a percentage change versus last year, you may have something like this:
SALES REVENUE % CHG VS YA =
('Sales'[SALES REVENUE THIS YEAR] - 'Sales'[SALES REVENUE LAST YEAR])
/ 'Sales Data'[SALES REVENUE LAST YEAR]
Whilst this isn't an incorrect formula and will give the right result, it isn't best practice and will result in errors appearing in your reports where the division finds that last year's sales were 0.
With an Excel hat on, you may start looking for fixes for this problem as you may have previously, which may result in some complex DAX. But in Power BI the Divide Function was designed to handle cases where a division is by zero.
The divide calculation allows you to put in the two parts of your calculation as you would have normally, but gives you the option to add in an 'alternateResult' which will be used in the event of an error, like a divide by zero.
Syntax:
DIVIDE(numerator, denominator, alternateResult)
Numerator:Â The number you want to divide.
Denominator:Â The number by which you want to divide the numerator.
AlternateResult:Â The result to return if the calculation returns an error.
Using the Divide Function in DAX changes our year-on-year percentage change function to be as follows:
SALES REVENUE % CHG VS YA =
DIVIDE(
('Sales'[SALES REVENUE THIS YEAR] - 'Sales'[SALES REVENUE LAST YEAR])
'Sales Data'[SALES REVENUE LAST YEAR],
0
)
In this case, the numerator and denominator haven't changed, but are now separated by a comma, whilst the final '0' is the 'Alternate Result' which is provided in the case of a division by zero. You can change the alternate result to be anything you need it to be, whilst you can also leave it blank, although that will return a blank.
Easy to implement, and straight out of the box functionality that can save you a lot of time and effort when trying to plan for a divide by zero case.
Key Benefits of Using Divide Function:
Error Handling:Â Prevents division by zero errors.
Clarity:Â Makes your code more readable and maintainable.
Efficiency:Â Simplifies complex calculations.
Maximise these benefits!
Sontai offers customised Power BI solutions tailored to your business needs.
Conclusion
Mastering the DAX Divide Function is a valuable skill for any Power BI report builder. However, if building reports and trying to master calculations to solve for all the complexities of your data, then for Sontai's expert team is here to help.
Get Started with Sontai Today!
Whether you need a one-time consultation or ongoing support, Sontai's services are designed to fit your needs. Contact us now to discover how we can enhance your data analytics.
Comments