Introduction
This is the final post to discuss the last Lookup function Multilookup(). The other two – Lookup() and LookupSet() have already been discussed in my previous posts.
Syntax
Multilookup(source_expression, destination_expression, result_expression, dataset)
source_expression – The field which will act as the key/lookup value for the destination. This will be evaluated in the current scope – generally the DataSet with which the Tablix is bound. The only difference from the previous lookup functions is that, here this is a VariantArray.
destination_expression – The field in which the source expression will be looked in. This field will belong to the dataset provided in the same function as the last parameter.
result_expression – The field we want to retrieve from the destination DataSet for the matching source_expression & destination_expression for each row.
Note, If multiple matches are found, the value from the first matching row will be returned for all the values in the source expression. And we can not use any aggregate functions in the result_expression.
Dataset – The Dataset in which we want to look the values into.
Returns – A VariantArray, or Nothing if there is no match.
Example
Continuing with the same example from the previous posts of Employees & Departments, suppose we want to display the name of the departments to which the Employee is related to, we can use the following expression -
Table Structure of Employees Table
ID | EmployeeID | DepartmentIDs |
1 | 1 | 1,2,3 |
2 | 2 | 2,3 |
Table Structure of Departments Table
ID | Name |
1 | D1 |
2 | D2 |
3 | D3 |
Required Output
ID | EmployeeID | Departments |
1 | 1 | D1,D2,D3 |
2 | 2 | D2,D3 |
To get the Departments, we can use the following expression in SSRS -
=Join(MultiLookup(Split(Fields!DepartmentIDs.Value,","),
Fields!ID.Value,Fields!Name.Value,"Departments")),
",")
Here, we have used 3 functions -
- Split() – To convert the comma separated DepartmentIDs into a value array.
- Multilookup() – To find the Name of departments for the matching ID.
- Join() – Prepare the comma separated string for the names returned by the Multilookup() as array.
Conclusion
So, with this we have covered all the 3 lookup functions provided in SSRS 2008 R2 and have learned how to make best use of them to fetch data from multiple datasets.