SSRS Expression how to compare value from two different datasets? - reporting-services

So I've searched for this specifically but cannot find an example or answer anywhere.
I'm trying to compare a value of one dataset to the value of another dataset but it's not working.
Here's my expression:
=IIF((Fields!IC_ADV.Value + Fields!OC_ADV.Value) > 0, Max(Fields!AMOUNT.Value, "Data_bucket_info") > 15, nothing)
All I get is "True". If I remove the '> 15' then I get a value but it's not the right value. I've tried replacing Max with First, Min, Sum; without the '>15' I again get a value but not the right one and with the '>15' I get "True" or "False".
If I have Max in, I'm expecting to get the Max value for AMOUNT for all amount > 15; if I have Min in, I'm expecting to get the Min value for AMOUNT for all amount > 15.
So for example:
AMOUNT is in increments of 5; so 5, 10, 15, 20, 25....etc.
If (Fields!IC_ADV.Value + Fields!OC_ADV.Value) = 24 then what I want is for AMOUNT to display 25. If (Fields!IC_ADV.Value + Fields!OC_ADV.Value) = 3 then I want AMOUNT to display 5.
The warning I keep getting in SSRS is 'The Value expression for the textrun 'Textbox12.Paragraphs[0].TextRuns[0]' uses a numeric aggregate function on data that is not numeric. Numeric aggregate functions (Sum, Avg, StDev, Var, StDevP, and VarP) can only aggregate numeric data.'
But the AMOUNT field is a numeric value.

It is not lot clear from your description what you are trying to achieve. But for what I understand you want to match the sum of IC_ADV and OC_ADV with some value and depending on that you want to get the value. for that you can set the expression as below,
=IIF((Fields!IC_ADV.Value + Fields!OC_ADV.Value) = 24,Max(Fields!AMOUNT.Value, "Data_bucket_info"),IIF((Fields!IC_ADV.Value + Fields!OC_ADV.Value) = 3,Min(Fields!AMOUNT.Value, "Data_bucket_info"),Nothing)
But if you want to match match value of one dataset to another datasets value based on some common field then you should take a look at the LookUp. Here is more info.

In this scenario, I think it's better to make it in Math way. Try the expression below:
=((Fields!IC_ADV.Value + Fields!OC_ADV.Value)\5+1)*5

Related

Calculated field in SSRS

I have tried to create a calculated field that will show the number of customer transactions processed within 15 minutes.
I have added the expression:
=count(fields!wait.Value<15)
However, when I run the query I'm getting the error message : 'expression used for the calculated field includes an aggregate RowNumber...'
Can you advise please on how to create a calculated field so I can capture the value I want?
I have tried = SUM(IIF(Fields!wait.Value < 15 , 1, 0)) to no avail.
With many thanks.
Calculated fields added to datasets can't have aggregate functions. The calculated field is essentially adding an extra column to your dataset. It sounds like you may want a variable? Used elsewhere in the report, your second expression would work, or the similar
=Count(IIf(Fields!wait.Value<15, 1, Nothing))
would work too.

SSRS - I want to add a filtered expression for a calculation

--
I would like to add and expression that calculates a value based on a certain value. I have a dataset with the name DSSPend that has to columns one is the Area and the other spend. Now I would like to calculate the spend based on certain area values.
I tried something like this but does not seem to work
=Iif((Fields!Area.Value, "DSSSpend") IN ('New York','Miami','Texas') = SUM(Fields!Spend.Value, "DSSSpend"), 0)
=sum(iif((Fields!Area.Value = "New York" or Fields!Area.Value = "Miami" or Fields!Area.Value = "Texas"),
CDec(Fields!Spend.Value), CDec(0)))
There is no IN operator in SSRS expressions. You can achieve the same goal by using OR.
Also, your SUM aggregation needs to go outside the IIF. Your expression would evaluate the first record only rather than going through each record in the dataset and then aggregating. The expression I have written above goes through the entire dataset, summing either the value in the Spend field or 0, depending on the value of Area.
Update: If you were getting an error before, it was probably because it wasn't seeing your amounts as numbers. By converting them explicitly, you should get around this issue.

Average of all values Except 0

I have a report that I have designed in SSRS 2008. I have a row which has couple of values. This is basically a Survey form where there are Values from 1 to 5 .Some of the values are either Blank or N/A(If they don't answer). But While I calculate Average of the Values it includes that particular value. I think it takes it as 0. So
Average of (4, 5,4,4,5,2,3, ,5) = 3.56 instead of 4.00.
Can You please tell me how I can Calculate Average of the the values without considering Blank values.
Thank you
You can add a calculated field to your dataset to filter out the unwanted values. Then you can just use a regular expression to make it more readable. Here's an example of the calculated field expression:
=iif(Fields!Score.Value > 0, Fields!Score.Value, Nothing)
Now you can simply reference =Avg(Fields!FilteredScore.Value) and it will work as desired.
I found the below solution for solving my problem:
=SUM(Fields!Score.Value)/Sum(iif(Fields!Score<>0,1,0))
Instead of using inbuilt average function, you need to create your own function to accomplish your task.
Mathematically Average = (sum of all answers)/(count of all answers)
For your case you need to modify the denomination to exclude all blank and N/A values.
New Avg func = (sum of all answers)/((count of all answers)-(count of all answers where it is blank or N/A))
SSRS equivalent:
=SUM(Fields!SurveyAnswer.Value)/
(Count(Fields!SurveyAnswer.Value)-
Count(IIF(Fields!SurveyAnswer="" OR UCASE(Fields!SurveyAnswer)="N/A", 1, **NOTHING**)))
OR
=SUM(Fields!SurveyAnswer.Value)/(Count(Fields!SurveyAnswer.Value)-
SUM(IIF(Fields!SurveyAnswer="" OR UCASE(Fields!SurveyAnswer)="N/A", 1, 0)))
HTH.

SSRS Avg function returns result that is different than expected

Currently I am trying to average a set of numbers, but with certain conditions.
Is it possible to use an iif() within an avg() and return the correct result?
Furthermore, as of now my computations return a decimal returned to a power (8.9267....E -05).
I tried circumventing the AVG function by conditionally summing and then dividing by a conditional count but it gives me the same results.
Can someone explain why this is returned and offer help?
Currently I have:
=avg(iif((This_case) AND (That_case) AND (This_conditional)
, Fields!ResponseRate.Value
, 0))
Essentially I want the average ResponseRate if certain conditions are met.
The sum function works fine for the conditions but the average doesn't.
You can definitely use IIf within Avg and get the correct results.
Do you want to exclude the False values from the calculation entirely?
In your example you're still including them, just setting them to 0 and hence still including them in the calculation. This might explain your unexpected results.
If you want to exclude them entirely use Nothing instead of 0.
Edit after comment
You can nest an expression in another IIf statement and check for NULL values using IsNothing.
Say your condition average expression is:
=Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing))
You can return 0 for NULL values with something like:
=IIf(IsNothing(Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing)))
, 0.0
, Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing)))
I'd like to add my two cents to this one, a little late, but also valuable. I was able to use the above code to Average out a data set if another record appears X number of times.
=AVG(IIF(Count(Fields!AcctNo.Value, "AcctNo1") = 2, Fields!Limit.Value, Nothing))
So, if the acctno field appears 1 time, avg the limit field for that row group. Ian's example was very helpful

