This is really weird. I want to divide Amount by Units to get the average
amount per unit. So I used this:
=IIF(Fields!UnitsBilled.Value = 0, Nothing, Fields!AmountBilled.Value /
Fields!UnitsBilled.Value)
Pretty standard stuff. I have three other calculations on the same report
that do much the same on different fields and work perfectly. However, when
UnitsBilled = 0, I get #Error for the entry in the table rather than Nothing
and VS2003 reports a divide by zero error. Changing the Nothing part of the
clause to 0.0 or "Nothing" still has the same effect. It appears the false
part of the statement, the division calculation, is still being done even
when UnitsBilled = 0.
UnitsBilled and AmountBilled always have a value - there are no Nulls in my
data. Changing the test to Fields!UnitsBilled.Value = 0.0 has no effect.
So the test for UnitsBilled = 0 is failing, which means that UnitsBilled is
not zero, so the false part of the IIF statement, the division calculation,
is being done, resulting in a divide by zero error because UnitsBilled is
zero! Under what circumstances does zero not equal zero?
If I use the following calculation, it works fine (that is, when UnitsBilled
= 0 I get Nothing in the table's textbox):
=IIF(Fields!UnitsBilled.Value = 0, Nothing, Fields!UnitsBilled.Value)
If I change the calculation to this, I get NaN when UnitsBilled = 0:
=Fields!AmountBilled.Value / IIF(Fields!UnitsBilled.Value = 0, Nothing,
Fields!UnitsBilled.Value)
However, what is really weird is that when I use the following bizarre
calculation I get the desired result of Nothing in the table's textbox when
UnitsBilled = 0:
=IIF(Fields!UnitsBilled.Value = 0, Nothing, Fields!AmountBilled.Value /
IIF(Fields!UnitsBilled.Value = 0, Nothing, Fields!UnitsBilled.Value))
Environment is SQL Server 2000, RS2000 and VS2003.
Gearóid - 24 May 2006 11:50 GMT
Reporting Services evaluates all parts of an iif statement, even if you
only want it to evaluate the true or false part. What a usually do to
avoid divide by zero errors caused by the likes of this is call a code
behind function and evaluate it there. Just a suggestion.
Gearóid
> This is really weird. I want to divide Amount by Units to get the average
> amount per unit. So I used this:
[quoted text clipped - 32 lines]
>
> Environment is SQL Server 2000, RS2000 and VS2003.
Azrael Seraphin - 25 May 2006 04:26 GMT
Reporting Services evaluates all parts of an iif statement, even if you
only want it to evaluate the true or false part. What a usually do to
avoid divide by zero errors caused by the likes of this is call a code
behind function and evaluate it there. Just a suggestion.
Wow, that seems crazy. The whole point of using conditional statements is
that you don't want the invalid parts of the equation to be calculated. Now
I'm wondering why the other places I use this construct work as expected.
Azrael
dba56 - 25 May 2006 15:18 GMT
Technically, IIF is a function with all parameters evaluated, not a
conditional statement. You may want to try something like this to work
around it:
=IIF(Fields!UnitsBilled.Value = 0, Nothing,IIF(Fields!UnitsBilled.Value
=0,0,Fields!AmountBilled.Value) / Fields!UnitsBilled.Value)
This way when the denominator is 0, the results of the division should
be zero. It should not give you an error and avoid giving you a NaN
value.
> Reporting Services evaluates all parts of an iif statement, even if you
> only want it to evaluate the true or false part. What a usually do to
[quoted text clipped - 6 lines]
>
> Azrael
dba56 - 25 May 2006 15:26 GMT
Technically, IIF is a function with all parameters evaluated, not a
conditional statement. Don't know if MS plans to change this function
in the future. In the meantime, you found the workaround. The
following workaround also works and should give you the correct results
with a little less typing.
=IIF(Fields!UnitsBilled.Value = 0, Nothing,IIF(Fields!UnitsBilled.Value
=0,0,Fields!AmountBilled.Value) / Fields!UnitsBilled.Value)
This way when the denominator is 0, the results of the division should
be zero. It should not give you an error and avoid giving you a NaN
value.
Rob 'Spike' Stevens - 29 Jun 2006 19:24 GMT
I have had a different experience with the dba56's formula, but found a
slight modification that works, and I thought I would share it. I was still
getting the division by zero.
Based on the same reasoning that the IIF works as a function, the only real
differences are:
1. the second IIf clause is placed in the denominatior of the division you
are trying to make
2. When the Field is Zero, the second IIF clause returns a 1.
=IIF(Fields!UnitsBilled.Value = 0, Nothing,Fields!AmountBilled.Value /
IIF(Fields!UnitsBilled.Value =0,1,Fields!UnitsBilled.Value))
This worked for me, but if dba56's has been working for you, then stay with
what works.
> Technically, IIF is a function with all parameters evaluated, not a
> conditional statement. Don't know if MS plans to change this function
[quoted text clipped - 8 lines]
> be zero. It should not give you an error and avoid giving you a NaN
> value.
Peter Nolan - 28 May 2006 23:46 GMT
Hi Azrael,
as the other guys have said....perform divisions and protection agains
divide by zero in a custom assembly.....we are writing a product to run
on RS and all those pesky calculations such as divisions, percentages
of, etc are all done in custom assemblies so we can protect ourselves
from the feature of RS that both sides of the IIF statement are
evaluated every time....Indeed, we are doing huge amounts of work in
the custom assembly and we would suggest that the more things you can
put into a custom assembly the better...the custom assembly can be
written in any .net language but we are using vb.net right now...
Just an interesting point of note..... 0 <> 0 when either of the
numbers are of 'float' data type ;-)
Best Regards
Peter Nolan
www.peternolan.com