One of the important factors to be considered and taken care in SSRS report designing is Ordered display of report:
In this article, let us see examples for
Consider sample Dataset query for this example as shown below:
DECLARE
@Tmp
TABLE
(Id
VARCHAR
(5),
Name
(5))
INSERT
SELECT
1,
'C'
2,
'B'
3,
'A'
*
FROM
Create Table report based on above Dataset and when the report is previewed, you will notice the report getting displayed as shown below: (with default sorting of rows as returned from Dataset) If we want the report rows to be displayed in the ascending sort order based on Name column, we can achieve that in two ways : i) By adding ORDER BY clause in the Dataset as shown below:
ORDER
BY
ii) Select the entire row in the Tablix, right-click - > Tablix Properties ... - > Sorting - > Add - > mention Sort By = Column and Order = Ascending/Descending After adding Sort option, when we preview the report again, we will notice sorted output as shown below:
@SalesbyMonth
(Product
(20),Months
(20),MonthNo
INT
,Sales
)
,
'Oct'
,10,1000
'Nov'
,11,2000
'Dec'
,12,3000
Create Matrix report based on above Dataset and then if the report is previewed , you will notice the report getting displayed as shown below : (with default sorting of Columns): If we want the report columns to be displayed in the ascending sort order based on MonthNo column, we can achieve this by: Click on Months column - > right-click - > Column Group - > Column Groups Properties ... - > Sorting - > Add - > mention Sort By = Column and Order = Ascending/Descending or At the bottom of the Design pane , Column Groups - > Column Groups Properties ... - > Sorting - > Add - > mention Sort By = Column and Order = Ascending/Descending After adding Sort option, when we preview the report again, we will notice sorted output as shown below:
You can add interactive sort buttons to enable a user to toggle between ascending and descending order for rows in a table or for rows and columns in a matrix. The most common use of interactive sort is to add a sort button to every column header. The user can then choose which column to sort by. This feature is supported only in rendering formats that support user interaction, such as HTML. Example 1 : Interactive Sort option for Column headers of table
Consider sample Dataset query for this example as shown below :
Create Table report based on the above Dataset. To enable Interactive sorting for Column header Id , In the Design pane , click on the Column header Id - > right-click - > TextBox Properties - > Interactive Sorting
Similarly do for the Column header Name . After adding interactive sort option, when we preview the report again, we will notice Up/down arrows in the column headers which will allow the users to perform interactive sorting as shown below : Example 2 : Interactive Sort option for Column headers of table with group. Consider sample Dataset query for this example is as shown below :
(Country
(20) , State
(20))
'US'
'Texas'
'New York'
'California'
'India'
'Mumbai'
'Kolkata'
'Chennai'
Create Table report based on above Dataset and then Create Row group on Country column and then if we Preview the report, we will notice as shown below : Enable Interactive Sorting option for Column header Country as shown below : Enable Interactive Sorting option for Column header State as shown below : After adding interactive sort option, if we again preview the report, Clicking arrow symbol on Country column header will sort the entire group Clicking arrow symbol on state column header will sort rows within the group
Dynamic sorting - ( by getting column name as input from user based on that column sorting has to be performed ) can be done in two ways : Approach 1 : Using ORDER BY clause with CASE statement
Consider sample Dataset Stored procedure for this example is as shown below :
CREATE
PROCEDURE
DynmaicSort
@Sortby NVARCHAR(10)
AS
BEGIN
CASE
WHEN
@Sortby =
'Name'
THEN
ELSE
Id
END
Dataset Properties is as shown below :
Create Table report and then if we Preview the report , users will be prompted for input column name based on which report rows will be sorted .
Approach 2 : Writing expression for Sorting
Select the entire row in the Tablix , right-click - > Tablix Properties ... - > Sorting - > Add - > write expression for sorting .
Refer to the answer provided by Heidi-Duan in this thread .
Consider sample Dataset query for this example is as shown below :
(Months
(20),Sales
,MonthNo
'January'
,1000,1
'February'
,100,2
'March'
,100,3
'April'
,200,4
'May'
,500,5
'June'
,800,6
'July'
,500,7
'August'
,100,8
'September'
,900,9
'October'
,600,110
'November'
,100,11
'December'
,100,12
Create Column chart report based on above Dataset . To display all labels on the X-axis . If we Preview the report , Month labels on the X-axis will not be in proper order . To sort labels in X-axis of charts , Under Chart Data - > Category Groups - > Category Group Properties ... -> Sorting After adding sort option, if we again Preview the report, we will notice proper order of labels in X-axis
Create Stacked Column chart report based on above Dataset . If we Preview the report , we will notice the series groups in default sorted order . To sort series groups in stacked charts , Under Chart Data - > Series Groups - > Series Group Properties ... -> Sorting After adding sort option, if we again preview the report, we will notice the report with series groups sorted in the order we mentioned :
Additional Resources
See Also