IIF in SSRS returning an error - reporting-services

I have an SSRS report that is throwing and #ERROR# in the field.
I am comparing Down Time to Total time...DownTime/TotalTime, to get a percentage of downtime over all time.
The original code was:
=Sum(Fields!PrevYear_Total_Down_Time.Value)
/
Sum(Fields!PrevYear_Total_Time.Value)
This threw an #ERROR# which I assumed was due to "Sum(Fields!PrevYear_Total_Time.Value)" being zero or null, so I adjusted the code to an IIF statement:
=IIF(Sum (Fields!PrevYear_Total_Time.Value)=0,0,
IIF(Sum(Fields!PrevYear_Total_Down_Time.Value)=0,0,
Sum(Fields!PrevYear_Total_Down_Time.Value)/Sum(Fields!PrevYear_Total_Time.Value)
)
)
If I replace the second nested IIF statement with a number, it works and puts whatever number I designate, But, that is NOT what I wanted. I want to test for zero, or null, and if the Total Downtime is null or 0, return 0.

Solved it by writing a code snippet that passes the two values then returns the formatted value.

The other way is to put another IIF in the denominator to account for the zero:
=IIF(Sum(Fields!PrevYear_Total_Time.Value) = 0, 0, Sum(Fields!PrevYear_Total_Down_Time.Value))
/
IIF(Sum(Fields!PrevYear_Total_Time.Value) = 0, 1, Sum(Fields!PrevYear_Total_Time.Value))

Related

SSRS : Empty string from webservice errors even with iif statements

I am using a web service to get back a value. Which when not needed is empty string. However no matter what I do to try and code for if it is empty is always errors :
=iif( Fields!X.Value="","",iif(IsNumeric(Fields!X.Value) ,CStr(CDbl(Fields!X.Value) * 100),""))
Thanks for any help
Ian
Try the following...
=IIF(
IsNumeric(Fields.X.Value),
Val(Fields.X.Value) * 100,
""
)
If X is not numeric you will get and empty string, if X is zero you will get 0 returned and if X is numeric you will get X *100

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 error Index was outside the bounds of the array. (rsRuntimeErrorInExpression)

I am building a report in SSRS and i am getting an the error: Index was outside the bounds of the array (rsRuntimeErrorInExpression).
I have tried different expressions to fix this but I can't make it work. I currently have:
=iif(LEN(Fields!Comment.Value) - LEN(REPLACE(Fields!Comment.Value, ",","")) > 3, Split(Fields!Comment.Value, ",")(3), Nothing)
Could anyone advice me how to solve this?
The error has to do with SSRS trying to evaluate the split expression even if the Iif condition is false.
Instead of the static value of 3 as the array index you can use a nested Iif to return a valid index value like 0, to make split always work.
=iif(
LEN(Fields!Comment.Value) - LEN(REPLACE(Fields!Comment.Value, ",","")) >3
, Split(Fields!Comment.Value, ",")(
iif(
LEN(Fields!Comment.Value) - LEN(REPLACE(Fields!Comment.Value, ",","")) >3
,3
,0
)
)
, Nothing
)

SUM IIF Expression returning an error

I have a simple expression that should work,but it keeps returning an error.
Please keep in mind, the Parameter is a Multi elect Parameter.
=SUM(IIF(Fields!Month.Value = Month(Today()) AND Fields!Year.Value = Year(Today()) AND Fields!Warehouse.Value = Parameters!warehouse.Value, Fields!Budget.Value, 0), "Budgets")
Since the parameter is multi-value, the values are passed as an array. One way to handle this is by combining the values into a comma-separated string.
So you would replace Fields!Warehouse.Value = Parameters!warehouse.Value with:
InStr(Join(Parameters!warehouse.Value, ","), Fields!Warehouse.Value) > 0

Hiding table or assigning temp data based on visibility expression ssrs 2008

I have a table in ssrs 2008. This table has a row visibility expression like:
=IIF(max(Fields!VExpected.Value) <> "", 1, 0) +
IIF(max(Fields!MExpected.Value) <> "", 1, 0) +
IIF(max(Fields!PExpected.Value) <> "", 1, 0) = 3, false, true)
Sometimes the datasource returns no data, or the returned data is not matching with this expression. In this case what I see is that a table with borders and column names but no data on it like:
id Vex Mex Pex
However, I want to show it as
id Vex Mex Pex
- - - -
Or if possible:
id Vex Mex Pex
No Data
Another question is, is there any way to hide the complete table if there is no returning data or any matching data with the expression?
Thanks
You can use CountRows function to determine how many rows your dataset is returning. If it is zero hide the table otherwise show it.
=iif(CountRows("DataSetName")=0,true,false)
Replace DataSetName by the actual name of your dataset.
For not matching expression data you can use the this expression.
=IIF(
max(Fields!VExpected.Value) <> "" AND
max(Fields!MExpected.Value) <> "" AND
max(Fields!PExpected.Value) <> "",False,True
)
The whole expression for matching expression and no rows cases could be something like this:
=Switch(
CountRows("DataSetName")=0,true,
max(Fields!VExpected.Value) = "",true,
max(Fields!MExpected.Value) = "",true,
max(Fields!PExpected.Value) = "",True,
true,False
)
Supposing VM, ME and PE expected values are numeric type I'd use ISNOTHING() function to determine when null values are being returned.
=Switch(
CountRows("DataSetName")=0,true,
ISNOTHING(max(Fields!VExpected.Value)),true,
ISNOTHING(max(Fields!MExpected.Value)),true,
ISNOTHING(max(Fields!PExpected.Value)),True,
true,False
)
Additional you can set a message when no rows are being returned from your dataset. Select the tablix and press F4 to see properties window. Go to NoRowsMessage property and use an expression to say your users there is no data.
="There is no data."
In this cases the tablix will not appear in your report but the message you set will be rendered in the location where the tablix should be.
Let me know if this helps.