SSRS calculation expression for specific value - reporting-services

I'm looking at doing a calculation expression in SSRS but I've become unstuck.
I'm trying to calculate when a field equals a specific value, then return a percentage calculation.
This is what I've tried so far:
=SUM(IIF(Fields!Alert.Value="Red",(FormatPercent(Count(Fields!Sales.Value) / 6532 ,0)))SUM(IIF(Fields!Alert.Value="Yellow",(FormatPercent(Count(Fields!Sales.Value) / 2541 ,0)))SUM(IIF(Fields!Alert.Value="Green",(FormatPercent(Count(Fields!Sales.Value) / 1025,0)))
Obviously this is incorrect and doesn't work. The expression needs to include all 3 colours.

UPDATED see if this works.
UPDATED per requested. I put the statement in the last False part, but I'm not sure if this is what you really want.
UPDATED again. Added False part to the last IIF statement.
UPDATED: Removed the SUM function. Try this to see if it works. Your IIF statement didn't have any False parts. Also, just SUM once around the whole statement if you want it summed. Not knowing your data, I'm not sure if you want the % summed up.
=IIF(Fields!location.Value="East" AND Fields!Alert.Value="Red",(FormatPercent(Count(Fields!Sales.Value) / 6532 ,0)),IIF(Fields!Alert.Value="Yellow",FormatPercent(Count(Fields!Sales.Value) / 2541 ,0),IIF(Fields!Alert.Value="Green",FormatPercent(Count(Fields!Sales.Value) / 1025,0),0)))

Related

SSRS How to calculate a ratio from two subtotal in a table

I'm using SSRS reporting tool and I need to calculate a ratio (out/in) for two subtotal in a table. Is there a way to do this? All data are coming from the same DataSet. In the example I need to divide 8649 by 2638 (absolute value). For a ratio of 3.28
enter image description here
It's hard to give an exact answer without seeing your dataset but you can probably do something like this.
=
SUM(IIF(Fields!Processus.Value = "Production", ABS(Fields!Qte.Value), Nothing), "DataSet1")
/
SUM(IIF(Fields!Processus.Value = "Intrant", ABS(Fields!Qte.Value), Nothing), "DataSet1")
What this does is to 2 sums, the first sums anything where the Processus field is "Production" and then divides this by a second sum.
The second sum is similar to the first except it sums anything where the Processus field is Intrant. The difference is that we use ABS() to give us a positive number.
Note: These SUM() functions are working across the entire dataset, as defined by the "DataSet1" scope argument. Change this to match your dataset name exactly (case sensitive) if required.
I created a cut down version of your sample data to test and these are the results.

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)

How to Sum the aggregates of a child group

This should be easy, but I am stuck.
I have a table listing some figures about Qualifications - to achieve which a dataset that is essentially a row per Student is being grouped on Qualification with a Parent Grouping on "Measure" (which is just a bucket of qualifications).
One of the columns is trying to work out the number of students (well, more properly the number of students with a value in a particular field, weighted by another field) in each Measure/Qualification. In the screenshot below, it's the "Pred. Avg" column on the right hand side.
So for the Qualification Row Grouping, that column is calculated by:
=CountDistinct(Iif(IsNothing(Fields!AVG_PTS.Value) = False, Fields!Learner_ID.Value, Nothing), "Qual") * Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP")
This works fine - the values of 35 and 11.5 in that rightmost column are correct for those rows. What the top row should be doing is simply adding up the values in the other rows to give me the number of students in this Measure, in this case to give 46.5. To do that the expression I am using is:
=Sum(CountDistinct(Iif(IsNothing(Fields!AVG_PTS.Value) = False, Fields!Learner_ID.Value, Nothing), "Qual") * Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP"), "Measure")
However as you can see in the screenshot, this returns 2917 instead.
So my question is; Why doesn't that work, and given that it doesn't work how can I, within a parent group, aggregate the results of aggregates inside a child group?
EDIT:
OK so, I have determined that the following works correctly:
=Sum(CountDistinct(Iif(IsNothing(Fields!AVG_PTS.Value) = False, Fields!Learner_ID.Value, Nothing), "Qual"), "Measure")
The problem there is that the Qual row that returns 11.5 is weighted to 0.5. I.E. it actually returns 23, and the Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP") is for that row returning 0.5 and altering it to 11.5...so the question becomes; "how do I force that ...*Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP") into the "Qual" scope, like the CountDistinct() is already in?
The issue here is that you're trying to aggregate values using that Lookup function which only returns one value. There are a couple ways you could go about doing this. One option would be to use the LookupSet function to get the applicable weightings. An even better option is to combine the data in your dataset so that the weighting is available without using a lookup. That way the function can recalculate an any grouping level without you having to force a scope on it. Also, CountDistinct ignores "Nothing" so you can do without the extra IIf statement. Hope that helps.

Initial value for RunningValue() SSRS

I am designing a report that need to get the cumulative value. I am using the RunningValue() function for it. The issue is the value always starts from 0. Is it possible to get an initial value and then do the RunningValue() from it?
Thanks
[EDITED]
This is what I need. The initial value comes from the total of accountIDs in a given date and the runningvalue() would start to add more accountIDs from it
Have you tried...
=RunningValue(*condition*) + starting_value
For example setting the value of the third column in the image below to
=RunningValue(CInt(Fields!Val.Value), Sum, "DataSet1") + 10
Gives

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.