Tuesday, October 6, 2015

[NAV] Using Groups to Choose the Number of Rows per Page

source: http://www.wiseowl.co.uk/blog/s257/ssrs-rows-per-page-pt2.htm

Using Groups to Choose the Number of Rows per Page

The most accurate way to control the number of records on a page in a report is to combine grouping with a simple expression.  This part of the blog series explains exactly how to make this technique work.

Step 1 - Create the Basic Report

For our example we'll create a simple report with a table containing a list of 263 films.  Our aim is to make each page of the report display only 10 rows.  The design of this report is shown in the diagram below:
Report design
This is the basic design of the report. For demonstration purposes we've added an expression to calculate the row number of each record in the table.
The expression that we've used in the Row column is shown in the diagram below:
Rownumber
The RowNumber function calculates the number of each row in the current table. We don't need to include it here but it helps to demonstrate the effect.
 
The results of running this report are as follows:
Preview of report
The report contains 6 pages. The first 5 pages contain 48 rows and the final page contains 23 rows, making a total of 263 records.

Step 2 - Add a Grouping Level

The next step is to add a grouping level to the table using the groups panel at the bottom of the screen:
Add group
Right-click on the (Details) row and choose Add Group > Parent Group...
On the dialog box that appears, click the button shown in the diagram below to launch the Expression Builder dialog box:
Launch expression builder
Click the fx button shown here to launch the Expression Builder.
In the next dialog box, enter the calculation shown below and click OK.
Expression
The number 10 in this expression ensures we see 10 rows per page, but you can change this value if you want to.
 
Click OK once more to apply the grouping level to the table.
Apply group
Click OK again to apply the grouping to the table.

Step 3 - Remove the Automatic Sorting

If you try to preview the report at this stage you'll receive an error message similar to the one shown below:
Error message
The error message you'll see when you preview the report.
To prevent this error from happening we need to remove the automatic sort that has been applied to the group.  Start by right-clicking on the group in the panel at the bottom of the screen:
Choose group properties
Right-click on the group name and choose Group Properties...
 
Complete the dialog box which appears as shown below:
Remove sort
The numbered steps are described below.
  1. Click on the Sorting tab.
  2. Select the sort that you want to remove.
  3. Click on the Delete button to delete the sort level.
  4. Click OK.
You should now find that when you preview the report you can see all of the data in groups of 10 rows.
Grouped data
Each set of 10 rows has a different value in the Group1 column.
The next step is to put each group on a separate page.

Step 4 - Add a Page Break Between Each Group

To add a page break between each group, right-click on the group name in the panel at the bottom of the screen:
Select group properties
Right-click the group name and choose Group Properties...
 
Complete the dialog box which appears as shown below:
Add page breaks
The numbered steps are described below.
  1. Select the Page Breaks tab.
  2. Check the box shown here.
  3. Click OK.
When you preview the report you should find that you have the specified number of records per page.
Final result
We get the exact number or records we require on each page, at the cost of having more pages in the report.
To tidy things up you could delete the first two columns in the example shown above - neither are required to make this technique work.
At this point you know everything you need to make this technique work for any number of records per page (up to the maximum that the page size can handle).  If you're interested in how the expression we've used actually works you can read the next part of this blog series to find out!

No comments:

Post a Comment