SSRS expression causing error - sql

I have the following expression in SSRS:-
=IIf(IsNothing(Fields!Hours.Value), 0, Sum(Fields!Hours.Value) / Sum(Fields!ReportingHours.Value))
The expression appears as #Error. I removed the division part of the equation, leaving just this:
=IIf(IsNothing(Fields!Hours.Value), 0, Sum(Fields!Hours.Value))
Then the correct numbers appear. If I perform an addition operation, the calculation also errors out. It seems that it does not like something about performing mathematical operations when using the Sum() function. If I do simple division without the Sum, as in this expression:
=IIf(IsNothing(Fields!Hours.Value), 0, Fields!Hours.Value / Fields!ReportingHours.Value)
Then numbers show up fine. It is only when I have the Sum() function on the expression that causes an error. Any ideas as to how to fix this?

Try the following expression:-
=IIf(IsNothing(sum(Fields!Hours.Value)), 0, Sum(Fields!Hours.Value) / Sum(Fields!ReportingHours.Value)
EDIT
I have edited my previous post.

Related

Handle divide by zero exception in this SSRS rdlc expression

The following code is showing result as Nan% if values are zero:
=FORMAT(((Sum(IIF(Fields!flag.Value=1,CINT(Fields!area1.Value),0)))
/ (Sum(IIF(Fields!flag.Value=1,CINT(Fields!UnitArea.Value),0))) *100),"N") + "%"
The simplest way to avoid the divide by zero error here is to not create it in the first place! If you replace the 0 as second return value in the divisor Iif expression with a 1, then the problem goes away.
That said I think your whole expression could do with simplifying somewhat. If I read it correctly you want to determine the proportion of area1 within UnitArea but only when the value of flag is 1. The expression could be thus written:
=Format(
Iif(
Fields!flag.Value = 1,
CInt(Fields!area1.Value) / CInt(Fields!UnitArea.Value),
Nothing
),
"Percent"
)
Note that I've dropped the multiplier and instead used the Format function to return the result of the division as a percentage (you could also simply further by removing the Format function entirely and handling the formatting in the designer).
You don't have to layout the expression with indentation as I have, but the expression builder ignores the whitespace and it does make larger expressions easier to read, so I think its a good habit to get into.

Whats wrong with this expression?

Comments: Switch(((IIf(([qty_req]-[qty_on_hand])<0,0,([qty_req]-[qty_on_hand])))=0) And ((([qty_on_hand]-[qty_req])/[qty_req])<=0.2),"Please check manually")
I have been struggling with this expression for too long. I keep getting the error "This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables." I've tried breaking down the expression to see if there was a bracket I the wrong place but I can't figure this out.
Note: The word "Comments" is just the field name (I primarily use the Design View in MS Access).
Update - The goal behind this is to eventually add more conditions to this switch statement, but this first one isn't working so that's why it seems like it doesn't make sense to use a Switch. Also, in pseudo code, this is what the intention of this expression is:
Switch([TransferQTY]=0 And [Req is within 20% of Inventory], "Please check manually")
In regards to the first IIF statement:
IIf([Req-Inventory is negative, that means that we have enough on hand and don't need to send],0, [Req-Inventory])
I think it's simply a check like this:
IIf([qty_req]-[qty_on_hand]<0 And ([qty_on_hand]-[qty_req])/[qty_req]<=0.2,"Please check manually","") AS Comments
The first IIF is just strangely built and has some redundancy to it. The second might give you strange answers because you don't have parans around your numerator. As it's written it could be simplified to:
As for the first IIF, you stated
"IIf([Req-Inventory is negative, that means that we have enough on
hand and don't need to send],0, [Req-Inventory])"
in the context of the switch (psuedo-coded):
Switch([TransferQTY]=0 And [Req is within 20% of Inventory], "Please
check manually")
This is basically saying "If the quantity requested minus thequantity on hand is less than or equal to 0", so instead of an IIF to do the "Less than or equal to" bit, just use <=:
Switch(((qty_req - qty_on_hand) <= 0) AND (((qty_on_hand - qty_req)/qty_req) <= 0.2), "Please Check Manually")
This will work better because Access is balking about the complexity. This dramatically reduces the complexity and accomplishes the same thing.
Also, I've gone a little heavy handed with the parantheses here. You could remove the ones that delineate each of the conditions that the AND function is evaluating and it would be fine.
I've removed the bit here about not using switch that was in a previous version of this answer since OP stated that switch() will be used after this bit starts working.

Division Always Yields 1 or 0 in SAP BusinessObjects Data Services

Going to be answering my own question here, but I wanted to post this to get some attention in case anyone else ever comes across this.
Within the BusinessObjects Data Services Designer, I have two decimal(36, 10) values, and I'd like to divide one by the other. In order to account for divide-by-zero situations, I have to first check if the denominator is zero, so I end up with an ifthenelse statement like the following:
ifthenelse(Query.Denominator = 0, 0, Query.Numerator / Query.Denominator)
When I execute my job, however, I end up always getting 0 or 1, rather than a decimal value.
And as I said, I already had an answer here, just wanted to provide this for the community.
An ifthenelse statement is interesting, in that it takes its data type from the second parameter. In the code example above, it assumes the correct data type is the data type of the parameter "0", which happens to be an integer.
When the ifthenelse receives a value from "else", it converts it to an integer. So a value of 0.356 becomes 0 and 0.576 becomes 1.
The trick is to cast the "0" to a decimal(36, 10):
ifthenelse(Query.Denominator = 0, cast(0, 'Decimal(36, 10)'), Query.Numerator / Query.Denominator)
Thanks for this, I thought I was going crazy!
Incidentally, for something slightly simpler, this also works:
ifthenelse(Query.Denominator = 0, 0.0, Query.Numerator / Query.Denominator)

divide by zero/null workaround in SSRS 2008 report

I have a report with a field whose value was the expression:
Fields!TotalPrice.Value/Fields!TotalSlots.Value
Although sometimes TotalSlots was blank and thus I was getting a divide by zero runtime error. So I changed the expression to this:
=IIF(Fields!TotalSlots.Value > 0, Fields!TotalPrice.Value/Fields!TotalSlots.Value,"unknown")
but I'm still getting a divide by zero error. How do I work around this zero divisor issue.
Jamie F's answer is correct. As a tip, you can add a function to your report code to make the division a bit easier to implement in multiple cells, e.g.
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
You can then call this in a cell like so:
=Code.Divider(Fields!FieldA.Value, Fields!FieldB.Value)
The VB IIF evaluates all arguments, so it will throw an error if any argument throws an error:
Your formula can be written as:
=IIF(Fields!TotalSlots.Value > 0,
Fields!TotalPrice.Value /
IIF(Fields!TotalSlots.Value > 0,
Fields!TotalSlots.Value,
1 ),
"unknown")
Then even when TotalSlots is zero, the formula still won't encounter a division problem.
I don't think your error is on the calculation. First of all, SSRS deals automatically with this situation. See my third column. And the forth shows your expression:
Your problem is probably somewhere else
This only seems to happens when the division is one of the results of an IIF, not if you just write a formula to divide one by the other, e.g.
=IIF(thing=1,10/0,0)
Before it has evaluated thing, it has already tried to calculate both results, causing an error. You can't use IIF in this way to protect from zero, you have to put the IIF on the bottom line of the division, e.g.
=IIF(thing=1, 10/IIF(divisor=0,-99999999999,divisor),0)
This is not satisfactory, since we've introcudes a weird small non zero number as the result, but it may be ok if you just want a non-error.
Technically, the #error is the correct answer.
Function IIF(arg1, arg2, arg3) always calculates all arguments, before returns a result, so your 2nd argument Fields!TotalPrice.Value/Fields!TotalSlots.Value can return #Error.
Try to use IF(arg1, arg2, arg3) function instead IIF.
=IF(Fields!TotalSlots.Value > 0,
Fields!TotalPrice.Value/Fields!TotalSlots.Value,
"unknown")

Divide by zero error in SSRS Report

I am writing a report in SQL Server 2005 Reporting Services, involving the division of money values that might be equal to zero. I put the following code in to check for a zero denominator:
=IIf(Sum(Fields!PreviousPremiumMTD.Value) = 0, "N/A", FormatPercent((Sum(Fields!PremiumMTD.Value) / Sum(Fields!PreviousPremiumMTD.Value))-1, 0))
However, for some reason I am still getting #Error displaying on my report with the following warning thrown:
[rsRuntimeErrorInExpression] The Value expression for the textbox ‘textbox62’ contains an error: Attempted to divide by zero.
Any assistance is greatly appreciated.
IIF evaluates the expression before passing that to function, thats why you will always get the DivideByZero error here.
See an example post: http://secretgeek.net/iif_function.asp