SSRS #ERROR Issue - reporting-services

I am getting an #ERROR in SSRS. I believe it's happening when it tries to divide 0 by 0.
How can I change this to handle that scenario.
=Fields!Total_Incidents.Value/Fields!Units_Sold.Value

You can add a function in custom code of the report, ex:
function divide(a as decimal, b as decimal) as decimal
if (b > 0.0) then
divide = a/b
else
divide = 0
end if
end function
Then, call this function in your textbox expression

This was how I ended up doing it
=IIf(Fields!Units_Sold.Value = 0, 0, Fields!Total_Incidents.Value / IIf(Fields!Units_Sold.Value = 0, 1, Fields!Units_Sold.Value))

Check denominator is zero or not,
=IFF(Fields!Units_Sold.Value=0,0,Fields!Total_Incidents.Value/Fields!Units_Sold.Value)

Related

SSRS multiple iif statements rounds instead of keeping 2 decimal calculation

I have the following statement in a calculated field that will only return whole numbers instead of a 2 decimal dollar amount. If I only use one of the 3 iif conditions it works fine but as soon as I add the second or all 3 it will not return a decimal value. Ive also tried all 3 iif conditions separaately and they all work. It appears to be just the OR's causing the issue???
=
(iif ((Fields!loc.Value = "C08") OR (Fields!loc.Value = "C09"), Fields!price1.Value, Fields!price1.Value - Fields!price2.Value) )
OR
(iif ((Fields!loc.Value = "C33") OR (Fields!loc.Value = "C32") OR (Fields!loc.Value = "C10") OR (Fields!loc.Value = "C30"), 0, Fields!price1.Value - Fields!price2.Value) )
OR
(iif (Fields!price1.Value = 0, 0, Fields!price1.Value - Fields!price2.Value))
You can't write and IIF like that, you would need to nest you IIFs which would get a bit messy.
You might find it easier to use a SWITCH statement which is a bit easier to read.
I might not have understood your logic properly but it should be close enough. Note that the first expression that returns true is what will get returned so in thsi case I have assume that if price1 is 0 then the returned value will always be zero, if this is incorrect, just re-order the expression pairs
=SWITCH(
Fields!price1.Value = 0, 0,
Fields!loc.Value = "C08" OR Fields!loc.Value = "C09", Fields!price1.Value,
Fields!loc.Value = "C33" OR Fields!loc.Value = "C32" OR Fields!loc.Value = "C10" OR Fields!loc.Value = "C30", 0,
True, Fields!price1.Value - Fields!price2.Value
)
This reads... "If price1 is zero then return 0" else "if loc is C08 or C09 return price1" else "if loc is C33, C32, C10 or C30 then return 0". Finally, if no previous test return true, then return price1-price2 .
The final True acts like and else
This could be simplified but I wanted to leave it as close to your original attempt as I could.

SSRS expression divide by zero in rdl