SSRS Conditional Summing

I have an SSRS report that displays several pages of rows. In each row is a "TYPE" field. In that TYPE field there is either an "M" for the value or a "P" for the value. At the end of the report I want to summ up all the price values for the "P" TYPES. I tried this but it prioduced an #Error:
=Sum(iif(Fields!TYPE.Value = "P",Fields!EXT_QTY.Value * Fields!PRICE.Value ,0))
this summed all rows
=iif(Fields!PART_TYPE.Value = "P" , Sum(Fields!EXT_QTY.Value * Fields!PRICE.Value ), 0 )
I'm sure this is do-able. Any ideas? Thanks
Found the answer....
=SUM(IIF(Fields!PART_TYPE.Value ="P",CDbl(Fields!EXT_QTY.Value * Fields!PRICE.Value), CDbl(0.0)))
The SUM fails due to type comparison - you can't Sum values of different types, being the expression (probably a Double) with 0, an Integer. MikeTWebb's answer does explicit type conversion to get around this error. This is fine for this specific example, being a Sum, however this doesn't produce an accurate result if you want an average (being Sum / Count) of the values where the Type is P. That is because 0 is a value and would be included in the averaging calculation when you actually want those values excluded from the calculation.
Another option is to use Nothing instead of 0:
=Sum(IIF(Fields!TYPE.Value = "P", Fields!EXT_QTY.Value * Fields!PRICE.Value, Nothing))
This solves the type comparison error without needing explicit typecasting and is a better solution when you are using aggregations where whether the value exists or not is significant to the result, like Average.