SSRS expression count - reporting-services

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)

Related

Expression to calculate % in a report

I am building a report(report server project) using the Data Tools. I have a column Quantity it has a Total.
I need another column that calculate the share(%) of each line in the Quantity comparing to the Total.
The expression would be: Line_1_Share = Quantity_of_line_1/Total.
I tried =[Sum(Total/Quantity)] but it does not even accept as a valid expression.
If you right-click the textboxes that contain your working 'Quantity' and 'Total' values and look at the expressions you will see the correct format.
For exmaple your 'Quantity' expression might be something like
=Fields!Quantity.Value
or if it is in a grouped row it might be
=SUM(Fields!Quantity.Value)
your 'Total' expression might also be
=SUM(Fields!Quantity.Value)
When you use SUM() (or any similar aggregate) then the scope of the expression decides what is included in the sum. The scope can be a single row, a row group or an entire dataset. If you do not specify a scope then the position of the textbox determines the scope.
So, if you have a simple table with no grouping other than the total line and your dataset name is dataset1 then your expression would need to be
=Fields!Quantity.Value / SUM(Fields!Quantity.Value, "dataset1")
The above reads .... "For the current row, take the Quantity and divide is but the sum of all Quantities that are within the entire dataset called dataset1"
If this does not help, post your current report design including and row and/or column groups.

How to properly set a SUM AGGREGATE on a group-level column

I have a SSRS report, and I'm trying to sum one of the columns.
The first column is a countdistinct(field1) and works perfectly.
The second column is in the same group as the first. When the first expands, the second column is part of the first group.
I'm trying to get the value here 24 in the group total.
if I perform a [SUM(CDEC(FIELD2.value))] it results in 72 because technically the 12 is repeated through all six records.
[SUM(MAX(FIELD2.value))] results in a 12, because the MAX() function gives max value of ALL the records.
How do I get 24, here?
Thanks
UPDATE #1.
here's the field setup for those columns
Someone mentioned using a scope in my sum(). How do I determine what my scope is? Thanks!
You have to add the scope in the sum expression in your tablix otherwise it always takes the dataset data. Let´s say you have a dataset (Dataset1), with the fields SalesOrder, SalesPrice. If you group now in your tablix by SalesOrder (GroupingBySalesOrder) you can add different scopes for the aggregate functions:
=Sum(Fields!SalesPrice.Value) 'This takes the Default scope "Dataset1"
=Sum(Fields!SalesPrice.Value, "Dataset1") 'The same result like above
=Sum(Fields!SalesPrice.Value, "GroupingBySalesOrder") 'The scope is now your grouping. Different result like the other two

Reporting Parameters in Expression

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")

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.

Show all records/some records based on parameter value

I have stored procedure which returns around 100 rows. One column is Category.
In SSRS, I've created DataSet related to that stored procedure. On Report body I have Tablix, which I relate to DataSet.
Now, I have additional parameter, called FilterColumn, which consist all different categories from dataset, plus one row called "All products".
How to filter tablix based on that parameter to show me whether products from specific categories or all products?
You need to set up a filter similar to the following:
Where the expression is:
=IIf(Parameters!FilterColumn.Value = Fields!Category.Value
or Parameters!FilterColumn.Value = "All products"
, "Include"
, "Exclude")
This matches the row Category based on the parameter value, or, if the value = All products, will include all rows.
As mentioned in the comments and the other answer, this is possible in the SP too, but since it seems to be specifically to demonstrate the functionality this should do the trick at the report level.
I have created some solution and worked for me:
In Expression field, I put first expression:
1. Iif(Parameters!FilterColumn.Value = "All", 1, Fields!Category.Value)
In Value field, I put second expression:
2. Iif(Parameters!FilterColumn.Value = "All", 1, Parameters!FilterColumn.Value)
So, when I choose "All" value in parameter, then first expression will result 1, and second expression will result 1, and i have 1 = 1 which is true for all rows, and I got all rows in table.
When I choose specific category from parameter, then in first expression result will be Fields!Category.Value and in second expression will be Parameters!FilterColumn.Value. Simple, I got Fields!Category.Value = Parameters!FilterColumn.Value, or just give me rows where category is that choosen in parameter.
Pass the Additional Parameter to your store procedure, so you send data that is already sorted to your report. This will avoid multiple Tablix created depending on your parameter selection.