In this article, we will show you how to use the SSRS Lookup function and its advantages. Or, How to combine multiple datasets in the SSRS report. Or, How to work with multiple data sets in report creation using SSRS Lookup with an example.
We are going to use the below-shown DataSet to explain the SSRS lookup function. Please refer to Embedded Data Source, and Dataset articles to understand the steps involved in creating Embedded Data Source, and Dataset that we used for this report.
Data written by the custom SQL query that we used in the above Dataset is:
SSRS Lookup Example
To demonstrate the Lookup in SSRS, we are going to use the previously designed report.
Please refer to SSRS Table Report, Format Table Report in SSRS article to understand the steps involved in creating Table Report & format, and Add headers and Footer to SSRS Report for adding Page headers, and footers.
First, let me show you the Report Preview.
The above report is the Client’s primary criteria, and all of a sudden client asked you to add the Department Name to the above report. As you know that the Employees table doesn’t have Department Name, let me add another dataset.
To add the second dataset, right-click on the Datasets folder and select the Add Dataset.. option from the context menu.
The following screenshot will show you the Embedded Dataset that we are using
Data present in the Department Table is:
You might be tempted to add the Department name easily. Let me show you what will happen if I drag and drop the Department Name to the existing table?
From the above screenshot, you can see that it is throwing an error because the report designer will only allow you to use only one dataset.
Now let me show you the ideal approach to do so. First, insert a column to the right side of a Sales column by right-clicking on the Sales column -> Insert Column -> Right
before we go further, let me explain the SSRS Lookup function
Lookup in SSRS Example
The SSRS lookup function returns the matching records from the specified dataset. And the syntax is:
Lookup(Source_IDColumn, Target_IDColumn, Target_Column, Target_Dataset)
- Source_IDColumn: Similar to a Primary key. It is the Id column of the first dataset.
- Target_IDColumn: Similar to a Foreign key column. It’s the Id column of the Second dataset. SSRS Lookup function will check whether Source_IDColumn is equal to Target_IDColumn and if they match then corresponding Target_Column value will return
- Target_Column: Column that you want to add to the existing table.
- Target_Dataset: Second Dataset name.
Next, Please select the newly created column, and right-click on it will open the context menu. Please select the Expression option from it.
Once you select the Expression option, a new window called the Expression will open. Use this expression window to write the SSRS lookup function.
=Lookup(Fields!DeptID.Value, Fields!DeptID.Value, Fields!DepartmentName.Value, "SQLTESTDepartmentDS")
Once you completed with SSRS lookup function, Click OK to close the window
As you can see that our report preview is displaying the Department Name. It means, the SSRS Lookup report is displaying data from multiple datasets