Lesson 1: Adding Parameters to Filter Reports by Date (Report Builder 2.0)
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. In Transact-SQL, query parameters begin with the @ symbol, whereas report parameters as displayed in the Report Data pane 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 (Report Builder 2.0).
To open an existing report
Click Start, point to Programs, point to Microsoft SQL Server 2008 Report Builder, and then click Report Builder 2.0.
From the Report Builder button, click Open.
Navigate to Sales Order. This is the report that was created in the Tutorial: Creating a Basic Table Report (Report Builder 2.0).
Click Open. The report definition opens in design view.
Next, you will replace the dataset query with a query that contains a query parameter.
To replace the existing dataset query
In the Report Data pane, right-click the dataset DataSet1, and then click Query. The query designer opens.
In the query pane, replace the text with the following query:
SELECT SH.OrderDate ,SH.SalesOrderNumber ,SD.OrderQty ,SD.LineTotal ,P.Name AS [Product] ,PS.Name AS [Subcategory] FROM Sales.SalesOrderHeader AS SH INNER JOIN Sales.SalesOrderDetail AS SD ON SH.SalesOrderID = SD.SalesOrderID INNER JOIN Production.Product AS P ON SD.ProductID = P.ProductID INNER JOIN Production.ProductSubcategory AS PS ON PS.ProductSubcategoryID = P.ProductSubcategoryID INNER JOIN Production.ProductCategory AS PC ON PC.ProductCategoryID = PS.ProductCategoryID WHERE PC.Name = 'Clothing' AND SH.OnlineOrderFlag = 0 AND (SH.OrderDate BETWEEN (@StartDate) AND (@EndDate))
This is the same query as before, except that the sort ORDER BY clause has been removed and a condition with two limiting parameters has been added:
AND (SH.OrderDate BETWEEN (@StartDate) AND (@EndDate))
Click Run (!) on the toolbar. The Define Query Parameters dialog box opens and prompts for a value for each parameter.
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 dataset with values for orders in the years 2001 and 2002.
Click OK.
In the Report Data pane, expand the Parameters node to view the two report parameters, StartDate and EndDate, that are automatically created from the query parameters.
After you have defined query parameters that represent a date or time, you must change the data type of the report parameters to match.
To change the data type and default values of a report parameter
In the Report Data pane, right-click StartDate, and then click Parameter Properties.
Verify that the name of the parameter is StartDate and the prompt is Start Date.
In Data type, select Date/Time. This sets the report parameter data type that is saved in the report definition.
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 Run to preview the report. 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. You can use the calendar control or just type the following dates in:
In the StartDate parameter text box, type 2001-01-01.
In the EndDate parameter text box, type 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 enable the report to run automatically; otherwise, a user must enter parameter values to run the report.
To set default values for parameters
Switch to 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.
In the Value text box, replace the default text (Null) and 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 each parameter has a default value.
To add a new field to the query to use for filtering
Switch to Design view.
Right-click the dataset DataSet1 and select Dataset Properties. Open the query designer and replace the query with the following new query:
SELECT SH.OrderDate ,DATENAME(weekday, SH.OrderDate) as Weekday ,SH.SalesOrderNumber ,SD.OrderQty ,SD.LineTotal ,P.Name AS [Product] ,PS.Name AS [Subcategory] FROM Sales.SalesOrderHeader AS SH INNER JOIN Sales.SalesOrderDetail AS SD ON SH.SalesOrderID = SD.SalesOrderID INNER JOIN Production.Product AS P ON SD.ProductID = P.ProductID INNER JOIN Production.ProductSubcategory AS PS ON PS.ProductSubcategoryID = P.ProductSubcategoryID INNER JOIN Production.ProductCategory AS PC ON PC.ProductCategoryID = PS.ProductCategoryID WHERE PC.Name = 'Clothing' AND SH.OnlineOrderFlag = 0 AND (SH.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.
Now that the table has data that identifies the day of the week, you can add a filter so that only sales on specific of the week are used in the report. To help verify this, you can display the day of the week with the sales order date.
To format the date in the table
Switch to Design view.
Click the cell with the OrderDate field expression. On the Home tab, in the Number group, click the dialog box launcher.
In Category, verify that Date is selected.
In the Type box, select a date type that displays the day of the week. For example, for regional setting English (United States), you can select Monday, January 31, 2000.
Click OK.
Click Run to preview the report. The date in the first column of the table displays the day of the week as well as the calendar date.
Next, you will add a parameter to enable the user to filter by the day of the week.
To add a new report parameter
Switch to 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.
Next, you will add a filter to display only the value you choose for the DayoftheWeek parameter. You can add a filter to the table or to the dataset. This report has both a table and chart that display the same data. Add the filter to the table if you want to display the filtered data in the table only. Add the filter to the dataset if you want both the table and the chart to display filtered data. In the following procedure, you will set the filter on the dataset.
To set a dataset filter with a parameter expression
In Design view, in the Report Data pane, right-click DataSet1, and then click Dataset Properties.
Click Filters.
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.
Value displays the simple expression for the DayoftheWeek parameter: [@DayoftheWeek].
Click OK.
The filter for the dataset 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 and chart show only values that fall within the date range defined by StartDate and EndDate and that fall on a Friday. For this dataset, only three dates fall on a Friday:
February 1, 2002
March 1, 2002
November 1, 2002
Next Steps
You have successfully defined query parameters and report parameters, set default values for the parameters, and set a filter on a dataset. 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 (Report Builder 2.0).