SSRS Expression with parameter in WHERE clause - reporting-services

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

Related

SSRS Expression - Subtracting the Sums, when one group is a parameter

I am trying to Subtract two scenarios from each other in a SSRS report. One scenario is a constant, 'Actuals' the other is a dynamic forecast scenario and set by a parameter. Here is the code I am trying:
=sum(iif(Fields!Scenario_Name.Value=Parameters!ScenarioName.Value,CDbl(Fields!CAD.Value),0))-
sum(iif(Fields!Scenario_Name.Value="Activity Actuals",CDbl(Fields!CAD.Value),0))
I have also tried:
=sum(iif(Fields!Scenario_Name.Value=Parameters!ScenarioName.Value,Fields!CAD.Value,0))-
sum(iif(Fields!Scenario_Name.Value="Activity Actuals",Fields!CAD.Value,0))
The report runs but I get an error in the line where this expression is entered.
The CDEC was important to both the CAD.value and the 0. The Parameter value needs a (0) as in the final expression that works.
enter code here=Sum(IIf(Fields!Scenario_Name.Value=Parameters!ScenarioName.Value,CDEC(Fields!CAD.Value),CDEC(0))) -
Sum(iif(Fields!Scenario_Name.Value="Activity Actuals",CDEC(Fields!CAD.Value),CDEC(0)))

DateDiff function in SSRS (report server) gives error?

Im trying to find the days gap between two dates using DateDiff function.
I have 2 datasets defined. If companycode is 'AB' then from one dataset else from another dataset I retrieve data.
Here is my expression. When I change to preview mode, it shows redmark to the first First(Fields!PeriodFrom.Value line. Why? (after generating report that field shows #Error
What Im doing wrong here?
=IIF(Parameters!CompanyCode.Value="AB",
DateDiff("d",First(Fields!PeriodFrom.Value, "ABReportData"), First(Fields!PeriodTo.Value, "ABReportData")),
DateDiff("d",First(Fields!PeriodFrom.Value, "XYReportData"), First(Fields!PeriodTo.Value, "XYReportData")))
I think there are two possible scenarios. First one is the expression
=First(Fields!PeriodFrom.Value, "ABReportData")
doesnt return a value. Add a column with this expression and check if you get a value.
If the value is correct, make sure that the DateDiff() function gets a date:
=IIF(Parameters!CompanyCode.Value="AB",
DateDiff("d",
CDate(First(Fields!PeriodFrom.Value, "ABReportData")),
CDate(First(Fields!PeriodTo.Value, "ABReportData"))
),
DateDiff("d",
CDate(First(Fields!PeriodFrom.Value, "XYReportData")),
CDate(First(Fields!PeriodTo.Value, "XYReportData"))
)
)

Conditional Aggregate Lookup - SSRS

I have a report with two datasets to summarise the number and value of incomplete orders by status. I have a "Back Order" column, which is using the 'Lookup' function to refer to a second database, based on a whether the Fields!IsBackorder.Value returns true. This works at line level, but I've run into issues at the aggregate level.
For the total count of orders, this forumula works:
=SUM(IIF(LOOKUP(Fields!SalesOrderID.Value, Fields!SalesOrderID.Value, Fields!IsBackorder.Value, "DstBackorders") = "TRUE",1,0))
However, for the total value of orders ("Fields!NetValue.Value"), this returns '#Error'
=SUM(IIF(LOOKUP(Fields!SalesOrderID.Value, Fields!SalesOrderID.Value, Fields!IsBackorder.Value, "DstBackorders") = "TRUE",Fields!NetValue.Value,0))
I've tried custom aggregate functions but I haven't found any that work. I'm not sure how I'm getting this error.
Any suggestions would be really helpful.
Thanks,
Report Screenshot
The syntax looks perfectly fine , also the lookup looks good , can you please check on the below things in your DataSet:
Is Fields!NetValue.Value in scope of the current DataSet.
Are we using the correct data type for Fields!NetValue.Value(Something which is aggregatable , like int , decimal etc.)

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: 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.