Reporting Parameters in Expression - reporting-services

I am trying to use parameters within my expression because I won't be able to use the parameters in the dataset, I am trying to do a simple expression but I am struggling to figure out what I am doing wrong, here is what I want to achieve:
I want to count the rows when the Month(Date) = #Month.
What I have so far:
=IIf(Month(Fields!Date_Logged_SQL.Value) = Parameters!rpMonth.Value,CountRows(),Nothing)
My results return no values which I am assuming must be something to do with my false return.

You need to use the IIf expression as part of a larger aggregation expression, something like:
=Sum(IIf(Month(Fields!Date_Logged_SQL.Value) = Parameters!rpMonth.Value,1,0))
For each row in Scope, this will either add 1 or 0 to the running total of rows that fulfill your check, the end result being the total of rows that match the month.
Depending on where you're adding the expression, you may need to add a Scope parameter to get the correct result, e.g. something like:
=Sum(IIf(Month(Fields!Date_Logged_SQL.Value) = Parameters!rpMonth.Value,1,0), "DataSet1")

Related

SSRS Multi-Value Parameter - When Select ALL, returns zero rows

My current definitions for this report are like this:
DataSet A = a pretty extensive query with 'AND ai.Channel IN (#ChannelParameter)'
DataSet B = Channel 'Select distinct channel from Account_Info'
result set 'Retail Channel' or 'Wholesale'
Parameter = #ChannelParameter is set to Allow Multiple Values and is Getting the values from Query.
Tablix Properties - Filters
Expression [Channel]
Operator IN
Value =Parameters!ChannelParameter.Value(0)
When I run the report and select 'Retail Channel', I get the correct data.
When I run the report and select 'Wholesale', I get the correct data.
When I run the report and select both values, I get zero rows returned.
When I modify the query for DataSet A to be ai.Channel IN ('Retail Channel','Wholesale'), I get all of the rows. There are no rows in the data where the Channel field is NULL.
I've seen and tried some changes to the expression in the parameter using a JOIN statement, but no better results there.
What am I missing?
You filter expression is incorrect, as you stated (0) that means only the first parameter value will be used.
Having said that, it looks like you are already filtering the data in your A dataset so there is no need for the tablix filter, just remove it.

SSRS expression count

I can use expression =count(Fields!xxx.value, "DataSet1") and return the total count.
But I have a field with Active and Not active and want to count for record with active. I use expression =Count(IIf(Fields!xxx.Value = "Active", 1, Nothing)) and it will not work.
It said "the value expression for textbox uses an aggregate expression without scope. a scope is requiered for all aggregates use outside od a data region unless the export contains exactly one datadset"
You have two problems.
1: you need to include the "DataSet1" scope in your field definition, like you did in the count which worked.
2: instead of doing a count on your IIF statement, do a SUM of IIF([Condition Is True],1,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.

IIF Statement Not Returning a Value in SSRS

I'm very confused by this situation, i've written quite a few IIF statements and always get them to work. I have two columns in my dataset called CATEGORY and PCT, what i'm trying to do is return the PCT value for only one specific value in CATEGORY.
For example, I have the following table of data
Category PCT
A .50
B .75
I have placed a textbox on my report and have written the following expression to return the PCT value if Category = B, like so:
=IIF(Fields!Category.Value = "B", Fields!PCT.Value, " ")
For some reason this expression returns empty every single time. When I just put Fields!Category.Value in the textbox as a test then the value A returns which is as expected.
I'm really thrown-off by this and I know i'm missing something very simple - can someone help?
Its important that we understand the context of the textbox as the expression seems valid.
If you placed a single textbox on your report and used your above expression (with references to the datasets) ONLY the first row of your dataset will be evaluated. In which case the expression will always evaluate to the same result.
Can you confirm that the control you are using is indeed a textbox? If it is then i believe you do need a reference to the dataset and the expression will look more like this:
=iif(First(Fields!Category.Value, "datasetName") = "B", First(Fields!PCT.Value, "datasetName"), " ")
This would only evaluate the first row in your dataset.
If you were to do this in a tablix using your original expression then it should work.

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