SSRS divide by zero error expression - reporting-services

Hope your well.
I am working on a report and seem to get a #error. Seems like it is a divide by zero error but I can not work out a solution. The expression:
=( Sum(Fields!Line_Sell.Value) - Sum(Fields!Line_Cost.Value) ) / Sum(Fields!Line_Sell.Value)
I am relatively new to RS but have tried
ISNULL( )
but with no success.
Any help, I would be greatful.
Thanks

Let's say you have an expression set to x / y, where y has the potential to be zero. As you experienced, SSRS will change this value to #ERR (or sometimes NaN).
Your first instinct would be to test for zero
=Iif(y = 0, 0, x/y)
Unfortunately, this can result in a debugger warning that says you are attempting to divide by zero. This is because SSRS attempts to evaluate the entire statement during compilation.
As a result, we need to provide SSRS with a value that it can evaluate
=x * y ^ -1
This is the same as dividing by your y value, but SSRS sees this as dividing by infinity, and therefore can make an evaluation.
As a final step, you will want to make sure your code can properly export to Excel. Under 2012 SSRS (and possibly later, I haven't tested), SSRS has an issue where possible zero values export to Excel as 0.000000000000000 and cause loss of data errors.
To correct this, we explicitly output the value of zero
=Iif(x = 0 OR y = 0, 0, x * y ^ -1)

The expression in your comment doesn't look complete, so I can't tell what you tried next that didn't work. You definitely want to test for zero before performing division.
You can try this:
=iif( Sum(Fields!Line_Sell.Value) = 0, 0, (Sum(Fields!Line_Sell.Value) - Sum(Fields!Line_Cost.Value)) / Sum(Fields!Line_Sell.Value))
Or to check for an empty value also, you can try this:
=iif( (Sum(Fields!Line_Sell.Value) = 0 or IsNothing(Sum(Fields!Line_Sell.Value)), 0,Sum(Fields!Line_Sell.Value) - Sum(Fields!Line_Cost.Value)) / Sum(Fields!Line_Sell.Value))
If that's still not yielding results, break down the expression and run the report. Try Sum(Fields!Line_Sell.Value) by itself, run the report and see what you get. To be more efficient create a table with one column for Fields!Line_Sell.Value and another for Fields!Line_Cost.Value. In the table, include detail rows and add a total row so that you get the Sum function with each of those fields individually. You need to look at the detail records to try to extrapolate why the aggregate function isn't working. Bottom line - decompose your expression and test it piece by piece. The answer is in there somewhere.

Related

How to get group total inside a row in a rdl report?

How do I get the total of the group inside a row item?
I have tried Fields!Sales.Value/SUM(Fields!Sales.Value) but it returns 1 instead of the row_amount/group_total the report returns row_amount/row_amount
PS: Data comes from a SSAS cube. I made picture bellow1 to summarize what I wanna do
You could potentially just compare the values by the ReportItems. That is, you can specify a name for the sales textbox and for the totalsales textbox and do the comparison using those values. The following expression would likely work as intended.
= (ReportItems!SalesTextbox.Value / ReportItems!SalesTotalTextbox.Value) * 100.0
The other thing to consider with this is that if your result is 1, you may be dealing with integer division from your data. Make sure the data being returned is coming in with a double or decimal datatype, not an integer. It may work as you have it currently if you get the datatype corrected.
You also need to handle "divide by Zero' errors. Using an "=IIf (denominator = 0, 0, numerator/denominator) doesn't work since an "IIf" function evaluates both the "then" and the "else" and will still return an error. We use a bit of custom code that I found on SO (I wish I could remember where it came from so I could give credit where it's due).
Public Function HandleDivideByZero(ByVal numerator As Decimal, denominator As Decimal) As Decimal
If denominator = 0 Then
Return 0
Else
Return numerator / denominator
End If
End Function

How to stop rounding in ssrs report

I have an SSRS report that is rounding currency and I need the report to show the actual value. When I run the query in query designer all the values are shown correctly. If I output this to Excel and SUM the values I get the total I expect (e.g £56724.30)
When I run the report I get the value £56840.00 so it looks as if the data being used is getting rounded before output.
I have a Calculated field in the report called Total_Rent_Due_UC_Claims:
=iif(Fields!UC_Rent.Value = Fields!LastCharge.Value, Fields!UC_Rent.Value, 0) or
iif(Fields!UC_Rent_Date.Value = Fields!LastCharge.Value and
Fields!extra10a_d003.Value < Parameters!AsAtDate.Value, 0, Fields!UC_Rent_Date.Value)
I then use this to get a total:
=Sum(Fields!Total_Rent_Due_UC_Claims.Value)
I have formatted this field to currency to two decimal places.
Can someone assist with this so that the value in the report is the same as the expected value?
I don't think this difference can be attributed to rounding -- at least not any rounding I've ever seen before. That's a difference of £115.70. I think the calculated field isn't quite doing what you want it to and needs a little modification. I'm not exactly sure what it's doing right now, but basically both of those IIF statements will evaluate because OR doesn't really work the way you have it. I would try the following expression.
=IIF(Fields!UC_Rent.Value = Fields!LastCharge.Value,
Fields!UC_Rent.Value, IIF(Fields!UC_Rent_Date.Value = Fields!LastCharge.Value
AND Fields!extra10a_d003.Value < Parameters!AsAtDate.Value, 0, Fields!UC_Rent_Date.Value))
Of course, this could still be wrong as I can't tell if all of the fields are date datatypes or if there's some kind of mismatch going on here. Let me know if this doesn't work and I'll see if I can adjust it.

