SSRS IIF Returning #ERROR - reporting-services

I have an expression in my SSRS report that calculates a field Total across multiple groups:
=IIf(SUM(Fields!sales_Dollars.Value) > 0 ,(SUM(Fields!Total_Defective__.Value)/IIF(SUM(Fields!sales_Dollars.Value) <= 0, 1, SUM(Fields!sales_Dollars.Value))),0)
I had to alter it by adding groups within the statement:
=SUM(IIf(SUM(Fields!sales_Dollars.Value,"Item_Process_Group") > 0 ,(SUM(Fields!Total_Defective__.Value,"Item_Process_Group")/IIF(SUM(Fields!sales_Dollars.Value,"Item_Process_Group") <= 0, 1, SUM(Fields!sales_Dollars.Value,"Item_Process_Group"))),0))
The problem is that there is a 0 in the denominator of one of the rows in the calc that is not in the group specified in the clause. This causes an #error. (at least I believe this is the cause)
is there a way to slip in a check for zero in Fields!sales_Dollars.Value with no group? sort of like how it does it in the first code example.
I need the group names in the bottom portion to pull the correct values.

The first thing I like to do when I look at these is simplify them, in this case there is a redundancy to be removed. There is no way that
SUM(Fields!sales_Dollars.Value) > 0
and
SUM(Fields!sales_Dollars.Value) <= 0
Could be true at the same time, which renders your second Iif statement always evaluating to false if the first one is true, if the first one is false the second Iif is skipped and a value of 0 is returned. So, we can set it to the false value drop the second Iif to end up with:
=IIf(
SUM(Fields!sales_Dollars.Value) > 0
,(SUM(Fields!Total_Defective__.Value)/SUM(Fields!sales_Dollars.Value))
,0)
Now the question is do you want to do something different if sales dollar value is zero. If that's the case we replace the zero with what the equation would have been if the second Iif statement were ever evaluated:
=IIf(
SUM(Fields!sales_Dollars.Value) > 0
,(SUM(Fields!Total_Defective__.Value)/SUM(Fields!sales_Dollars.Value))
,SUM(Fields!Total_Defective__.Value))
Hopefully that fixes the syntax problem you were experiencing, with the first statement.

Related

Sum results of expression containing CountDistinct in Report Builder

I have a report that requires counting the number of days an event occurred. I have created a Calculated Field that weeds out the dates:
=IIF(Fields!ID_ACW.Value > 0, Fields!CallDate.Value, Nothing)
By placing this in a CountDistinct expression, each row shows the correct number of distinct days this event occurred.
However, I need to add these results together for each row group's total. I have tried using
=Sum(CountDistinct(=IIF(Fields!ID_ACW.Value > 0, Fields!CallDate.Value, Nothing)))
and
=RunningValue(CountDistinct(=IIF(Fields!ID_ACW.Value > 0, Fields!CallDate.Value, Nothing)),Sum, "DataSet1")
, but neither gives me the correct total. Is there something I have missed or is it possible to add an expression to total each row's value?
I was able to get this to work by adding the row-group's value to the date in the Calculated Field like so:
=IIF(Fields!ID_ACW.Value > 0, Fields!CallDate.Value & " " & Fields!CallHour.Value, Nothing)
Now I can use
'''
CountDistinct(ACWDay)
'''
And get correct totals across all row-groups.
Is there a cleaner method to do this?
This is from memory so it might not be 100% but you should get the idea...
=Sum(
CountDistinct(=IIF(Fields!ID_ACW.Value > 0, Fields!CallDate.Value, Nothing), "YourRowGroupName")
)
Basically you need to do your CountDistinct within each rowgroup and then sum the results.

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 #Error in combination with IIF and Cdbl

