SSRS 2008 RunningValue Multiplication - reporting-services

Is it possible to calculate rolling throughput yields in SSRS with RunningValue function? If not, is there another way to?
Currently I am using the expression for the orange line series:
=(RunningValue(((Sum(Fields!StartQty.Value) - Sum(Fields!LossQty.Value)) / Sum(Fields!StartQty.Value)), SUM, "DataSet1"))
And getting this expected outcome because of the SUM aggregate in RunningValue:
I need to get it to display 0.88, 0.62, 0.62 all the way to the end of every step unless it ran into another low first pass yield in a step.
So the calculation should be, 0.88 * 0.71 = 0.62
If it hit another bad yield on a step, 0.62 * 0.92 = 0.57 to the end.
The closest thing I could find was another question similar here on Stack Overflow but could not figure out a way to implement it in my case: Function equivalent to SUM() for multiplication in SQL Reporting
Edit 1
I forgot you cannot have nested aggregates in SSRS 2008, so I would be interested to see if there is a solution for both SSRS 2008 and 2012+
Edit 2
As requested in a comment, a sample data set:
The simple expression to calculate the blue bars is the following:
=((Sum(Fields!QtyStart.Value) - Sum(Fields!ScrappedQty.Value)) / Sum(Fields!QtyStart.Value))

I've recreated your scenario with some simpler data. For this requeriment you can use custom code to get each value by step.
Go to Report menu / Report properties... / Code tab. In the box use this VB code:
Dim prev As Double = - 1.0
Public Function GetYield(ByVal current As Double) As Double
if prev = -1.0 then
prev=current
return prev
End if
if (current*prev) <= prev then
prev = current*prev
End if
return prev
End Function
Now add an additional expression to the Chart Values.
=Code.GetYield(
(Sum(Fields!QtyStart.Value) - Sum(Fields!ScrappedQty.Value)) / Sum(Fields!QtyStart.Value)
)
In order to this works you can only call the GetYield function once per report, since it changes the variable prev per call and its value is shared across the report at runtime.
Also note prev variable is initialized with -1 to determine the first time the function is called so if eventually the expression we are passing to the function produces -1, it will return unexpected values. Set the initial value to something you know won't be produced.
It should produce:
Let me know if this helps.

Related

How to handle #ERROR, NAN and INFINITY in a single expression in SSRS Report

So, if current and previous both are blank then I need to show result also blank. My Formula looks like this (Current-Previous)/Previous*100
I tried like this
=IIF((SUM(Current)= "" OR SUM(Previous) = ""),"",(SUM(Current) - SUM(Previous))) / SUM(Previous)
but it was giving #ERROR, NAN and INFINITY I want solution for all these issues in a single expression. Please help me on this
This is a well documented issue with SSRS. It evaluates both sides of the iif so it always ends up with divide by zero errors if it encounters one.
One way to handle it is through vb code in your report.
Go to report properties -> Code
Paste the following code :
Public Shared Function divbyzero(ByVal Standard As Decimal, ByVal Actual As Decimal) As Decimal
If Actual = 0 Then
Return 0
End If
If Standard = 0 Then
Return 0
End If
Return (Standard / Actual )
End Function
The above function will return a zero if either a numerator or the denominator is a zero.
Now on your report, go to the text box where you are performing your equation and do the following instead.
=code.divbyzero(SUM(fields!Current.value) - SUM(fields!Previous.value) ,SUM(fields!Previous.value))

How to remove an NaN and Inf in a formula

I am writing a report with a column that calculates year over year comps in sales. The problem I am having is that we do not have any sales history that dates before the beginning of this year. I am using the formula below to calcute the percent change but am getting NaN and Inf errors due to the fact that there is zero in the denominator. Is there a way to write this formula to exclude those, perhaps an iif statement?
=(SUM(Fields!MTDInvoiced.Value)-SUM(Fields!PYMTDInvoiced.Value))/SUM(Fields!PYMTDInvoiced.Value)
You could write a custom function to do this, that's probably the best way especially if you will need this often, but the expression only version below should work.
=IIF(
SUM(Fields!PYMTDInvoiced.Value)=0,
0,
(SUM(Fields!MTDInvoiced.Value) - SUM(Fields!PYMTDInvoiced.Value))
/
IIF(SUM(Fields!PYMTDInvoiced.Value)=0,1,SUM(Fields!PYMTDInvoiced.Value))
)
I've not tested this but hopefully it's correct.
The first thing we do is test is if the denominator is zero, then return 0 if it is. If it's not then we do the standard expression but we again test that the denominator is not zero. Even though this last check will never return the final output, we still have to test for it as each part of the expression is evaluated in all cases.
Another way to do this is via VB code.
Go to report properties -> code
paste the following
Public Function fdivide (Byval a As Decimal,Byval b As Decimal, Byval c As Decimal) As Decimal
' Fix for divide by zero problem in VB
' calculates a/b and either returns result or c if b = 0
if b = 0 then
return c
else
return a/b
end if
end function
now on the text box you want to divide, simply call this function like so :
=Code.fdivide(Fields!numerator.value, Fields!denominator.value,0)
The code is self explanatory. Can be reused without having to worry about nested IFF statements.