SSRS divide by 0 error even when using iif() isnothing() and =0 or =nothing

I have a simple % change formula I want to implement into a report... the formula being (a-b)/b.
Issue is sometimes the value for b coming into the data set is blank/null... and furthermore SSRS can't ever seem to accurately realize this through the iif() statement I'm using. Instead whenever the report renders, for any line item where data for b in the formula is missing I get the dreaded "ERROR" showing up in that cell. The formula in the cell I'm using is below. What's the issue here? Why can't SSRS see that sum(b) = nothing or isnothing(sum(b)) evaluates to true? It's like it bypasses that part of the formula and goes ahead and does the math, divides by 0 and throws and error?
=iif(
isnothing(sum(Fields!BR_Quantity.Value))=true or sum(Fields!BR_Quantity.Value)=0 or sum(Fields!BR_Quantity.Value)=nothing,
nothing,
(sum(Fields!Full_Case_Quantity.Value)-sum(Fields!BR_Quantity.Value)) / sum(Fields!BR_Quantity.Value)
)
The IIF function evaluates both the TRUE and FALSE parts of the expression.
Use a separate IIF on both the numerator and denominator:
=IIF(ISNOTHING(SUM(Fields!BR_Quantity.Value)) OR SUM(Fields!BR_Quantity.Value) = 0, 0, SUM(Fields!Full_Case_Quantity.Value) - SUM(Fields!BR_Quantity.Value)
/
IIF(ISNOTHING(SUM(Fields!BR_Quantity.Value)) OR SUM(Fields!BR_Quantity.Value) = 0, 1, SUM(Fields!BR_Quantity.Value) )
This way when your denominator is 0 or NULL, the resulting expression is 0 / 1 .
Kinda lame but Microsoft won't fix it.

SSRS calculation expression for specific value

I'm looking at doing a calculation expression in SSRS but I've become unstuck.
I'm trying to calculate when a field equals a specific value, then return a percentage calculation.
This is what I've tried so far:
=SUM(IIF(Fields!Alert.Value="Red",(FormatPercent(Count(Fields!Sales.Value) / 6532 ,0)))SUM(IIF(Fields!Alert.Value="Yellow",(FormatPercent(Count(Fields!Sales.Value) / 2541 ,0)))SUM(IIF(Fields!Alert.Value="Green",(FormatPercent(Count(Fields!Sales.Value) / 1025,0)))
Obviously this is incorrect and doesn't work. The expression needs to include all 3 colours.
UPDATED see if this works.
UPDATED per requested. I put the statement in the last False part, but I'm not sure if this is what you really want.
UPDATED again. Added False part to the last IIF statement.
UPDATED: Removed the SUM function. Try this to see if it works. Your IIF statement didn't have any False parts. Also, just SUM once around the whole statement if you want it summed. Not knowing your data, I'm not sure if you want the % summed up.
=IIF(Fields!location.Value="East" AND Fields!Alert.Value="Red",(FormatPercent(Count(Fields!Sales.Value) / 6532 ,0)),IIF(Fields!Alert.Value="Yellow",FormatPercent(Count(Fields!Sales.Value) / 2541 ,0),IIF(Fields!Alert.Value="Green",FormatPercent(Count(Fields!Sales.Value) / 1025,0),0)))

SSRS 2005 Matrix Reports - Editing Total

This seems as though it should be simple, but appears not to be.
In SSRS 2005 I've written a matrix report and added some fields, one of which has the formula: If (x / y >= n, 1, 0). I've called this field 'Accuracy'. The report aggregates this field across a number of individuals and then for a number of days.
Ideally I want a subtotal that gives a sum of the 'Accuracy' figures (so we can say we had n people who were accurate today). However, the subtotal calculates the formula for the totals of x and y. Subtotals is only ever going to be 1 or 0.
Any ideas as to how I can get a Count of Accuracy displayed on the matrix report? I've tried creating various fields along the lines of Sum(accuracy) and Count(accuracy) - these return an error when the report is run.
Thanks!
maybe report behaves 0 and 1 as a boolean value. you can make a dll that do this work for you and reference to your report and use the methods that exist for this work in dll.
Try using the running value. You could put the expression inside it or inside the report code block and just call it.
=RunningValue(what to count, sum, scope)
=RunningValue(Fields!Cost.Value, Sum, Nothing)
More info here:
http://msdn.microsoft.com/en-us/library/ms159136.aspx
Hope it helps.