Working with Reporting Services Filters – Part 3: The TOP and BOTTOM Operators

Posted by smisner on SQL Blog See other posts from SQL Blog or by smisner
Published on Fri, 07 Jan 2011 23:07:00 GMT Indexed on 2011/01/07 23:58 UTC
Read the original article Hit count: 398

Filed under:

Thus far in this series, I have described using the IN operator and the LIKE operator. Today, I’ll continue the series by reviewing the TOP and BOTTOM operators.

Today, I happened to be working on an example of using the TOP N operator and was not successful on my first try because the behavior is just a bit different than we find when using an “equals” comparison as I described in my first post in this series. In my example, I wanted to display a list of the top 5 resellers in the United States for AdventureWorks, but I wanted it based on a filter. I started with a hard-coded filter like this:

Expression Data Type Operator Value
[ResellerSalesAmount] Float Top N 5

And received the following error:

A filter value in the filter for tablix 'Tablix1' specifies a data type that is not supported by the 'TopN' operator. Verify that the data type for each filter value is Integer.

Well, that puzzled me. Did I really have to convert ResellerSalesAmount to an integer to use the Top N operator?

Just for kicks, I switched to the Top % operator like this:

Expression Data Type Operator Value
[ResellerSalesAmount] Float Top % 50

This time, I got exactly the results I expected – I had a total of 10 records in my dataset results, so 50% of that should yield 5 rows in my tablix.

image

So thinking about the problem with Top N some  more, I switched the Value to an expression, like this:

Expression Data Type Operator Value
[ResellerSalesAmount] Float Top N =5

And it worked!

So the value for Top N or Top % must reflect a number to plug into the calculation, such as Top 5 or Top 50%, and the expression is the basis for determining what’s in that group. In other words, Reporting Services will sort the rows by the expression – ResellerSalesAmount in this case – in descending order, and then filter out everything except the topmost rows based on the operator you specify.

image

The curious thing is that, if you’re going to hard-code the value, you must enter the value for Top N with an equal sign in front of the integer, but you can omit the equal sign when entering a hard-coded value for Top %. This experience is why working with Reporting Services filters is not always intuitive!

When you use a report parameter to set the value, you won’t have this problem. Just be sure that the data type of the report parameter is set to Integer. Jessica Moss has an example of using a Top N filter in a tablix which you can view here.

Working with Bottom N and Bottom % works similarly. You just provide a number for N or for the percentage and Reporting Services works from the bottom up to determine which rows are kept and which are excluded.

© SQL Blog or respective owner

Related posts about ssrs