SSRS 2012 Custom Code/Expression for Divide by Zero and calculations - reporting-services

I know similar questions have been asked. However, I have not found anything that exactly helps me out in my situation.
I need to avoid divide by zero but before I do a divide with another column value, I am performing a math calculation:
=Sum((Fields!col1.Value) - Sum(Fields!col2.Value) / Sum(Fields!col2.Value))
I basically want to do a subtraction first, then a divide but I am unable to get the expression or the custom code to work for me. And before I spend too many hours on this one, I figured I would reach out for help.
This is the code I have come up with so far and it works, but I want the column to show nothing instead of 0:
=IIf(Sum(Fields!LWCasesBudget.Value) = 0, 0,
Sum(Fields!LWShipQuantity.Value) - Sum(Fields!LWCasesBudget.Value))/
IIf(Sum(Fields!LWCasesBudget.Value) = 0, 1, Sum(Fields!LWCasesBudget.Value))

It seems like you are happy with your calculation. If the 0 is the only issue then try and replace the 0 with the word "nothing".
Example:
=IIf(Sum(Fields!LWCasesBudget.Value) = 0, nothing,
Sum(Fields!LWShipQuantity.Value) - Sum(Fields!LWCasesBudget.Value))/
IIf(Sum(Fields!LWCasesBudget.Value) = 0, 1, Sum(Fields!LWCasesBudget.Value))
Hope that helps.

Related

IIF condition in SSRS and handling zeros

