How to handle divide-by-zero errors in SSRS expressions?

Asked 7 hours ago Updated 7 hours ago 22 views

0

When writing expressions that divide numbers in SSRS, you may notice that using a simple IIF statement still results in a #Error when the denominator is zero:

=IIF(Fields!TotalSales.Value = 0, 0, Fields!Revenue.Value / Fields!TotalSales.Value)

Why does this fail?

In Visual Basic / SSRS expression evaluation, both the True and False parts of an IIF function are evaluated prior to returning a value. Therefore, division by zero occurs regardless of the condition check.

Correct Workaround:

=IIF(Fields!TotalSales.Value = 0, 0, Fields!Revenue.Value / IIF(Fields!TotalSales.Value = 0, 1, Fields!TotalSales.Value))

Replacing the denominator with 1 in the false branch prevents the evaluation engine from dividing by zero.

0 Answers


Write Your Answer