Thursday, August 14, 2008

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.

Hugo Eduardo said...

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

David Carrington said...

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.

David Carrington said...

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.

Irina Chernigina-Gomareli 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.