Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSRS CountRows of a specific Field containing a specific Value

I am building an SSRS report. I have a DataSet that contains several Fields, one of which is Doc_Type. Doc_Type can contain several values, one of which is 'Shipment'.

In my report, I want to count the number of Doc_Types that are equal to 'Shipment'. This is what I am using, and it is not working:

=CountRows(Fields!Doc_Type.Value = "Shipments")

The error I get is: "The Value expression for the textrun 'Doc_Type.Paragraphs[0].TextRuns[0]' has a scope parameter that is not valid for an aggregate function. The scope parameter must be set to a string constant that is equal to either the name of a containing group, the name of a containing data region, or the name of a dataset.

like image 580
LanterneRouge Avatar asked Aug 27 '15 22:08

LanterneRouge


People also ask

How do I use IIF in SSRS expression?

Using IIF Function in SSRS We are going to add a new field to the report data set to determine if the Order Year is the Max or Current Year. As shown below, the dataset properties window is opened, and the Fields tab is selected. After clicking Add, at the bottom of the list a new field is added.

What is tablix in SSRS?

In SSRS RDL standard, “tablix” is a generalized term that means the combination of table, matrix (cross-table or pivot table), and list report items (table+list+matrix=tablix). It displays report data from a data set in cells that are organized into rows and columns.

How do I add a calculated field in SSRS?

To add a calculated field In the Report Data pane, right-click the dataset, and then click Add Calculated Field. In the Fields page of the Dataset Properties dialog box, click Add, and then click Calculated Field. A new row is added to the bottom of the grid.

How do you hide a column based on an SSRS expression?

To hide static columns in a table, matrix, or list. In Design view, select the table, matrix, or list to display the row and column handles. Right-click the column handle, and then click Column Visibility.


2 Answers

You need to use an IIF to evaluate your fields and SUM the results. If your expression is in a table, you could use this:

=SUM(IIF(Fields!Doc_Type.Value = "Shipments", 1, 0))
like image 96
Hannover Fist Avatar answered Nov 04 '22 11:11

Hannover Fist


There are many ways to achieve this.

Method 1

You can set up your expression something like this

=SUM(IIf(Fields!Doc_Type.Value = "Shipments", 1, 0), "YourDataSetName")

Remember SSRS is case sensitive so put your dataset name and Field names correctly.

Method 2

I prefer handling it in SQL as I want to keep the business logic out of RDLs.

You can use window functions to get the shipment count.

Also notice in the case of count I haven't added ELSE 0 condition. Because that will give wrong results. Count doesn't care what is the value inside it. It just counts all Non Null values. ELSE NULL will work.

SELECT Column1, Column2, Column3,
       SUM(CASE WHEN Doc_Type = 'Shipments' THEN 1 ELSE 0 END) OVER() ShipmentCount_UsingSum
       COUNT(CASE WHEN Doc_Type = 'Shipments' THEN 1 END) OVER() ShipmentCount_UsingCount
FROM myTable
JOIN....
WHERE .....

Now you can use this field in the report.

like image 36
Anup Agrawal Avatar answered Nov 04 '22 12:11

Anup Agrawal