hope someone more experienced can give me answer/solution to my problem.
In a database I have two columns, one to control do we show data and the data to be shown is in the second column.
I have data and expression as below:
ShowPriceOnMatrix | RSP
-------------------------------
0 | 1.48
1 | 10.26 euro -> This one fails with #Error
1 | 4.59
0 | 7.12
=IIF(Fields!ShowPriceOnMatrix.Value = 1, CDbl(Fields!RSP.Value), "")
This expression sometimes gives me #Error.
The problem is that the RSP field was defined as nvarchar and I cant change it now so I'm trying to convert to number and show as. Some users when input price on the filed they also type a text ex. "12.45 euro" or similar. When the expression hits a value with text the whole IIF fails with an #Error even if the first column is stated not to show the price.
The problem with IIF is that it is not skipping the second parameter if the first one evaluates to false - and therefore produces #Error in my case.
I've tried to use
IsNumeric(Cdbl(Fields!RSP.Value))
and
IsError(Cdbl(Fields!RSP.Value))
but both of them give an #Error also. From SSRS explanation for those functions, they should return a Boolean value but somehow I'm getting #Error :)
Just convert the field to numeric using
VAL(Fields!RSP.Value)
Also, you might want to consider using SWITCH instead of IIF. SWITCH stops at the first expression which evaluates to True, whereas IIF will evaluate all expressions regardless of if they will be used or not.
In your case, with a single IIF, SWITCH is not relevant but in cases where nested IIFs are used or situations where you may get a divide by zero error, SWITCH is often easier to read/debug. So something like this where we have a hypothetical situation that we want to test for divide by zero and return zero or if the thing we are dividing by is negative we want to return -1, otherwise do the calc...
=IIF(Fields!MyDivisor.Value = 0, 0, IIF(Fields!MyDivisor.Value <0, -1, Fields!MyNumber.Value/ Fields!MyDivisor.Value))
would become
=SWITCH (
Fields!MyDivisor.Value =0, 0,
Fields!MyDivisor.Value <0, -1,
True, Fields!MyNumber.Value/ Fields!MyDivisor.Value
)
each expression/result pair is evaluated in order until it hits the first True expression, so if MyDivisor was zero it would return 0 and stop, if it was negative, it would return -1 and stop. The final True acts like an else (as True always returns True ! ) so it would only do the calculation if all other expressions returned false.
You can use this expression to extract your numeric values:
=System.Text.RegularExpressions.Regex.Replace(Fields!RSP.Value, "[^0-9,]", "")
But you have to take care of the . in 1.48. With , and the language DE it worked for me. With . it would be:
=System.Text.RegularExpressions.Regex.Replace(Fields!RSP.Value, "[^0-9.]", "")

Need to exclude zero values from SSRS SSDT Report calculations in Field expression window

I have Productivity calculation as TotalMinutes/(TotalHours*60)
Some of my TotalHours values=0 and I need to exclude them from calculation.
How do I write it in Field Expression window?
My current expression is
(I am also setting zeros to "99" values in this field):
=IIf(Fields!TotalHours.Value=0,99,
Sum(Fields!TotalMinutes.Value/(Fields!TotalHours.Value*60)))
Thank you for help
This is untested but should work...
The problem is that IIF will evaluate both expressions even if one is never used so to avoid a divide by zero we need to swap the potential zero for a 1. This won't affect the result as it will never be used as the final output.
=SUM(
IIF(Fields!TotalHours.Value=0,99
, IIF(Fields!TotalHours.Value=0,0, Fields!TotalMinutes.Value)
/ IIF(Fields!TotalHours.Value=0, 1, Fields!TotalHours.Value * 60)
)
)
So what happens here is that if TotalHours is zero we do a 0/1 in the False part of the outer IIF just to satisfy the IIF statement (it will never actually be used)

SSRS Avg function returns result that is different than expected

Currently I am trying to average a set of numbers, but with certain conditions.
Is it possible to use an iif() within an avg() and return the correct result?
Furthermore, as of now my computations return a decimal returned to a power (8.9267....E -05).
I tried circumventing the AVG function by conditionally summing and then dividing by a conditional count but it gives me the same results.
Can someone explain why this is returned and offer help?
Currently I have:
=avg(iif((This_case) AND (That_case) AND (This_conditional)
, Fields!ResponseRate.Value
, 0))
Essentially I want the average ResponseRate if certain conditions are met.
The sum function works fine for the conditions but the average doesn't.
You can definitely use IIf within Avg and get the correct results.
Do you want to exclude the False values from the calculation entirely?
In your example you're still including them, just setting them to 0 and hence still including them in the calculation. This might explain your unexpected results.
If you want to exclude them entirely use Nothing instead of 0.
Edit after comment
You can nest an expression in another IIf statement and check for NULL values using IsNothing.
Say your condition average expression is:
=Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing))
You can return 0 for NULL values with something like:
=IIf(IsNothing(Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing)))
, 0.0
, Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing)))
I'd like to add my two cents to this one, a little late, but also valuable. I was able to use the above code to Average out a data set if another record appears X number of times.
=AVG(IIF(Count(Fields!AcctNo.Value, "AcctNo1") = 2, Fields!Limit.Value, Nothing))
So, if the acctno field appears 1 time, avg the limit field for that row group. Ian's example was very helpful