Note, before getting started with this lesson there are some prerequisites you should know about. Please read my post Getting Started with SQL Server 2008 to ensure you have everything setup correctly, otherwise you will be missing objects required to get the code here to work correctly.
Often when a user looks at a report, they don’t want the entire report but just subsets of the data. To assist those users we have two methods for reducing the amount of data on a report, Filter Parameters (often just called Filters) and Query Parameters.
Query Parameters are applied before the query to get data is sent to the database, and only the data the fits the criteria from the parameter is brought back. This is ideal for situations where most of the time a user is going to look at only one set of data the report can provide. A salesman, for example, who only wants to look at his sales data. Each time a query filter is applied a round trip to the database occurs to update the data in the report.
Filters, which we’ll work with in this lab, are applied after the data is returned from the server. All possible data is returned from the database, and then the filter is applied to the data being seen. This means as a user switches from one filter to another it is very fast, as all the data is already present in memory and only data being displayed is changing. A sales manager would be a good example, one who will want to see the data for all his or her sales people, but only one at a time.
Let’s start our look at Filters by creating a fairly simple report.
Step 1. Add the report
As with our other reports, right click on the Reports branch in Solution Explorer, pick Add New Report, and (if you haven’t already disabled it) click next to move past the welcome screen.
Step 2. Set the data source.
Pick the Contoso shared data source, or setup a new source to Contoso, and click Next.
Step 3. Setup the query.
In the query builder, we’ll be using one of our views. Enter this SQL statement:
SELECT [FiscalYear] , [ProductCategoryName] , [ProductSubcategory] , [Region] , [TotalAmount] FROM [ContosoRetailDW].[Report].[V_SubcategoryRegionTotalsByYear]
and click next.
Step 4. Set the format.
For the report type we’ll use the simple Tabular format, so just click Next.
Step 5. Determine field placement in the report.
To keep this simple we’ll not use any groups on this report, so just put all report fields into the Details section. You can do it in one easy step by clicking on the top most item (FiscalYear), holding down the shift key, and clicking the bottom item (TotalAmount). This will select all of the fields, just click the Details button to move them. Then click Next.
Step 6. Select the formatting Style
Once again we’ll go with Corporate for the style and click Next.
Step 7. Name the report.
Finally we’ll give the report a name of “Regional Sales by Subcategory” and click Finish.
Step 8. Format report columns
To make the report a little easier to read, expand the width of the columns and format the Total Amount as Currency. (See the previous labs if you don’t recall how to accomplish this.)
Previewing the report shows our data. There’s a lot of it, so let’s say we are the sales manager and we want to apply filters so we are only looking at pieces of our sales.
Step 9. Add a new dataset to act as a source for the filter parameter
There are several choices we can use for creating filters. We could allow the user to simply key a value to use into a text box. It is also possible to hard code a list of values, for example “Yes” and “No”. In most cases though, you will want to create a filter based on a set of values in the database, which is what we’ll do in this lab.
Here we’ll apply a filter for the Region. To do so we’ll first need to create a dataset to supply this list from the database. In the Report Data window (if it’s not visible pick View->Report Data from the main BIDS (aka Visual Studio) menu) right click on Contoso and pick “Add Dataset”.
Give the new data source a good name. Here we can use Region.
We have several choices for a data source; here take the default of Text (which means we’ll just enter a query).
In the Query area we have many choices. Even though there is a designer built in, the best way is still to use SQL Server Management Studio to create and test your query, then paste it in here.
SELECT [RegionCountryName] FROM [ContosoRetailDW].[Report].[V_Regions] ORDER BY [RegionCountryName]
Once you’ve entered the above, click OK.
Step 10. Add the Region parameter
Now that we have a new dataset, we need to add a parameter to apply to our filter. Still in the Report Data window, right click on the Parameters and pick “Add Parameter”.
Give the parameter a good name. This is the variable name you’ll use elsewhere to refer to this item. Remember it, as you’ll need it later! Make sure it has no spaces and follows other typical guidelines for naming variables. For this example we can use the word Region.
Next you want to supply a prompt. This is the message shown on the report beside the parameter selection control. For our example let’s use “Select a Region to work with:”
You should now indicate the data type for the parameter. There are only a few you can pick from, for this though the default of Text will do fine.
Users often want to see multiple items on a report, but not all, so we’ll allow them to pick more than one by checking on the “Allow multiple values” check box. Your parameter window should now look like:
Next we need to tell the report where to get the data from. On the Available Views area, select the “Get values from a query” option. Then pick the new dataset we created, the Region one.
Below this you will see the Value field and Label field options. Frequently when dealing with data we have primary key data, such as an INT, that is needed to link data together. But we also need a human readable value, something that the users can see and understand.
A good example is the classic Employee table. You have an EmployeeID and an EmployeeName. For the value field, you’d pick the EmployeeID, but for the Label field you use the EmployeeName.
In this particular case, we are using the same field for both, and it’s perfectly valid to do so. Just pick “RegionCountryName” for both Value and Label drop downs.
For the rest of this example, know that we won’t do anything with Default Values or the Advanced Area. In the Default Values we can pick or set a value to be the default, and the Advanced lets us determine how often we need to refresh the source data for our parameter.
Step 11. Bind the parameter to the main reports Dataset Filter
Now that the parameter is setup, we need to bind it to the Dataset for our main report. By default, the report wizard named it DataSet1 when it setup our report. Right click on it and pick Dataset Properties.
In “the real world” we should have already renamed the default name of DataSet1 to something more meaningful. Let’s do so now, and call it MainReportData.
Next, you might be tempted to click on the Parameters area. But in this case what we will be doing is using the Region parameter as a Filter. When we use it as a filter, Reporting Services reads in the entire dataset, and then applies the value in our parameter before displaying it. This means as we change the value of the parameter, the switch from one set of data to another goes very fast.
Let’s go down to the Filters area, and click Add. In the Expression, pick [Region] from the drop down. This is the Region field from our query, not the parameter. Since we want to allow multiple regions, change the Operator to “In”. Finally for the value, enter the name of the parameter preceded by an @ sign. [@Region] is what we’ll type in.
OK all done, click OK to complete your task.
Step 12. Preview your work
After you save your work, click on the Preview tab. You will now see a new area above the report with the prompt you entered.
Open the drop down and pick a few of the items.
Over on the right side you’ll see the View Report button. Click it to generate the report. As you select different countries, you’ll see the view of the report data updates very quickly.
thank you for this nice article
The stuff is really helpful for the begineers. Really appreciate the effort of publishing this document.
Thank you! Finally, something I can understand. Very helpful.
Hi Dude..
Thanks Lot man…..No one is declared that we need to create one more data set to achieve the task…It is very help full..
Thanks again .
Shiva vp
Very interesting. How about making the parameters optional?
Awesome article
thanks for the article but how to add ALL to filter list? e.g. if user want to select ALL which is not available in database, how to make the report/filter work?
Thanks
I have similar question as ajitha.
What if one choice is subset data of the another choice. For example if you have region of China vs. SouthEast Asia (which includes China, Vietnam, Thailand…) ?
How do I filter from one vs the other….
Thank you. This was very helpful. I particularly appreciated the distinction between the Filters and Parameters sections.
Very helpful article.
How can i apply this filter data to report for fetching related information.
AM
Hi, its not working in SSRS 2005. any idea why?
Thanks
Excellent instructions – well written, clear, without ambiguity or unnecessary complexity. This is one of the best ‘how to’ documents i’ve seen on this subject.
Great article. Well worth the read. One small correction is needed: “On the Available Views area, select the “Get values from a query” option.” should read “On the Available Values area, select the “Get values from a query” option.”
Thank you. That works great 🙂