I've got a fairly complex formula in SSRS and for some reason it shows an error when it encounters a zero value, even though I'm handling zeros in my formula. Can anybody advise how to fix this issue?
=iif(datepart("h",Now())=6, iif(SUM(Fields!PYRevenue.Value) = 0, 0,((SUM(Fields!Revenue.Value) - SUM(Fields!PYRevenue.Value)) / SUM(Fields!PYRevenue.Value)))
,iif(SUM(Fields!PYRevenue.Value) = 0, 0,(SUM(Fields!Revenue.Value) / SUM(Fields!PYRevenue.Value))))
Sorry, I misread your formula until I indented it properly. Even though you have a check for PYRevenue = 0, you need to also include another IIF() after both occurrences of "/", like so:
iif(
SUM(Fields!PYRevenue.Value) = 0,
0,
(
(SUM(Fields!Revenue.Value) - SUM(Fields!PYRevenue.Value)
) / IIF(SUM(Fields!PYRevenue.Value) = 0, 1, SUM(Fields!PYRevenue.Value))
)
SSRS evaluates all the expressions so you have to handle the 0 value even if your parent logic prevents that block from being hit.
Apologies for the hurried answer... Doing this on my phone..
Even though your last expression will not be used if SUM(PYRevenue) is zero, it will still get evaluated and give a divide by zero error.
2 ways to resolve.
I personally use SWITCH instead of IIF for anything that is not simple.
Alternatively you need to check SUM(PYRevenue) on both side of your division. Pseudo code would be =IIF(SUM(PYRevenue)=0,0,SUM(Revenue)) / IIF(SUM(PYRevenue)=0, 1, SUM(PYRevenue)) So if sum PY is zero then do 0/1 else do revenue/pyrevenue

Additional nested IIF

I'm really hoping someone can help with this, I'm not really hot on the finer points of SSRS and I'm struggling to find any articles that answer my query, or at least point me in the right direction.
I am trying to write an report that looks at the grades a student gets for each unit they take, change this from a "D" or "M' etc... to a value; 1 or 2 etc... and then gives me the average based upon the number of units they completed.
I've managed to get this far, but I now need to output a field that shows if the average value is between 1.0 and 1.5 display "Pass", between 1.6 and 2.5 display "Merit" and between 2.6 and 3.0 display "Distinction".
This is where I've got thus far:
=SUM(IIF(Fields!ActualGrade.Value="D",3,IIF(Fields!ActualGrade.Value="M",2,IIF(Fields!ActualGrade.Value="P",1,0))))/Fields!CompletedUnits.Value
I think I may need another IIF in front of the SUM, but I'm really not too sure.
Any help would be massively appreciated
Thanks
S
Try using this expression:
=Switch(
(SUM(IIF(Fields!ActualGrade.Value="D",3,IIF(Fields!ActualGrade.Value="M",2,IIF(Fields!ActualGrade.Value="P",1,0)))) /
Fields!CompletedUnits.Value) > 2.6, "Distinction",
(SUM(IIF(Fields!ActualGrade.Value="D",3,IIF(Fields!ActualGrade.Value="M",2,IIF(Fields!ActualGrade.Value="P",1,0)))) /
Fields!CompletedUnits.Value) > 1.5, "Merit",
(SUM(IIF(Fields!ActualGrade.Value="D",3,IIF(Fields!ActualGrade.Value="M",2,IIF(Fields!ActualGrade.Value="P",1,0)))) /
Fields!CompletedUnits.Value) > 0.9, "Pass",
true,"Nothing"
)
Also if you want to show the average in a column and the determined string (Metir, Pass, and Distinction) you can reference the text value by doing someting like this:
=Switch(
ReportItems!TextBox14.Value > 2.6, "Distinction",
ReportItems!TextBox14.Value > 1.5, "Merit",
ReportItems!TextBox14.Value > 0.9, "Pass",
true,"Nothing"
)
Where TextBox14 is the textbox where you put the Sum(IIF(...))/YouField expression.
Hope this helps.

Is it impossible to increment in a for loop by decimal numbers in actionScript 3 and get accurate results?

I can't seem to find an answer for this anywhere online, but this for loop, although seemingly valid, creates an infinite loop because actionscript does not seem to recognize 0.1, 0.2, 0.3, and so on. This for loop will just trace 0, 0, 0, and so on infinitely.
Can anyone tell me why actionscript does this, and is there a way to get to precise decimal number increments in actionscript?
for(var i:int = 0; i<1; i = i + 0.1)
{
trace(i);
}
The initial problem, as reported in Hobo Sapiens's comment, is that you have declared i as int, but want it to store fractions. However, even after you have fixed that, you are likely to encounter rounding error issues. Adding up a floating point approximation to 0.1 is not a good way of getting the closest possible approximation to 0.9, or the exact value of 1.0.
There are generally two solutions:
Use a decimal data type. I have not been able to find a decimal arithmetic package for Actionscript, but that does not mean there isn't one.
Use an integer that represents a scaled version of the value you want. In the case of your loop, you would increment i by one on each iteration, and limit it to less than ten. You would then use a Number conversion of i, divided by ten, inside the loop. That results in using the closest floating point number to 0, 0.1, 0.2 etc. inside the loop.
Can't comment yet, so I'll post it as an answer:
There are definitely cases where increments by a float value are useful. And if it was absolutely wrong, why would it then be possible at all?
What akmozo suggested is a good way to do it, instead of doing the increment i = i + 0.1 you can do i = Number((i+0.1).toFixed(decimalSpacesYouWant)). This will return a number without the usual rounding problems. It gets especially useful with very small values (converging towards zero) which tend to flip into Infinity or NaN at some point.

Maple numeric dsolve. Maxtime of result

I have a system of differential equations. I solve it with numerical dsolve:
sol := dsolve({First, Fourth, Second, Third, iA(0) = 0, iB(0) = 0, theta(0) = 0, (D(theta))(0) = 0}, numeric, vars,maxfun=100000):
First, Second, Third, Fourth --- are differential equations, then there are initail conditions, vars --- is a set of variables (in respect of time) I want to get. maxfun - is maximum number of evaluations made by dsolve to calculate derivatives.
So, I need to retrieve maximum time value, where vars are calculated. I tried setting range parameter but it takes too long to solve (and I need to call dsolve for a hundred or even more times). With maxfun=1e5 I get result in three or four seconds.
Any tricks can be done here? Maybe there is a way to set timestep between evaluations of derivatives?
Thanks in advance and sorry for my broken English.

How can I always round up decimal values to the nearest integer value?

On a report I have the following code for a field:
=Sum([PartQty]*[ModuleQty])
Example results are 2.1 and 2.6. What I need is for these value to round up to the value of 3. How can I change my field code to always round up the results of my current expression?
This is an old Access trick I learned a very long time ago, and it makes use of the way Access handles fractional, negative numbers. Try this:
-Int(-[DecimalValue])
It's odd, but it will always round your numbers up to the nearest whole number.
you could do
=Int(Sum([PartQty]*[ModuleQty]))+1
I think. That would get the Int part of the sum (2) and then add 1. you might need to be a little more clever as this will probably give you 3 even if the sum is exactly 2, which is probably not what you want.
not tested it but something along these lines might work (access syntax is not that great, but should give you the right idea) :
Iif(Sum([PartQty]*[ModuleQty])-Int(Sum([PartQty]*[ModuleQty]))=0,
Sum([PartQty]*[ModuleQty]),
Int(Sum([PartQty]*[ModuleQty]))+1)
Test this:
Round(yournumber + 0.5, 0)