Interactive Sorting for a SSRS Report

Problem

You created a report for your client and the report default sorting is in descending order which was applied at the dataset level. But what if the client wants to see the data in ascending order? Or what if the client wants to be able to switch back and forth as needed? In this tip we look at how to make the sorting option interactive, so after the report has been rendered the user can changed the sort order.

Solution

This tip assumes that you have previous real world work experience building a simple SSRS Report. In this tip I will describe how to add interactive sorting in a Tablix in SQL Server Reporting Services. To demonstrate the solution, I will use the AdventureworksDW2008R2 sample database for SQL Server and SQL Server 2012 Reporting Service.

SQL Server Reporting Services comes with out of the box functionality known as interactive sorting. This feature allows users to sort the data at the report level in either ascending or descending order. The user can sort the data accordingly without the need to modify the dataset or report.

Step 1: Create New SSRS Report

Let’s create a sample report for demo purposes. Please change the report name to Interactive_Sorting. You can refer to the below image.

Adding New Report

Step 2: Create Data Source and Dataset in SSRS

I have already created a data source connection to the AdventureworksDW2008R2 sample database and let’s create the below dataset for this sample report.

Main Dataset: This dataset will be used for the report body and it returns 4 data fields. I have applied ascending sort order on the YearlyIncome data field.

SELECT 
FirstName, LastName, Gender, YearlyIncome
FROM  DimCustomer
ORDER BY YearlyIncome ASC

After creating the Main dataset, the Report Data Pane looks like the below image.

Report Data Pane After Adding Dataset

Step 3: Add Tablix in Reporting Services

Let’s add a Tablix for data viewing purpose. I will pull all 4 data fields from the Main Dataset into the Tablix. After adding the Tablix, the sample report looks like the below image.

Adding Tablix

Let’s preview this sample report, as you can see from the below image our sample report displays all the records in ascending order based on the YearlyIncome data field value.

Report Preview when data is sorted at dataset level

Now a user wants to see YearlyIncome data in descending order. To do this, either the dataset has to be modified with descending sorting on the YearlyIncome data field or you have to change sorting order at the report level on the YearlyIncome data field.

What if a user wants a choice to sort data in either ascending or descending order? To handle this kind of requirement SQL Server Reporting Services comes with out of the box functionality known as interactive sorting. We can provide interactive sorting at the report level on the YearlyIncome field.

Step 4: Adding Interactive Sorting to SSRS

Let’s add interactive sorting on the YearlyIncome column. Right click on the YearlyIncome column and select Text Box Properties, you can refer to the below image.

Adding Interactive Sorting YearlyIncome Textbox

Once you click on the Text Box Properties, a new Text Box Properties window will open. In the Text Box Properties window click on Interactive Sorting, you can refer to the below image.

YearlyIncome Textbox Interactive sorting Properties Window

Now we have to enable the sorting option for the YearlyIncome textbox. Check the “Enable interactive sorting on this text box” check box as shown below.

Enable Interactive Sorting Option For YearlyIncome Textbox

Once you enable Interactive Sorting, other options will be enabled. You can apply this interactive sorting on Detail rows or Groups. You have to select this as per your requirement. In our sample report we don’t have any groups, so we want to sort base on detail rows.

Select the Detail rows radio button and select the column we want to sort by. In our case we want to sort on YearlyIncome, so I have selected it in the “Sort by:” drop down option.

If you want to apply this sorting to all groups and data regions then you can check that check box. In our case it is not required, so I am not checking it.

Applying interactive sorting on Yearlyincome Textbox

Step 5: Report Preview

We have done all the necessary changes, now let’s preview the report.

For the first time when you will preview the report you will find two signs (up and down) next to the YearlyIncome column. These two signs represent the interactive sorting functionality. If we applied any sorting at dataset level then the report will show the sorted data by default, if no sorting is applied then report will show unsorted data.

As you can see from the below image, the report is sorted based on YearlyIncome by default. It is because we sorted the data at the dataset level.

Report Preview With Interactive Feature Default Order

Users can change the sorting at the report level just by clicking on the interactive sorting icon. Once the user clicks on the interactive sort icon, the interactive sort sign will change and the report will show the sorted data. If the interactive sort sign changed to single arrow and points down then it means the report is sorted in descending order. You can refer to the below image.

Report Preview With Interactive Feature Desc Order

If the user clicks on the interactive sort down arrow, the report will be sorted in ascending order.

Report Preview With Interactive Feature Asc Order

Next Steps

Leave a Reply

Your email address will not be published. Required fields are marked *