SQL Server 2008 R2: Reporting Services – Look Up, Look Down, Look All Around (Part I)

March 19, 2010

SQL Server 2008 R2 Reporting Services is loaded with features for the report author. In today’s post and two posts that will appear next week, I’ll explore the lookup functions which enable you to combine data from two datasets in a single data region which not possible in earlier versions of Reporting Services. If all of the data in a report comes from a single data source, then it’s probably easier to create a single dataset by using a SQL query that joins the data from multiple tables. However, there might be situations when you can’t write the query to join the data in advance. For example, you might have some data coming from a relational source and other data in a CSV file. Or perhaps you’re taking advantage of shared datasets in Reporting Services 2008 R2 and need a way to link together data from two shared datasets in one data region. That’s where the new Reporting Services lookup functions come in handy.

There are three lookup functions available:

My focus today is on the Lookup function. This function is in effect a join between two datasets where there is a one-to-one relationship between the source and destination. Let’s say that I want to produce a report of sales by state, but the dataset that I have for sales provides the two-letter code for state. Let’s say also that I find another dataset that provides a list of states and the two-letter code for each state. Now I have two datasets available, shown below in Figure 1, for my report.

Figure 1

Next, I create a simple table, add the SalesAmount field from Dataset1 to the table, and add an expression in the first column. The layout and a portion of the report are shown in Figure 2.

Figure 2

The expression uses the Lookup function and looks like this:

=Lookup(Fields!StateProvinceCode.Value,Fields!StateProvinceCode.Value,Fields!Name.Value,"Dataset2")

The Lookup function takes four arguments:

  1. The first argument, Fields!StateProvinceCode, is a field (or expression based on a field) from the dataset that is bound to the data region, which is Dataset1 in this example.
  2. The second argument is a field (or expression) from the dataset that you want to use for the lookup. In this example, the second dataset also contains a field called StateProvinceCode, but it’s okay if the field names are different in each dataset. They should, of course, contain data that matches up and they must have the same data type. You can think of the first and second arguments as analogous to two columns that you want to use to define a join between two tables.
  3. The third argument, which is Fields!Name.Value in this example, is the field (or expression) in the second dataset that you want to return as the function result.
  4. The fourth and final argument is the name of the second dataset which must be enclosed in quotes and is case-sensitive.

If you need to match two fields in the first dataset to two fields in the second dataset, that’s okay. You simply create an expression to concatenate the pair of fields and use that expression in the argument in the Lookup function. Your expression cannot include report or group variables, however, nor can it include another lookup function.

Figure 3 shows another way to visualize what the Lookup function does.

Figure 3

Note that Reporting Services processes the Lookup function on the scope of the expression, which is in this case is the detail rows of the table which correspond to the rows of Dataset1. Using the Lookup function, Reporting Services uses a single value for the current row in the first dataset to find the first match in the second dataset and returns a single value. By contrast, the other two lookup functions, MultiLookup and LookupSet work with arrays. Next week, I’ll continue with Part II and Part III to explain how to use these functions.

This content is published under the Attribution-Noncommercial-No Derivative Works 3.0 Unported license.

7 Responses to “SQL Server 2008 R2: Reporting Services – Look Up, Look Down, Look All Around (Part I)”

  1. Very well written! This is quite helpful, thank you for taking the time to make this available.

  2. Thank you for the information. I tried it and it worked great. This is something that we had to do in SSRS 2005 with custom code and timing the dataset that loaded a custom code variable in memory just to use custom code to retrieve the info from that same cache in the report.

    I think one thing that is lacking is that one can not add a calculated field to a dataset and call the same lookup function. When one does that then an error occurs stating that it can’t be done for scope purposes.

    Do you know of a way to lookup information from another database and include it in a calculate field on a source dataset by a key code? If it is done at the data set level and not the table executing then one could do cool things like group/filter/sort by the new field.

  3. Hi Marc,

    I don’t have an idea immediately. I’m in travel mode before the long weekend, but I’ll give it some thought next week. If I come up with a solution, I’ll let you know.

  4. Hi Stacia,

    I tried to use the Lookup-Function in exactly this way.
    I my case the Expression-Builder stops me from editing.
    The table you use in Figure 2 to insert the expression, is connected to only one dataset (in your case Dataset1) as you can see in the Table/Tablix properties.
    Fields used in the expression Builder can only be selected from this dataset (Dataset1). All other Fields (e.g. from the Dataset2) are not available and produce runtime errors( e.g. in the Preview). Is this because of the situation: the datasets are build on different Sources ?
    There is something wrong in my scenario, because otherwise the Lookup-Function ( only for one Dataset ) makes no sense.

    Any idea ?

    Stefan

  5. Hi Stefan,

    You are correct that the expression builder will let you select fields only from the dataset that is connected to the data region. You have to know the name of the field from the other dataset in order to use it in the Lookup function. I have successfully used this function with datasets that use different sources – which is the only time this technique makes sense to me because if they used the same source, I could use a join operation in the dataset query which would be preferable for better performance when working with large data volumes.

    If you’re running into an error in Preview mode, then you might not have the right data type or something doesn’t match exactly between the fields that you are using to compare with one another in the first and second arguments of the Lookup function.

  6. very well, wait for part 2, 3 … :)

  7. Hi Annie – Part 2 and 3 are already posted. You can find them here:

    http://blog.datainspirations.com/2010/03/29/sql-server-2008-r2-reporting-services-look-up-part-ii-2/
    http://blog.datainspirations.com/2010/04/07/sql-server-2008-r2-reporting-services-look-up-part-iii/

Leave a Reply