A Brief Introduction to Expressions in SQL Server Reporting Services

This article shows how you can use expressions in SSRS reports to generate dynamic content based on the data within your report queries.

The intent of this article is to demonstrate the possible applications of expressions in SQL Server Reporting Services (SSRS) reports. You should already have a basic working knowledge of SSRS before continuing.
 

Expressions in SSRS

 
Expressions are basically little snippets of VB.NET code that return a value. The snippet can calculate a return value based on other values stored in parameters, datasets, or combinations thereof. You can also make use of several built-in values and functions to perform more complex calculations.
 
For the purposes of this article, we will use a default installation of SQL Server 2005 and  Reporting Services.  We will start with a simple report that retrieves data from the AdventureWorks sample database and then make various modifications to it in order to demonstrate the various possibilities.
 
Here is the query that the report is based on: 
  1. SELECT  
  2.  Person.StateProvince.Name,  
  3.  COUNT(Person.StateProvince.StateProvinceCode) AS StateCount  
  4. FROM  
  5.  Sales.Customer   
  6.  INNER JOIN Sales.CustomerAddress ON Sales.Customer.CustomerID = Sales.CustomerAddress.CustomerID   
  7.  INNER JOIN Person.Address ON Sales.CustomerAddress.AddressID = Person.Address.AddressID   
  8.  INNER JOIN Person.StateProvince ON Person.Address.StateProvinceID = Person.StateProvince.StateProvinceID   
  9.   AND Person.Address.StateProvinceID = Person.StateProvince.StateProvinceID  
  10. GROUP BY   
  11.  Person.StateProvince.Name  
  12. ORDER BY   
  13.  Person.StateProvince.Name,   
  14.  StateCount  
Here is the report layout
 
Expressions in SSRS 
 
And here's a portion of what it looks like in Report Manager
 
Expressions in SSRS 
 
For the first example of an expression, we'll change the colors of those count values that are below a certain threshold.  In this case, we want the color of the text to red for those rows with a count less than 25.  To do this, we need to navigate the report's layout   in Visual Studio, click on the table cell that renders the count field, and select Expression from the Color property dropdown menu: 
 
Expressions in SSRS 
 
The "Edit Expression" dialog appears:
 
Expressions in SSRS 
 
The expression is currently set to the default color value "Black".  We need to change this to a VB.NET snippet that returns one of two strings -- "Red" for values less than 25, or "Black" for other values.  Here is the expression that will do that:
 
=IIf(Fields!StateCount.Value < 25, "Red", "Black") 
 
It uses the IIf() function to determine the name of the color to be used, based on the value of the StateCount field in the dataset. After redeploying the report, it now looks like this:
 
Expressions in SSRS 
 
But what if you want to have more than two colors? For example, you may also want to show all values over 1000 with a color other than red or black (in this case, we'll use blue). You have some options here. One would be to use nested IIf() calls, as in: =IIf(Fields!StateCount.Value < 25, "Red", IIf(Fields!StateCount.Value > 1000, "Blue", "Black")) The function is evaluated as follows: if the count is < 25, then "Red" is returned, otherwise the second IIf() is evaluated and it checks if the count is > 1000. If so, it returns "Blue", otherwise it returns "Black", and your report looks like this: 
 
Expressions in SSRS 
 
But what if you want even more colors?  Well, you can continue to nest IIf() statements but your expression code will quickly become more difficult to read and maintain.  An alternative (assuming you can modify the report query) would be to add another column to your select query that returns the desired color strings as in:
  1. SELECT  
  2.  Person.StateProvince.Name,  COUNT(Person.StateProvince.StateProvinceCode) AS StateCount,  
  3.       CountColor = CASE  
  4.        WHEN COUNT(Person.StateProvince.StateProvinceCode) < 25   
  5.    THEN 'Red'  
  6.        WHEN COUNT(Person.StateProvince.StateProvinceCode) < 100   
  7.    THEN 'OliveDrab'  
  8.        WHEN COUNT(Person.StateProvince.StateProvinceCode) < 500   
  9.    THEN 'Teal'  
  10.        WHEN COUNT(Person.StateProvince.StateProvinceCode) < 1000   
  11.    THEN 'Blue'  
  12.        ELSE 'Black'  
  13.       END  
You can then remove the IIf() statements altogether and change the Color property expression to: 
 
=Fields!CountColor.Value
 
And the report now looks like this: 
 
Expressions in SSRS 
 
These examples only scratch the surface of the potential that expressions offer in generating dynamic report content. Take a moment to browse through the Properties list for a table cell (or any other object in the Toolbox). Most properties give you the option of entering an expression in addition to any predefined values. This means that you can manipulate any of them programmatically, just as easily as in the color example shown here. Here are some additional examples of what expressions can do for you: 
  1. Hide columns in tables, or hide groups in tables and matrices by using an expression to set the Hidden property of the column or group.
  2. Create entirely new columns by using an expression to return a value based on existing values in the recordset. For example, lets say you have inherited a report that shows 3 columns labelled A, B and C, using a stored procedure to generate the data. The end user wants a fourth column to be added that shows the average value from A, B and C. You could add that new column to the select statement in the stored procedure, but what if you aren't allowed to modify the procedure?  Instead, you can add that 4th column to the report, and set its value to an expression that returns (A+B+C)/3.  You've added the desired column and you've done it without modifying the stored procedure.
  3. Make use of built-in functions, such as Globals.ExecutionTime (to return the time & date that the report was executed) and Globals.PageNumber (to determine the current page number) to add meaningful tracking information to your report's headers & footers.
  4. Perform your own aggregation and grouping using the built-in functions such as Sum(), Avg(), etc. These functions allow you to add aggregation to established reports where (as in #2 above) you may not be able to modify the queries that generate the reports' data.
  5. Manipulate the data values using VB.NET string functions such as InStr, Replace, FormatNumber, etc. to get the data to appear exactly how you want it to.
Try experimenting with the various functions that appear in the Edit Expression dialog.  Remember that by changing the output of your report, you're not changing the underlying data in your database, you're only changing how it looks when rendered in the report.