SSRS report group parameter expression causing divide error - reporting-services

I have a report that uses an expression to sort a group but when choosing one value from the parameter it returns an "The sort expression for the grouping 'grp' contains an error: Attempted to divide by zero."
The expression being used is below;
=Choose(Parameters!sort.Value,
Choose(Parameters!set.Value,Fields!IncomeOfficer.Value,Fields! ManagementArea.Value,Fields!RentGroup.Value,Fields!Tenure.Value),
-Sum(Fields!RentCollected.Value,"grp"),
Sum(Fields!RentDue.Value,"grp"),
-Sum(Fields!RentCollected.Value,"grp")/Sum(Fields!RentDue.Value,"grp"))
Any thoughts amendments appreciated.
Thanks

Add a function to your report code to handle divide by zero error:
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
Then in your expression call the function:
code.Divider( Sum(Fields!RentCollected.Value,"grp")/Sum(Fields!RentDue.Value,"grp") )

How about you simple check if your Sum(Fields!RentDue.Value,"grp") returns 0 or null, if it does you do not need to divide Sum(Fields!RentCollected.Value,"grp")/Sum(Fields!RentDue.Value,"grp")
else just divide.
I have updated expression below.
=Choose(Parameters!sort.Value,
Choose(Parameters!set.Value,Fields!IncomeOfficer.Value,Fields! ManagementArea.Value,Fields!RentGroup.Value,Fields!Tenure.Value),
-Sum(Fields!RentCollected.Value,"grp"),
Sum(Fields!RentDue.Value,"grp"),
-IIF(IsNothing(Sum(Fields!RentDue.Value,"grp")) or Sum(Fields!RentDue.Value,"grp")=0,Sum(Fields!RentCollected.Value,"grp"), Sum(Fields!RentCollected.Value,"grp")/Sum(Fields!RentDue.Value,"grp")))

Related

SSRS Lookupset sum problem when one or more value are nothing

I got a little problem with Lookupset on a SSRS Report.
The report has a father-child structure in the group section and the datasets come from different tabular.
The problem occurs when one of the Product from Dataset1 is missing on Dataset2, the entire sum returns 0.
I tried this VB code in order to sum up all the values the Lookupset is able to return.
Function SumLookup(ByVal items As Object()) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim suma As Decimal = New Decimal()
suma = 0
For Each item As Object In items
suma += Convert.ToDecimal(item)
Next
Return suma
End Function
I expected the sum of every Product Lookupset is able to retrurn me but it is just returning 0 if one of the Products is missing in Dataset2.
Is there a way to manage the Nothing returned by Lookupset?
Sorry for the formatting and my poor english and thanks in advance!
I would handle this by checking for the value in your LOOKUPSET expression that SumLookup is summing.
I assume that your expression looks something like this:
=CODE.SumLookup(LookupSet(Fields!ProductID.Value,
Fields!ProductID.Value,
Fields!Price.value,
"Dataset2") )
Use an IIF with ISNOTHING to check for the NULL values and set them to zero:
=CODE.SumLookup(LookupSet(Fields!ProductID.Value,
Fields!ProductID.Value,
IIF(ISNOTHING(Fields!Price.value), 0.00, Fields!Price.value),
"Dataset2") )
The function returns a Decimal datatype. By a Decimal datatype Nothing and 0 are the same. You can test this.
Put a tablix into your report with year from 2017 to 2019. Then put the year in a column of the tablix as a number format, then write the following expression in the detail textbox:
=CDec(IIF(CDec(Fields!Year.Value) = 2017, 0, Nothing))
After executing your report you will notice that every value in the year column is 0.
The same goes for the check. Both of these expressions will always return Yes. I basically check for 0 and the second one for for Nothing:
=IIF(CDec(IIF(CDec(Fields!Jahr.Value) = 2017, 0, Nothing)) = 0, "Yes", "No")
=IIF(CDec(IIF(CDec(Fields!Jahr.Value) = 2017, 0, Nothing)) = Nothing, "Yes", "No")
But remember your textbox/column has the be a number format.
So if you want to return Nothing and you display it in a number format textbox, it will show you a 0.

SSRS 2012 Divide By Zero Error

I've read through the answers and updated my expression and I continue to get #Error in the row that has a 0 for the budget.
All the percentages are being calculated correctly.
=IIF(SUM(Fields!CM_Budget.Value,"Name")=0,0,SUM(Fields!MTD_Actual.Value,"Name")/SUM(Fields!CM_Budget.Value,"Name"))
This is because SSRS always evaluates both expressions.
You have to break your expression in two parts.
=IIF(SUM(Fields!CM_Budget.Value,"Name")=0,0,SUM(Fields!MTD_Actual.Value,"Name"))
/
IIF(SUM(Fields!CM_Budget.Value,"Name")=0,1,SUM(Fields!CM_Budget.Value,"Name"))
An alternative could be using custom code like below
Public Function Divider (ByVal Dividend As Double, ByVal Divisor As Double)
If IsNothing(Divisor) Or Divisor = 0
Return Nothing
Else
Return Dividend/Divisor
End If
End Function
Then you can write your expression like:
=Code.Divider(SUM(Fields!MTD_Actual.Value,"Name"),SUM(Fields!CM_Budget.Value,"Name"))
I usually prefer the second way to do it, to make my expressions more readable

