Average of all values Except 0 - reporting-services

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.

Related

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)

SSRS Expression with parameter in WHERE clause

In SSRS, I'm trying to calculate the average number of months a client used a program. The programID is the parameter for the whole report. I'm trying to achieve this (not written with real syntax):
=Avg(Fields!length_of_stay.Value, 0))/30.0 WHERE programid = #ProgramID
Using this question, I came up the the following code which is producing an incorrect answer. I tested in SSMS to get the actual values to compare to SSRS results.
=Avg(IIF(Fields!programid.Value = Parameters!ProgramID.Value, Fields!Length_of_Stay.Value, 0))/30.0
The "/30" is used since the value is in days and I need months. I think the issue is using the parameter value chosen; this is my first report trying to calculate expressions with parameters.
Avg returns the average of all non-null numeric values. 0 is not null so it gets included in the average, distorting your result for every row with a different PragramId. Try using Nothing instead:
=Avg(IIF(Fields!programid.Value = Parameters!ProgramID.Value, Fields!Length_of_Stay.Value, Nothing))/30.0

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.

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: Get values from a particular row of DataSet?

My dataset currently has 12 rows of data. Each representing data for a month. I would like to have variance of a column between to rows, the rows being last & last but one i.e., latest month and previous month's data.
It could have been simple if I were to work on tablix but thats not the case. I want those values for a textbox.
Any ideas on it anyone?
I hope you are using SSRS 2008R2:
R2 introduced the Lookup function that is perfect for this scenario.
=Lookup( Fields!ProductUID.Value ,Fields!ProductID.Value,Fields!Price.Value,"PriceDataSet")
The Lookup function above will evaluate the first parameter ("Fields!ProductUID.Value") in the current dataset, then look for a matching value in the field specified in the second parameter ("Fields!ProductID.Value") in the dataset specified in the fourth parameter. The value of the third parameter is then evaluated in that row of the dataset and returned.
A little convoluted, but very helpful.
In your case, you can use this in a textbox with a calculated a static number:
=Lookup(
Month(DateAdd(DateInterval.Month, -1, GetDate())),
Fields!MonthID.Value,
Fields!Name.Value,
"DataSet1")
This should calculate a number for last month, then look for a match in DataSet1.
In this example I have a tablix with Statecode and name as below
enter image description here
Suppose you want to display the name of state of CA, write an expression as -
=Lookup(
"CA" ,
Fields!StateCode.Value,
Fields!StateName.Value,
"ReportData"
)
This will return 'California' in the text box
I ran across this post while trying to solve a similar problem but with columns of double data type. Not sure why but SSRS did not want to return my first row using LOOKUP in combination with ROW_NUMBER in SQL(If someone can solve that all the better). I ended up using a SUM(IIF) instead. Hopefully, this is useful for someone else.
=Sum(IIF(Fields!RowNum.Value=1,CDBL(Fields!MyNumericColumn.Value),CDBL(0)))
Note: If SSRS complains about data types, just cast both parts of the IIF to the desired data type.