Lesson 2: Adding Parameters to Create a List of Available Values
Available values, or valid values, provide a users with a list of possible values for a report parameter. As a report author, you can provide valid values from a query specifically designed to retrieve a set of values from the data source, or you can provide a predefined set of values. By binding a set of available values to a dataset query that runs when the report is processed, you make sure that only values that exist in the database can be chosen from the drop-down list.
In this lesson, you will modify the Sales Orders report to present a drop-down list of available salesperson names from the AdventureWorks2008 database. You will set a table property to display a message when there are no rows in the result set for the selected parameter value. When you choose a name and view the report, the report shows the sales for that salesperson only.
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], 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)) AND soh.SalesPersonID = (@BusinessPersonID)
This is the same query as before, except that a condition has been added that limits the result set to one sales person:
AND soh.SalesPersonID = (@BusinessPersonID)
Click the Run (!) button. When prompted for the query parameters, use the following table to enter values.
@StartDate
20010101
@EndDate
20030101
@BusinessPersonID
290
Click OK. The result set appears for the sales person Ranjit Varkey Chudukatil with SalesPersonID = 290.
To populate a valid values list for a report parameter
In the Report Data pane, click New, and then click Dataset. The Dataset Properties dialog box opens.
In the Name field, type BusinessPersons. This dataset will be used to populate the valid values list for the SalesPersonID report parameter.
Verify that the data source is AdventureWorks_Ref.
Paste the following Transact-SQL query in the query pane:
SELECT SP.BusinessEntityID, C.FirstName, C.LastName FROM Sales.SalesPerson AS SP INNER JOIN HumanResources.Employee AS E ON E.BusinessEntityID = SP.BusinessEntityID INNER JOIN Person.Person AS C ON C.BusinessEntityID = E.BusinessEntityID ORDER BY SP.BusinessEntityID
Click OK twice. A list of fields is populated for the BusinessPersons dataset. This dataset will be used to provide valid values for the parameter BusinessPersonID.
You will notice that the BusinessPersons dataset has fields called FirstName and LastName. Next, we will concatenate these fields into one field called Name.
To define a calculated field in the Report Data pane
In the Report Data pane toolbar, right-click the BusinessPersons dataset, and click Add Calculated Field. The Fields page of the Dataset Properties dialog box opens with a new row added to the grid.
In the last Field Name text box, type Name.
In the Field Source text box, paste the following expression:
=Fields!LastName.Value & ", " & Fields!FirstName.Value
Click OK.
In the Report Data pane, under the BusinessPersons dataset, the new field Name appears in the field collection for the dataset.
To populate the report parameter with a list of available values
In the Report Data pane, expand the Parameters node and right-click BusinessPersonID, then click Parameter Properties.
In Prompt, type Select business person:.
In Data type, select Integer.
Click Available Values.
Select the Get values from a query option.
From the Dataset drop-down list, select BusinessPersons.
From the Value field drop-down, select BusinessEntityID.
From the Label field drop-down, select Name.
By selecting Name for the label, the drop-down list of valid values for the BusinessEntityID parameter will now show the name of each sales person instead of the sales person number.
Click Default Values.
Select the Get values from a query option.
From the Dataset drop-down list, select BusinessPersons.
From the Value field drop-down list, select BusinessEntityID.
Click OK.
Click the Preview tab. The report shows a drop-down list with business person names.
Click View Report. Select other parameter values to review the results.
Next Steps
You have successfully added a list of available values for a parameter to an existing report. Next, you will modify the DayoftheWeek and SalesPersonID parameters to be multivalued. See Lesson 3: Adding Parameters to Select Multiple Values in a List.