How to get group total inside a row in a rdl report?

How do I get the total of the group inside a row item?
I have tried Fields!Sales.Value/SUM(Fields!Sales.Value) but it returns 1 instead of the row_amount/group_total the report returns row_amount/row_amount
PS: Data comes from a SSAS cube. I made picture bellow1 to summarize what I wanna do
You could potentially just compare the values by the ReportItems. That is, you can specify a name for the sales textbox and for the totalsales textbox and do the comparison using those values. The following expression would likely work as intended.
= (ReportItems!SalesTextbox.Value / ReportItems!SalesTotalTextbox.Value) * 100.0
The other thing to consider with this is that if your result is 1, you may be dealing with integer division from your data. Make sure the data being returned is coming in with a double or decimal datatype, not an integer. It may work as you have it currently if you get the datatype corrected.
You also need to handle "divide by Zero' errors. Using an "=IIf (denominator = 0, 0, numerator/denominator) doesn't work since an "IIf" function evaluates both the "then" and the "else" and will still return an error. We use a bit of custom code that I found on SO (I wish I could remember where it came from so I could give credit where it's due).
Public Function HandleDivideByZero(ByVal numerator As Decimal, denominator As Decimal) As Decimal
If denominator = 0 Then
Return 0
Else
Return numerator / denominator
End If
End Function

SSRS divide by zero error expression

Hope your well.
I am working on a report and seem to get a #error. Seems like it is a divide by zero error but I can not work out a solution. The expression:
=( Sum(Fields!Line_Sell.Value) - Sum(Fields!Line_Cost.Value) ) / Sum(Fields!Line_Sell.Value)
I am relatively new to RS but have tried
ISNULL( )
but with no success.
Any help, I would be greatful.
Thanks
Let's say you have an expression set to x / y, where y has the potential to be zero. As you experienced, SSRS will change this value to #ERR (or sometimes NaN).
Your first instinct would be to test for zero
=Iif(y = 0, 0, x/y)
Unfortunately, this can result in a debugger warning that says you are attempting to divide by zero. This is because SSRS attempts to evaluate the entire statement during compilation.
As a result, we need to provide SSRS with a value that it can evaluate
=x * y ^ -1
This is the same as dividing by your y value, but SSRS sees this as dividing by infinity, and therefore can make an evaluation.
As a final step, you will want to make sure your code can properly export to Excel. Under 2012 SSRS (and possibly later, I haven't tested), SSRS has an issue where possible zero values export to Excel as 0.000000000000000 and cause loss of data errors.
To correct this, we explicitly output the value of zero
=Iif(x = 0 OR y = 0, 0, x * y ^ -1)
The expression in your comment doesn't look complete, so I can't tell what you tried next that didn't work. You definitely want to test for zero before performing division.
You can try this:
=iif( Sum(Fields!Line_Sell.Value) = 0, 0, (Sum(Fields!Line_Sell.Value) - Sum(Fields!Line_Cost.Value)) / Sum(Fields!Line_Sell.Value))
Or to check for an empty value also, you can try this:
=iif( (Sum(Fields!Line_Sell.Value) = 0 or IsNothing(Sum(Fields!Line_Sell.Value)), 0,Sum(Fields!Line_Sell.Value) - Sum(Fields!Line_Cost.Value)) / Sum(Fields!Line_Sell.Value))
If that's still not yielding results, break down the expression and run the report. Try Sum(Fields!Line_Sell.Value) by itself, run the report and see what you get. To be more efficient create a table with one column for Fields!Line_Sell.Value and another for Fields!Line_Cost.Value. In the table, include detail rows and add a total row so that you get the Sum function with each of those fields individually. You need to look at the detail records to try to extrapolate why the aggregate function isn't working. Bottom line - decompose your expression and test it piece by piece. The answer is in there somewhere.

SSRS 2005 Matrix Reports - Editing Total

This seems as though it should be simple, but appears not to be.
In SSRS 2005 I've written a matrix report and added some fields, one of which has the formula: If (x / y >= n, 1, 0). I've called this field 'Accuracy'. The report aggregates this field across a number of individuals and then for a number of days.
Ideally I want a subtotal that gives a sum of the 'Accuracy' figures (so we can say we had n people who were accurate today). However, the subtotal calculates the formula for the totals of x and y. Subtotals is only ever going to be 1 or 0.
Any ideas as to how I can get a Count of Accuracy displayed on the matrix report? I've tried creating various fields along the lines of Sum(accuracy) and Count(accuracy) - these return an error when the report is run.
Thanks!
maybe report behaves 0 and 1 as a boolean value. you can make a dll that do this work for you and reference to your report and use the methods that exist for this work in dll.
Try using the running value. You could put the expression inside it or inside the report code block and just call it.
=RunningValue(what to count, sum, scope)
=RunningValue(Fields!Cost.Value, Sum, Nothing)
More info here:
http://msdn.microsoft.com/en-us/library/ms159136.aspx
Hope it helps.