Saturday, October 24, 2015

How to resolve “Divide By Zero” Error in Navision RTC Report

question-686336_1920

One of the common error in RTC reports is #Error and there are many reasons for #Error in RTC report, but in this post I want to discuss one of the scenarios i.e. "Divide By Zero" exception.

If you have an expression which results Divide By Zero Exception then the result you see is #Error, and normally to avoid this you can write an IIF statement in expression.

For Example:

To display the value as zero if the denominator is zero then I would write something like this

=IIF(Sum(Fields!LineAmt.Value)=0,0,Sum(Fields!NetGP.Value)/Sum(Fields!LineAmt.Value))

 

In the above expression, I am doing a quick check to see if Sum(Fields!LineAmt.Value) is zero, then I want to display Zero otherwise I evaluate my expression, it is pretty simple. But here is the problem the reporting processing engine will still evaluate FALSE part, in this case Sum(Fields!NetGP.Value)/Sum(Fields!LineAmt.Value) and the result will be #Error even if Sum(Fields!LineAmt.Value) is Zero.

There are two possible solutions in this case to resolve the error:

1st Option:

 

=IIF(Sum(Fields!LineAmt.Value)=0, 0,Sum(Fields!NetGP.Value)/iif(Sum(Fields!LineAmt.Value)=0,1,Sum(Fields!LineAmt.Value)))

 

2nd Option:

VB Function

Public Shared Function DivideByZero(ByVal Numerator As Decimal, ByVal Denominator As Decimal) As Decimal

   If Denominator = 0 Then
     Return 0
   End If
   Return (Numerator / Denominator)
End Function

 

and use the following expression

Code.DivideByZero(Sum(Fields!NetGP.Value),Sum(Fields!LineAmt.Value)

 

Personally I prefer the VB function because it is neat and can be used in multiple places.

Please leave your comments, feedback or any suggestions you have for me to improve me my blog and also if you have any questions, feel free to post.

Share:

0 comments: