posted 4/20/2010 by DustinRyan - Views: [21808]
If you've ever tried to use an IIF statement expression to fix an error received by dividing by zero, you probably still received the divide by zero error. Very frustrating.
An expression like this returns an error when Sum(Fields!Beta.value) = 0:
=sum(Fields!Alpha.Value)/sum(Fields!beta.Value)
So you, being the critical thinker that you are, try the following:
=iif(sum(Fields!Beta.Value)=0,0,sum(Fields!Alpha.Value)/sum(Fields!Beta.Value))
Alas, this will not work. Even though SSRS may evaluate your expression as true, it still will attempt to resolve the false part of your expression, which gives you the divide by zero error.
To get around this infuriating issue Microsoft should have dealt with in the first place, try this:
=iif(sum(Fields!Beta.Value)=0,0,sum(Fields!Alpha.Value)/iif(sum(Fields!Beta.Value)=0,1,sum(Fields!Beta.Value))
This solution should fix any issues you have dividing by zero.
I came up with the same solution, however when you work with a lot of data, the extra sum() slows down processing time. I've not come up with any other ideas though....
I ran into this issue, but it seemed to be a data type issue. Check out the code below, this is working for me
=IIF(CINT(Fields!NEWCLICKCOUNT.Value) = 0, "N/A", IIF(CINT(Fields!UNITS.Value) = 0, "N/A", CINT(Fields!UNITS.Value)/CINT(Fields!NEWCLICKCOUNT.Value)))
When in doubt, do it at the source query! If you are using the ReportItems collection however, this can be an even bigger issue.
I get around this by putting a public function in the Report Properties >> Code window. The first blog I ever wrote here on BIDN.
http://www.bidn.com/blogs/Daniel/ssas/1245/divide-by-zero-tweak
Cool, thanks, Daniel.