Lesson 1: Adding Parameters to Filter Reports by Date
By including parameters for a start and end date in a query for your report, you can specify a date range that limits the data retrieved from the data source. You can create additional parameters to filter the data after it is retrieved from the data source.
In this lesson, you will add the parameters @StartDate and @EndDate to the query to limit the data retrieved from the data source. Two report parameters, StartDate and EndDate, are automatically created for you and appear in the Report Data pane. Parameters are case-sensitive. Query parameters begin with the @ symbol, whereas report parameters do not.
You will set the data type of the parameters to DateTime and see that a calendar control appears with the parameter text box on the report viewer toolbar. You will set default values for the parameters so that the report can run automatically. Finally, you will create a report parameter DayofWeek that is not bound to a query parameter and use it to filter data after the data is retrieved from the data source.
This tutorial requires that you have completed Tutorial: Creating a Basic Table Report.
To open an existing report server project
Click Start, point to All Programs, point to Microsoft SQL Server 2008, and click Business Intelligence Development Studio.
From the File menu, point to Open, and click Project/Solution.
Navigate to Tutorial.sln. This is the tutorial that was created in the Tutorial: Creating a Basic Table Report.
Click OK to open the project. The Tutorial project is displayed in Solution Explorer with one report called Sales Orders.rdl.
Note If Solution Explorer is not visible, from the View menu, click Solution Explorer.
To convert an embedded data source to a shared data source
In the Report Data pane, right-click the data source AdventureWorks and select Convert to Shared Data Source. A data source called AdventureWorks.rds is added in Solution Explorer.
In the Report Data pane, right-click the AdventureWorks data source and select Data Source Properties.
In Name, type AdventureWorks_Ref.
Click OK.
To replace the existing dataset
In the Report Data pane, right-click the dataset AdventureWorksDataset, and then click Dataset Properties.
Note
If you do not see the Report Data pane, from the View menu, click Report Data.
In Data source, verify that AdventureWorks_Ref is selected.
In Query type, verify that Text is selected.
Click the Query Designer button to open the query designer.
Replace the text with the following query into the text box.
SELECT soh.OrderDate AS [Date], soh.SalesOrderNumber AS [Order], pps.Name AS Subcat, pp.Name as Product, SUM(sd.OrderQty) AS Qty, SUM(sd.LineTotal) AS LineTotal FROM Sales.SalesPerson sp INNER JOIN Sales.SalesOrderHeader AS soh ON sp.BusinessEntityID = soh.SalesPersonID INNER JOIN Sales.SalesOrderDetail AS sd ON sd.SalesOrderID = soh.SalesOrderID INNER JOIN Production.Product AS pp ON sd.ProductID = pp.ProductID INNER JOIN Production.ProductSubcategory AS pps ON pp.ProductSubcategoryID = pps.ProductSubcategoryID INNER JOIN Production.ProductCategory AS ppc ON ppc.ProductCategoryID = pps.ProductCategoryID GROUP BY ppc.Name, soh.OrderDate, soh.SalesOrderNumber, pps.Name, pp.Name, soh.SalesPersonID HAVING (ppc.Name = 'Clothing' AND (soh.OrderDate BETWEEN (@StartDate) AND (@EndDate)))
This is the same query as before, except that a condition with two limiting parameters has been added:
AND (soh.OrderDate BETWEEN (@StartDate) AND (@EndDate))
Click Run (!) on the toolbar. The Define Query Parameters dialog box opens to prompt you for parameter values.
Provide two values to see a filtered result set:
In the Parameter Value column, enter a value for @StartDate, for example, 20010101.
In the Parameter Value column, enter a value for @EndDate, for example, 20030101.
Click OK.
The result set displays a filtered dataset for orders in the years 2001 and 2002.
Click OK twice. The Report Data pane is populated with the dataset fields. Also note that two report parameters, StartDate and EndDate, are automatically created and appear under the Parameters node.
After you have defined query parameters for the report, you have to change the data type of the report parameters to match the data type of the source data. The default is Text, which maps to the String data type in most data sources. If a source data is numeric, Boolean, or Date/Time, you have to change the report parameter data type.
To change the data type and default values of a report parameter
In the Report Data pane, expand Parameters, and double-click StartDate. The Report Parameter Properties dialog box opens.
Verify that the name of the parameter is StartDate and the prompt is Start Date.
In Data type, select Date/Time.
Click OK.
In the Report Data pane, double-click EndDate. Verify the name and prompt values.
In Data type, select Date/Time.
Click OK.
Click Preview. The StartDate and EndDate parameters each appear on the report toolbar with a calendar control. Calendar controls automatically appear when the parameter is data type Date/Time and you have not defined an available values list. If you define an available values list, a drop-down list of values appears instead.
Provide two parameter values to run the report:
In the StartDate parameter text box, enter the date 2001-01-01.
In the EndDate parameter text box, enter the date 2003-01-01.
Click View Report. The report displays only the data that falls within the report parameter values.
After you have created report parameters for the report, you can add default values for those parameters. Default parameters allow the report to run automatically; otherwise, a user must enter parameter values to run the report.
To set default values for parameters
In Design view, in the Report Data pane, expand Parameters, and double-click StartDate. The Report Parameter Properties dialog box opens.
Click Default Values.
Select the Specify values option. The Add button and an empty Value grid appears.
Click Add. An empty row is added to the grid.
Click in the Value text box and delete the default text (Null).
Type 2001-01-01. Click OK.
In the Report Design pane, and double-click EndDate.
Click Default Values.
Select the Specify values option.
Click Add.
Type 2003-01-01. Click OK.
Click Preview. The report runs immediately because there are default values defined for all parameters.
To add a new field to the query to use for filtering
Switch to Design view.
Right-click the dataset AdventureWorksDataset and select Dataset Properties. Open the query designer and replace the query with the following new query:
SELECT soh.OrderDate AS [Date], DATENAME(weekday, soh.OrderDate) as Weekday, soh.SalesOrderNumber AS [Order], pps.Name AS Subcat, pp.Name as Product, SUM(sd.OrderQty) AS Qty, SUM(sd.LineTotal) AS LineTotal FROM Sales.SalesPerson sp INNER JOIN Sales.SalesOrderHeader AS soh ON sp.BusinessEntityID = soh.SalesPersonID INNER JOIN Sales.SalesOrderDetail AS sd ON sd.SalesOrderID = soh.SalesOrderID INNER JOIN Production.Product AS pp ON sd.ProductID = pp.ProductID INNER JOIN Production.ProductSubcategory AS pps ON pp.ProductSubcategoryID = pps.ProductSubcategoryID INNER JOIN Production.ProductCategory AS ppc ON ppc.ProductCategoryID = pps.ProductCategoryID GROUP BY ppc.Name, soh.OrderDate, soh.SalesOrderNumber, pps.Name, pp.Name, soh.SalesPersonID HAVING (ppc.Name = 'Clothing' AND (soh.OrderDate BETWEEN (@StartDate) AND (@EndDate)))
In the query, an additional calculated column is defined for the day of the week a sale occurred by adding the following command to the SELECT statement:
DATENAME(weekday, soh.OrderDate) as Weekday.
Click Run (!).The Define Query Parameters dialog box opens.
In the Parameter Value column, enter a value for @StartDate, for example, 20010101.
In the Parameter Value column, enter a value for @EndDate, for example, 20030101.
Click OK. You should see a new column in the result set labeled Weekday.
Click OK twice. In the Report Data pane, verify that Weekday is a field.
(Optional) To format the date in the table data that will be filtered
Click the Design tab.
Right-click the cell with the [Date] field expression and then click Text Box Properties.
Click Number, and then in the Category field, select Date.
In the Type box, select Monday, January 31, 2000.
Click OK.
To add a new report parameter
In Design view, in the Report Data pane, click New, and then click Parameter. The Report Parameter Properties dialog box opens.
In Name, type DayoftheWeek.
In Prompt, type Filter on the day of the week:.
Verify that the data type is Text.
Click Default values.
Select the Specify values option. The Add button and an empty Value grid appears.
Click Add.
Type Friday.
Click OK.
To set a table filter with a parameter expression
In Design view, right-click on a row or column handle of the table and select Tablix Properties.
Note
The Table data region is a template based on a Tablix data region.
Click Filters. An empty filter grid appears.
Click Add. An empty row is added to the grid.
In Expression, from the drop-down list, select [Weekday].
Verify that Operator displays the equal sign (=).
Click the expression (fx) button next to the Value text box. The Expression dialog box opens.
In Category, click Parameters. The current list of parameters appears in the Values pane. Double-click DayoftheWeek. The parameter expression is added to the expression text box. The following expression now appears in the expression text box: =Parameters!DayoftheWeek.Value.
Click OK. Click OK again to exit the Tablix Properties dialog box.
The filter for the table is now set to compare the value in the field Weekday with the parameter value for DayoftheWeek. For example, when you enter the value Friday in the report toolbar for DayoftheWeek, the report processor will process only those rows in the table where the value for the field Weekday is Friday.
Click Preview. Because all parameters have default values, the report runs automatically. The table shows only values that fall within the date range defined by StartDate and EndDate and that fall on a Friday.
Next Steps
You have successfully defined query parameters and report parameters, set default values for the parameters, and set a filter on the table. In the next lesson, you will learn how to create an available values, or valid values, list for a parameter. See Lesson 2: Adding Parameters to Create a List of Available Values.