I have this formula in a textbox on my report:
= IIF(Fields!CallInvoiceAmount.Value=0,0.00,
Fields!CallProfit.Value/Fields!CallInvoiceAmount.Value
)
Basically, I am just trying to calculate a percentage and I need to handle the cases where the denominator is zero to avoid a divide by zero error.
Now if all of the fields above are int fields, the formula works. However, they are numeric fields (money values). SSRS throws a #Error every time the CallInvoiceAmount value is 0.
After much aggravation trying to get this to work, I finally found some info on some message boards that spelled out the problem. SSRS apparently evaluates the true and false conditions when there are numeric values involved, BEFORE it evaluates the IIF condition.
Now isn't that just illogical enough to raise Spock's eyebrow!
The prevailing suggestion was to embed code in the report a write a custom function to handle it and then call that. However, there are issues invoved with that when working with aggregates, and I also need the following to work:
= IIF(sum(Fields!CallInvoiceAmount.Value)=0,0.00,
sum(Fields!CallProfit.Value)/sum(Fields!CallInvoiceAmount.Value)
)
The answer turned out to be simple, if still a work around. I added a very tiny amount to the denominator. This also allows me to remove the iif statement entirely:
= sum(Fields!CallProfit.Value)/(sum(Fields!CallInvoiceAmount.Value)+0.00001)
Of course, if the denominator or really zero you will get a huge percentage, but that is fitting since the real answer is undefined.
Did this help you? If so, please leave a comment!
David Jedziniak maintains this blog and posts helpful tips and tricks on SQL Server and Dynamics GP development. The opinions expressed here are those of the author and do not represent the views, express or implied, of any past or present employer. The information here is provided without warranty of fitness for any particular purpose.
Subscribe to:
Post Comments (Atom)
SQL 2022 TSQL snapshot backups!
SQL 2022 now actually supports snapshot backups! More specifically, T-SQL snapshot backups. Of course this is hardware-dependent. Here ...
-
If you are reading this, you are probably aware of the fact that you cannot deploy SSRS 2008 reports on an SSRS2005 server. SSRS reports ar...
-
SQL Job to refresh TEST from PRODUCTION Last Updated: 2018.11.12 I like to include each of these steps as a separate job step. If you ...
-
I ran into an issue today where I had a report parameter default that I couldn't seem to get rid of. In BIDS, I deleted the defaults f...
No comments:
Post a Comment