How should I handle divide by zero error in rdl expression for SSRS 2017?
I have tried the below approaches but nothing works.
1.
=iif((iif((SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)))=0,0,
SUM(Fields!ClosingStock.Value)/(iif((SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30))))*30)
(SUM(Fields!ClosingStock.Value)/
(iif((SUM(Fields!Consumption.Value)/
(iif(Fields!daysINDate.Value<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif(Fields!daysINDate.Value<=0,1,Fields!daysINDate.Value))*30)))*30)
You should only be checking the denominator for zero - not the division operation.
=IIF(Fields!daysINDate.Value = 0, 0, SUM(Fields!Consumption.Value))
/
IIF(Fields!daysINDate.Value = 0, 1, Fields!daysINDate.Value)
*30
SSRS checks both sides of the IIF statement... if either the THEN or ELSE part if the IIF is 0, it will throw up an error.
One way to get around it is to use VB code.
Right click on your report -> report properties -> code and paste the following code :
Public Function NDZ(Byval a As Decimal,Byval b As Decimal, Byval c As Decimal) As Decimal
' Fix for divide by zero problem in VB
' calculates a/b and either returns result or c if b = 0
if b = 0 then
return c
else
return a/b
end if
end function
Now on your report, enter the following expression in your text box you want to perform the calculation :
=code.NDZ(SUM(Fields!Consumption.Value), Fields!daysINDate.Value, 0)
so if either Fields!daysINDate.Value or SUM(Fields!Consumption.Value) is 0, then it will return 0 and not an error.

Remove NaN in Nested iif in SSRS expression

New to SQL-SSRS
How do I add functionality to the below code to change returned NaN values to 0 or blank? What I think is happening is in situations that the sum of Agreed and Disagreed = 0 I am getting this Nan. I think this would be easier for me if I wasnt dealing with the Agreed and Disagreed categories. Again, new to SQL programming. Any help would be greatly appreciated.
=(Sum(iif(Fields!Response.Value = "Agreed",Fields!Days.Value,0)) + Sum(iif(Fields!Response.Value = "Disagreed",Fields!Days.Value,0)))/(Sum(iif(Fields!Response.Value = "Agreed",Fields!Fq.Value,0)) + Sum(iif(Fields!Response.Value = "Disagreed",Fields!Fq.Value,0)))
Try something like below,
=Replace((Sum(IIF(Fields!Response.Value = "Agreed",Fields!Days.Value,0)) +
Sum(IIF(Fields!Response.Value = "Disagreed",Fields!Days.Value,0))),"NaN","0")
/ Replace((Sum(IIF(Fields!Response.Value = "Agreed",Fields!Fq.Value,0))
+ Sum(IIF(Fields!Response.Value = "Disagreed",Fields!Fq.Value,0))),"NaN","0")
Also, look at this
Rather than replacing the text returned from dividing by zero, you should handle the DIV 0 issue in the expression:
=IIF(
SUM(IIF(Fields!Response.Value = "Agreed" OR Fields!Response.Value = "Disagreed", Fields!Fq.Value, 0)) = 0, 0,
SUM(IIF(Fields!Response.Value = "Agreed" OR Fields!Response.Value = "Disagreed", Fields!Days.Value, 0)))
/IIF(
SUM(IIF(Fields!Response.Value = "Agreed" OR Fields!Response.Value = "Disagreed", Fields!Fq.Value, 0)) = 0, 1,
SUM(IIF(Fields!Response.Value = "Agreed" OR Fields!Response.Value = "Disagreed", Fields!Fq.Value, 0)))
If the SUM is zero, the calculation is 0 / 1 otherwise it's the SUM(Days) / SUM(Fq) where the Response is Agreed or Disagreed.
And rather than two sum, just use an OR in the one.
Public Function Divider (ByVal Dividend As Double, ByVal Divisor As Double)
If IsNothing(Divisor) Or Divisor = 0
Return 0
Else
Return Dividend/Divisor
End If
End Function
this function enables you to write a custom code which fixes all problems with Nan .
this can be called by below
=Code.Divider(Fields!FieldA.Value, Fields!FieldB.Value)
your code is as below
=Code.Divider(((Sum(iif(Fields!Response.Value = "Agreed",Fields!Days.Value,0)) + Sum(iif(Fields!Response.Value = "Disagreed",Fields!Days.Value,0))), ((Sum(iif(Fields!Response.Value = "Agreed",Fields!Fq.Value,0)) + Sum(iif(Fields!Response.Value = "Disagreed",Fields!Fq.Value,0)))))

DateDiff negative results to 0 in SSRS

I'm trying to set my results for an expression in SSRS to return 0 if the datediff results is negative. my expression is:
=DateDiff(DateInterval.Minute, Fields!Check_In_Time.Value, Fields!Date___Time.value)
and tried:
=Max(0,(DateDiff(DateInterval.Minute, Fields!Check_In_Time.Value, Fields!Date___Time.value))) --this one errors and doesn't run
but some results are negative, I want it to return a 0 if it's negative. Can anyone help?
This actually worked for me:
=IIF(DateDiff(DateInterval.Minute, Fields!Check_In_Time.Value, Fields!Date___Time.Value) <= 0, nothing, DateDiff(DateInterval.Minute, Fields!Check_In_Time.Value, Fields!Date___Time.Value))
Unfortunately, there's no function to do this in SSRS.
You'd just need to use an IIF to check for it and use 0 if the Date_Time was before the Check_In_Time.
=IIF(Check_In_Time > Date_Time, 0, DateDiff(DateInterval.Minute, Fields!Check_In_Time.Value, Fields!Date___Time.value))

Divide by zero SSRS

I need help in adding logic to the following code to handle divide by zero
=iif(Fields!COVERAGE.value = 0, Sum(Fields!CalculatedTotalIncidents.Value) / (ReportItems!CalculatedUnitsSold1.value), Sum(Fields!CalculatedTotalIncidents.Value) / Sum(Fields!CalculatedUnitsSold.Value))
It should be very simple if you implement a function in custom code of report
function div (a as integer, b as integer) as decimal
if (a > 0 ) then
div = a/b
else
div = 0
end if
end function
Then call this function in your report. You also can extend this function base on your specific business rule.
Hope this help.
Try this:
=iif(Fields!COVERAGE.value = 0,
Sum(Fields!CalculatedTotalIncidents.Value)/
iif((ReportItems!CalculatedUnitsSold1.value)<>0,
(ReportItems!CalculatedUnitsSold1.value),1),
Sum(Fields!CalculatedTotalIncidents.Value) /
iif(Sum(Fields!CalculatedUnitsSold.Value)<>0,
Sum(Fields!CalculatedUnitsSold.Value),1))