Skip to main content

Display Top N Rows

In SQL Server Reporting Services, you can dynamically limit the number of rows that are displayed in a table or a matrix. Here are the steps to create a sample report showing this.

1. Create a DataSource connecting to the AdventureWorksDW database and a DataSet with the following query:
SELECT DimEmployee.FirstName, DimEmployee.LastName, SUM(FactResellerSales.SalesAmount) AS SalesAmount
FactResellerSales ON DimEmployee.EmployeeKey = FactResellerSales.EmployeeKey
WHERE (FactResellerSales.OrderDateKey = 20011001)
GROUP BY DimEmployee.FirstName, DimEmployee.LastName

2. Add a report parameter named “NumberPpl” of data type integer.

3. Add a table to the body of the report with the data fields of firstName, lastName, and salesAmount. Sort by the column salesAmount from ‘Z to A’.

4. Set the Filters option of the table as shown below. (This is where the real work is happening!)

5. Preview the report, modifying the value for the parameter NumberPpl to see the number of rows change. It should look similar to the reports below.

Version: SQL Server 2008 RC0


Anonymous said…
This comment has been removed by a blog administrator.
yOOrek said…
Thanks, this has helped me a lot. I was trying to achieve this in SQL statement with a parameter but it didn't work. The way you described it is perfect.
Hi Jessica,
This same case should I apply to RS 2005 but I get an error that says:

"An error occurred during local report processing.
An error has occurred during report processing.
Failed to evaulate the FilterValue."

Thanks for the help.
Jessica M. Moss said…
Hi Hugo,

The same logic should work for 2005 as well. Double check the parameter name in the Value field, and ensure that you are using brackets, an at symbol, and the exact name of the Parameter.

HTH, Jessica
This mostly works, except in my particular case I'm sorting on a column which has a few identical values, so when I say to give me the "Top 5", it gives me 8, since the last 4 all have the same value.
Further - my solution was to use Top N on another unique column at the same time.
Jessica M. Moss said…
Hi David,

Try using the alternate method: Make sure you order by an extra column, so that you can guarantee which of the four rows will be shown.

Good luck!
Anonymous said…

Thanks! Google pointed me right to this. It is exactly what I was looking for!

Anonymous said…
Hugo, that error occurs if you create the parameter with the wrong data type - such as text. Change it to an integer and it should work
Gilbert P said…
Awesome! Thanks for this.
Anonymous said…
Awesome. This worked perfectly.

Anonymous said…
Hi Jessica,

when Im trying this, I get an error
Microsoft Sql Server Repport Designer

value is not a decimal.

even though in parameter i have changed the data type to interger. I have also tried to change it to float as well.
JimC said…
Hi, Jessica

Thanks for the blog. I was wondering if there is a way to report the SUM of Top N's Amount?

Jessica M. Moss said…
Hi Jim,

You may want to use the alternative top n method found here: That will allow you to sum.

Andrea Grimaldi said…
I'm completely new at this. Would you please tell me how to create a report parameter? Please....
Jessica M. Moss said…

Please research "Creating report parameters in SSRS" in Google or Bing for information on creating report parameters.

Anonymous said…
Thank you so much!
Everything perfect is simple!
You are the best.
Anonymous said…
Very Nice.
Unknown said…
Hello, you say that top n rows will be displayed, what about downloaded data? it also will be filtered or unfiltered data will be rendered?? thank you
Jessica M. Moss said…
Hi Irina,

All rows are queried/downloaded from the source. Only the top N rows will be visible. If you only want to query/download N rows, try this method:

Jatin Shah said…
I have same kinda thing but in value I specified expression based on multivalued text type parameter which I am using as an input

=IIF(Parameters!Task.Count > 2,Parameters!Task.Value(2),"")

If I am spcifying "string" instead of Parameters!Task.Value(2) it works fine. I also tried by using CStr function but its not helping.
Jessica M. Moss said…
Hi Jatin,

The IIF function evaluates both the true and false parts, so I'm wondering if it's failing because there are less than 3 items? Try adding a few more elements in the task parameter and see if that helps.

CG said…
You first posted this years ago and it is still valuable information! Setting the filter on the tablix property was the only way that "SELECT TOP (@Parm) * ..." would work for me in the dataset. Thank you, thank you, thank you!!!
Anonymous said…
the same I am trying to do but its give me following error message
"A filter value in the filter for tablix specified data typethat is not supported by the 'TopN' operator. verify that the data type for each filter value is integer.
Unknown said…
Excellent, this approach is better than having "top(x)" directly in data source (sql statement). i.e. I have a data source common for few tablix-es, each tablix with it's own order. Setting the "top x" in tablix offers the necessary flexibility.
Marie Chase said…
Hi ggreat reading your post

Popular posts from this blog

Upgrading your SSIS Management Framework: Part 3

At this point, you understand the options for moving an SSIS framework to the latest version of SSIS, and you've upgraded the logging portion of the framework using a hybrid approach.  The final step in the framework upgrade is handling your configurations.  Let's walk through an existing configuration implementation and how you can upgrade it by combining your existing implementation with the standard SSIS framework. Overview A typical "old-school" configuration scheme is described in the SSIS PDS book or in this blog post here: .  Starting in SSIS 2012, the configuration scheme uses environments and parameters when using the Project Deployment Model, as discussed here: . In both scenarios, the core ideas in a configuration scheme are: Provide the ability to move packages through environments without having

Manipulating Excel Spreadsheets in SSIS

Tom, an attendee at last weekend’s SQLSaturday Olympia , asked me how to refresh a spreadsheet from within SQL Server Integration Services. My first thought was to turn on the connection’s “Refresh data when opening the file” option in the spreadsheet itself and avoid the situation entirely; however, this may not always be a viable solution. Here are the steps to perform the refresh from within an SSIS package. First, ensure that Microsoft.Office.Interop.Excel is registered in the GAC. If not, install the 2007 Microsoft Office system Primary Interop Assemblies . This will need to be done on any machine where you plan on running this package. Next, create a script task in your SSIS package that contains the following code (include your spreadsheet name): Imports System Imports System.Data Imports System.Math Imports Microsoft.SqlServer.Dts.Runtime Imports Microsoft.Office.Interop.Excel Public Class ScriptMain Public Sub Main() Dts.TaskResult = Dts.Results.Success Dim excel

Reporting Services 2008 Configuration Mistake

To start working with the management side of SQL Server Reporting Services 2008, I decided to set up a report server and report manager. Unfortunately, I made a mistake while setting up my configuration that left me a little perplexed. Here are the steps I took to cause, track down, and solve the issue. Problem: I began by opening the Reporting Services Configuration Manager from the Start Menu. I clicked through each of the menu options and accepted the defaults for any question with a warning symbol, since warning symbol typically designate an action item. After two minutes, all of the warning symbols had disappeared, and I was ready to begin managing my report server. Unfortunately, opening up a browser and trying to open up the report manager resulted in the dreaded " The report server has encountered a configuration error. (rsServerConfigurationError) " message. Sherlock-ing it: I put on my sleuthing hat and went to the log file directory: C:\Program Files\Microsoft