#Error in a greater than iif sum statement - reporting-services

I'm trying to write some code that will prevent a #Error.
I don't know where my issue is.
=Sum(IIF(Fields!CurrentNumDaysOD.Value >= 1 And Fields!CurrentNumDaysOD.Value <= 10,
Fields!CurrentBalance.Value, "0")) / Sum(IIF(Fields!CurrentNumDaysOD.Value > 0,
Fields!CurrentBalance.Value, "0"))
I have also attempted this:
=Sum(IIF(Fields!CurrentNumDaysOD.Value >= 1 And Fields!CurrentNumDaysOD.Value <= 10,
Fields!CurrentBalance.Value, Nothing)) / Sum(IIF(Fields!CurrentNumDaysOD.Value > 0,
Fields!CurrentBalance.Value, Nothing))

If the result of the second half of your statement (after the /) is 0, then you will be dividing by 0, which will cause an error.

Related

MS Access Case sensitive query giving incorrect result

Why do these queries give different results? Reference is a single character column and I would expect to have a result giving counts for upper and lower case letter 'r'.
Select SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
The result is that both BIG_R and LITTLE_R are the same and equal the count of BIG_R's
However,
Select SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTE_R,
SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Again LITTLE_R and BIG_R are the same, but this time they equal the count of LITTLE_R's
This looks like a bug in the way MS Access processes this type of query, or have I missed something here?
Access (or probably rather JetEngine) thinks that StrComp is called twice with the same argument and optimizes away one of the two calls.
A workaround is to compare the ASCII character values (Asc("r") = 114, Asc("R") = 82):
Select
SUM(IIF(Asc([REFERENCE]) = Asc('R'), 1, 0)) AS BIG_R,
SUM(IIF(Asc([REFERENCE]) = Asc('r'), 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Yet another workaround:
Select SUM(IIF(StrComp([REFERENCE],Chr$(82),0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],Chr$(114),0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Here the two inputs to StrComp are clearly different. So, the second call not optimized away.

SSRS Divide by Zero error on totals with weighted average

When attempting to get a total weighted average interest rate I occasionally receive Error when there is only one item in certain columns. Having trouble with the Iif statement handling this:
=Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 0, SUM(Fields!Current_Principal_Balance.Value * Fields!WAIR.Value))/Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 1, SUM(Fields!Current_Principal_Balance.Value))
Moved your brackets slightly, this seems to work:
=Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 0,
SUM(Fields!Current_Principal_Balance.Value * Fields!WAIR.Value)/Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 1, SUM(Fields!Current_Principal_Balance.Value)))

How to format to display leading zero

I am trying to display the duration in this format: 05:02:09 which is hour, minute and second.
At the moment I can display it without the leading zero in this format: 5:02:09
=IIF(
Fields!DataValue.Value < 0, 0,
Floor(Fields!DataValue.Value / 3600) &":"&Format(
DateAdd("s",IIF(Fields!DataValue.Value < 0, 0, Fields!DataValue.Value),"00:00"),
"mm:ss"
)
)
How can add a leading zero when the hour is less than 10?
I found a solution, which I tried before but failed. Strangely, it worked this time.
=IIF(
Fields!DataValue.Value < 0, 0,
Format(Floor(Fields!DataValue.Value / 3600), "00") &":"&Format(
DateAdd("s",IIF(Fields!DataValue.Value < 0, 0, Fields!DataValue.Value),"00:00"),
"mm:ss"
)
)

Wrong SSRS Expression

I've got this expression in an SSRS form:
=IIf(Fields!Number_Of_Txns.Value > 2, (Fields!Avg_Interpurch_Interval.Value/(IIf(Fields!St_Dev_Interpurch_Interval.Value = 0, 10000, Fields!St_Dev_Interpurch_Interval.Value))), 0.2)
What it should do is: if the Number_Of_Txns is > 2 and the standard deviation is not = 0 then divide the interpurchase interval by the standard deviation on the other hand if the standard deviation = 0 or the Number_Of_Txns <= 2 than just return 0.2
Where is the error?
You should first check both conditions, so your expression should be:
=IIf(Fields!Number_Of_Txns.Value > 2 AND Fields!St_Dev_Interpurch_Interval.Value <> 0
,Fields!Avg_Interpurch_Interval.Value / Fields!St_Dev_Interpurch_Interval.Value
, 0.2)

How to replace Null fields with 0s

I have a field in a matrix that has some nulls. I would like to replace all the nulls with 0s. This is the expression I currently have in the field:
=Sum(IIf(Code.isVM(Fields!deviceType.Value), 0, 1))
I'm guessing I have to have an IsNothing expression in there but I can't figure out how to add this with this existing expression.
How about:
=Iif(
Sum(IIf(Code.isVM(Fields!deviceType.Value), 0, 1)) Is Nothing,
0,
Sum(IIf(Code.isVM(Fields!deviceType.Value), 0, 1))
)
I recall testing for length greater than 0 worked well.
=IIf(Len(Fields!deviceType.Value) > 0, Fields!deviceType.Value, 0)