SSRS expression divide by zero in rdl - reporting-services

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.

Related

SSRS - IIF error in Report Builder

I am having an issue with a divide by zero error that I have half way worked through.
Basically I need (EstimatedValuePlanned - EAC) / EstimatedValuePlanned
I have the following, however I am still getting #Error on some:
= IIF (Fields!EstimatedValuePlanned.Value = 0 , 0,
Fields!EstimatedValuePlanned.Value - Fields!EAC.Value)
/
SUM(Fields!EstimatedValuePlanned.Value)
I have changed the code around several times but I still either get Error or NaN
Thank you
IIF will always evaluate both parts of the function, so when SUM(Fields!EstimatedValuePlanned.Value) is zero you will get the error even though that's not what will be returned.
Try using a SWITCH statement. SWITCH stops once an expression returns True.
Something like this
= SWITCH(
SUM(Fields!EstimatedValuePlanned.Value) = 0 , 0,
True, (Fields!EstimatedValuePlanned.Value - Fields!EAC.Value) / SUM(Fields!EstimatedValuePlanned.Value)
)
The True expression simply acts like an else
UPDATE WITH SAMPLE
I created a new report added a dataset and set the dataset query to be the following (just to generate some dummy data).
DECLARE #t TABLE (EVP float, EAC float)
INSERT INTO #t VALUES
(1,2),
(2,3),
(5,4),
(0,2),
(1,0),
(0,0)
SELECT * FROM #t
I then added a table, set the first to columns to be EVP and EAC respectively and set the 3rd column to an expression as follows.
=SWITCH (
Fields!EVP.Value = 0, 0
, True, (Fields!EVP.Value - Fields!EAC.Value) / Fields!EVP.Value
)
The report design looks like this.
And when it's run this is the result....
Try replicating the above steps and see if you can get it to work like this first then review difference to your report.
I got it to work with:
=IIF (Fields!EstimatedValuePlanned.Value = 0 , 0,
Fields!EstimatedValuePlanned.Value - Fields!EAC.Value)
/
IIF(Fields!EstimatedValuePlanned.Value =
0,1,Fields!EstimatedValuePlanned.Value)
Thank you both

AX SSRS use expression from another dataset in total

I have a header section which is showing totals etc. I have two fields in another dataset (dataset estimates) which I want to pull into header dataset.
Fee (circled) = IIf(Right(Fields!ProjCategoryId.Value, 3) <> "EXP", Fields!Value.Value, 0)
3rd Party (circled) = IIf(Right(Fields!ProjCategoryId.Value, 3) = "EXP", Fields!Value.Value, 0)
I know you can sum datasets from another dataset with no issues, but how to use IIF etc. as well?
You can use custom code and a lookupset().
Code:
Function SumLookup(ByVal items As Object()) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim suma As Decimal = New Decimal()
Dim ct as Integer = New Integer()
suma = 0
ct = 0
For Each item As Object In items
suma += Convert.ToDecimal(item)
ct += 1
Next
If (ct = 0) Then return 0 else return suma
End Function
To add this, right-click on the blue background of the report and select Report Properties.
Click on the Code option.
Paste it into this window
Call this with =Code.SumLookup(lookupset("EXP", right(Fields!ProjCategoryId.Value, 3), Fields!Value.Value, "DatasetName")) in your expression where you want the number to appear
This will add up all the Values where the ProjCategoryId starts with "EXP".
For <> "EXP", you might need to do several calls to it to add them all up for each thing that it can start with. For example..
Code.SumLookup(lookupset("ONE", right(Fields!ProjCategoryId.Value, 3), Fields!Value.Value, "DatasetName")) + Code.SumLookup(lookupset("TWO", right(Fields!ProjCategoryId.Value, 3), Fields!Value.Value, "DatasetName"))
This is actually really simple. Find the Textbox name that contains the expression.
(When we say textbox this does not have to be a textbox from the toolbox, it can be a field within tablix). Where you want to reference the value simply add the expression and you can reference the result.

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))

SSRS #ERROR Issue

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)

Does the iif function compute both paths in SSRS or is it short-circuited?

I am trying to evaluate a Price per Kilo ($/Kg) based on sales of a product. This works fine if the product was acutally sold during the period specified. However if the product is not sold the Kg (the denominator) ends up being 0 (zero) and an error results. - Divide by Zero error.
I tried this
=iif(KgSold=0,0,Revenue/KgSold)
It appears that the iif function is calculating both the true and false results. How do I get around this.
Should I be using the switch function instead?
=switch(KgSold=0,0
KgSold<>0,Revenue/KgSold)
You're right, it doesn't short circuit. That sucks.
You'll have to do something like this:
= Iif(KgSold = 0, 0, Revenue) / Iif(KgSold = 0, 1, KgSold )
The switch function should also work.
This happens because in VBScript all conditions within an IIF will be evaluated first before any functionality occurs.
Add the following to your code:
Public Function SafeDiv(byval num as double, byval den as double) as object
If den = nothing then return nothing
If den = 0 then return nothing
return num / den
End Function
Then call
=Code.SafeDiv(Revenue,KgSold)
in the text box epression