SSRS 2008r2 Using Aggregate on field with an Expression

SSRS 2008r2
I'm trying to perform an aggregate (Sum) on a field within a GROUPing that contains an expression.
The field where I want to SUM to appear is within a different GROUPing.
I've created the following Function within Report Properties
Dim public tot_OT_Hrs As Decimal
Public Function Add_OT_Hrs(ByVal OT_Hrs As Decimal) AS Decimal
tot_OT_Hrs = tot_OT_Hrs + OT_Hrs
return OT_Hrs
End Function
Public Function GetTotal()
return tot_OT_Hrs
End Function
I've added a call to the "Add_OT_Hrs" function in the field where the expression is and this works fine.
=Code.Add_OT_Hrs(
IIF(Sum(Fields!HrsWorked.Value) > Parameters!StdWorkingHrs.Value,
Sum(Fields!HrsWorked.Value) - Parameters!StdWorkingHrs.Value + Sum(Fields!Rate1Hrs.Value)
, Sum(Fields!Rate1Hrs.Value )
)
)
Fields!Rate1Hrs.Value is the field in which the expression resides in and Fields!HrsWorked.Value is an adjacent field
However, the field where I want to total to appear I've added the following
=Code.GetTotal()
All that is returned here is 0 (zero) on every row in the GROUPing. If I initialise the Dim public tot_OT_Hrs As Decimal variable to say 1.2 then 1.2 is returned on every row in the GROUPing. The Add_OT_Hrs function isn't working as expected.
Where am I going wrong?
Thanks in advance.
Looks to me that you are converting in the wrong order and also to the wrong data type. In the code example that you used you were converting the sum to a double which is not the correct data type. There is a notable difference between a double and decimal when using custom functions.
I tested this with a very small dataset as follows
select '1.2'
union all
select '1.8'
Then applied the following custom code
Dim public total as decimal
Public Function AddTotalr(ByVal r AS decimal) AS decimal
total = total + r
return r
End Function
Public Function GetTotalr()
return total
End Function
The order of your explicit conversion is vital. You have to convert the value before you sum the total so that you are working with consistent data types.
=code.addtotalr(sum(cdec(Fields!ID.Value)))
Even with the correct data type the this will produce an error because you are attempting to sum what is, most likely, being interpreted as a character string.
=code.addtotalr(cdec(sum(Fields!ID.Value)))
-----------edit ------------------------
As a test to see what the return on the function is change your function to
Dim public total as decimal
Public Function AddTotalr(ByVal r AS decimal) AS decimal
total = total + r
return total
This should give you a running total of the items processed and the last value in the list should match what your total should be.
I decided to do this in the proc.
It works out quite well now.

Sum of Averages in SSRS

I have a table created a report where there should be a field with sum of Avg.
Please find the below image
I am getting the average but i need to sum the average to display A.
Have you tried adding a custom function to the report, as outlined here?
This person wrote this custom function:
Dim Test as double
Public Function Testing(Byval value as Double, ByVal reset as Boolean) as Double
Test = Test + value
Testing = Test
If reset = TRUE Then
Test = 0
End If
End Function
At the footer of the group call Code.Testing([value], False) then call it again outside of the group with code.Testing([value],True) to reset. Worked for me :)

Replace #Error or NAN with -(hyphen) in SSRS

When i get a value into the text box (say "NAN" or "#Err") i want to replace it with -(hyphen).
How to achieve this in SSRS reporting ?
Thanks in advance.
You'll need to write an expression to handle it. Ideally, you would have some custom code function in your report (ie. Code.SafeDivision())
=iif(Fields!denominator.Value = 0, "-", Numerator / Denominator)
To elaborate on cmotley's answer, add the following as custom code to your report (to do this go to report properties, then code and then paste this in):
Function SafeDivide(value1 As Decimal, value2 As Decimal) As Decimal
If (value1 <> 0.0 And value2 <> 0.0) Then
Return value1 / value2
Else
Return 0
End If
End Function
Then you can use this in your textboxes to divide without receiving not a number and/or error, for example, adding the following as an expression (changing textbox names as required):
=Code.SafeDivide(ReportItems!Textbox186.Value, ReportItems!Textbox184.Value)
Divides two textboxes using our function.
To show a dash you can simply change the formatting of your textbox to 'number' and tick the box indicating that you'd like to replace '0' with '-'.
Alternatively if for some reason 'NaN' is coming through explicitly from a datasource (rare but not impossible when referencing a loaded table in SharePoint) you could use the following:
Function evalutator(value as String) as String
Select Case value
Case "NaN"
Return "-"
Case "NAN"
Return "-"
End Select
Return value
End Function
What about:
=IIF(Fields!yourcolumnname.Value = 0, "-", Fields!yourcolumnname.Value*1)
Or if you wanted zero's where you get NAN/#ERROR then you could use:
=IIF(Fields!yourcolumnname.Value = 0, Fields!yourcolumnname.Value*1, Fields!yourcolumnname.Value)