Common Expressions for Reports (Visual Studio Report Designer)
Some expressions are commonly used in reports. These include expressions to change the appearance of data in a report, calculate totals, and change properties of report items. This topic describes some expressions that can be used for common tasks within a report.
Many expressions within a report contain functions. You can write expressions that use functions from the Microsoft.VisualBasic, System.Convert, and System.Math namespaces, or you can add references to other assemblies or custom code. You can also use classes from the Microsoft .NET Framework SDK. For more information, see the .NET Framework SDK Class Library documentation.
Functions
Most functions within a report are either Microsoft Visual Basic or built-in report functions. You can format data, apply logic, and access report metadata using these functions.
Visual Basic Functions
You can use Visual Basic functions to manipulate the data that is displayed in text boxes or that is used for parameters, properties, or other areas of the report. This section provides examples demonstrating a few of these functions. For more information about Visual Basic functions, see the Visual Basic documentation.
Date Functions
You can use Visual Basic functions to provide date information in your report.
The following expression contains the Today function, which provides the current date. This expression can be used in a text box to display the date on the report, or in a parameter to filter data based on the current date:
=Today()
The DateAdd function is useful for supplying a range of dates based on a single parameter. The following expression provides a date that is six months after the date from a parameter named StartDate:
=DateAdd(DateInterval.Month, 6, Parameters!StartDate.Value)
The following expression contains the Year function, which displays the year for a particular date. You can use this to group dates together or to display the year as a label for a set of dates. This expression provides the year for a given group of order dates:
=Year(Fields!OrderDate.Value)
The Month function and other functions can also be used to manipulate dates. For more information, see the Visual Basic documentation.
String Functions
You can use Visual Basic functions to manipulate strings in your report.
You can format dates and numbers within a string with the Format function. The following expression displays values of the StartDate and EndDate parameters in long date format:
=Format(Parameters!StartDate.Value, "D") & " through " & Format(Parameters!EndDate.Value, "D")
If the text box contains only a date or number, you should use the Format property of the text box to apply formatting rather than the Format function within the text box.
The Right, Len, and InStr functions are useful for returning a substring, for example, trimming DOMAIN\username to just the user name. The following expression returns a portion of a string to the right of a backslash (\) character from a parameter named User:
=Right(Parameters!User.Value, Len(Parameters!User.Value) - InStr(Parameters!User.Value, "\"))
The following expression results in the same value as the previous one, using members of the .NET Framework String class instead of Visual Basic functions:
=Parameters!User.Value.Substring(Parameters!User.Value.IndexOf("\")+1, Parameters!User.Value.Length-Parameters!User.Value.IndexOf("\")-1)
Decision Functions
You can use Visual Basic functions to evaluate an input value and return another value depending on the result.
The Iif function returns one of two values depending on whether the expression evaluated is true or not. The following expression uses the Iif function to return a Boolean value of True if the value of LineTotal exceeds 100. Otherwise it returns False:
=Iif(Fields!LineTotal.Value > 100, True, False)
The following expression uses multiple Iif functions (also known as "nested Iifs") to return one of three values depending on the value of PctComplete.
=Iif(Fields!PctComplete.Value >= .8, "Green", Iif(Fields!PctComplete.Value >= .5, "Amber", "Red"))
The following expression also returns one of three values based on the value of PctComplete, but uses the Switch function instead, which returns the value associated with the first expression in a series that evaluates to true:
=Switch(Fields!PctComplete.Value >= .8, "Green", Fields!PctComplete.Value >= .5, "Amber", Fields!PctComplete.Value < .5, "Red")
Report Functions
Reporting Services provides additional report functions that you can use to manipulate data within a report. This section provides examples for two of these functions. For more information about report functions and examples, see Built-in Functions for Reports (Visual Studio Report Designer).
The Sum function can total the values in a grouping or data region. This function can be useful in the header or footer of a table group. The following expression displays the sum of data in the Order grouping or data region:
=Sum(Fields!LineTotal.Value, "Order")
An expression containing the RowNumber function, when used in a text box within a data region, displays the row number for each instance of the text box in which the expression appears. This function can be useful to number rows in a table. It can also be useful for more complex tasks, like providing page breaks based on number of rows. For more information, see "Page Breaks" later in this topic.
The following expression displays the row number from the first row in the outermost data region to the last. The Nothing keyword indicates that the function will begin counting at the first row in the outermost data region. To begin counting within child data regions, use the name of the data region.
=RowNumber(Nothing)
Appearance of Report Data
You can use expressions to manipulate how data appears on a report. For example, you can display the values of two fields in a single text box, display information about the report, or affect how page breaks are inserted in the report.
Page Headers and Footers
When designing a report, you may want to display the name of the report and page number in the report footer. To do this, you can use the following expressions:
The following expression provides the name of the report and the time it was run. It can be placed in a text box in the report footer or in the body of the report. The time is formatted with the .NET Framework formatting string for short date:
=Globals.ReportName & ", dated " & Format(Globals.ExecutionTime, "d")
The following expression, placed in a text box in the footer of a report, provides page number and total pages in the report:
=Globals.PageNumber & " of " & Globals.TotalPages
You can also refer in the report header or footer to report items from the body of the report. The following examples describe how to display the first and last values from a page in the page header, similar to what you might find in a directory listing. The example assumes a data region that contains a text box named LastName.
The following expression, placed in a text box on the left side of the page header, provides the first value of the LastName text box on the page:
=First(ReportItems!LastName.Value)
The following expression, placed in a textbox on the right side of the page header, provides the last value of the LastName text box on the page:
=Last(ReportItems!LastName.Value)
You can apply aggregates to a report item reference in a page header or footer. (You cannot apply an aggregate to a report item reference in the report body, however.) The following example describes how to display a page total. The example assumes a data region that contains a text box named Cost.
The following expression, placed in the page header or footer, provides the sum of the values in the Cost text box for the page:
=Sum(ReportItems!Cost.Value)
Note
You can refer to only one report item per expression in a page header or footer.
Page Breaks
In some reports, you may want to place a page break at the end of a specified number of rows instead of, or in addition to, on groups or report items. To do this, create a group in a data region (typically a group immediately outside the detail), add a page break to the group, and then add a group expression to group by a specified number of rows.
The following expression, when placed in the group expression, assigns a number to each set of 25 rows. When a page break is defined for the group, this results in a page break every 25 rows.
=Int((RowNumber(Nothing)-1)/25)
Properties
Expressions are not only used to display data in text boxes. They can be used to change how properties are applied to report items. You can change style information for a report item, or alter its visibility.
Formatting
You can use expressions to vary the appearance of report items in a report.
The following expression, when used in the Color property of a text box, changes the color of the text depending on the value of the Profit field:
=Iif(Fields!Profit.Value < 0, "Red", "Black")
The following expression, when used in the BackgroundColor property of a report item in a data region, alternates the background color of each row between pale green and white:
=Iif(RowNumber(Nothing) Mod 2, "PaleGreen", "White")
Visibility
You can show and hide items in a report using the visibility properties for the report item. In a data region such as a table, you can initially hide detail rows based on the value in an expression.
The following expression, when used for initial visibility of detail rows in a group, shows the detail rows for all sales exceeding 90 percent in the PctQuota field:
=Iif(Fields!PctQuota.Value>.9, False, True)
Report Parameters
You can use expressions in a parameter to vary the default value for the parameter. For example, you could use a parameter to filter data to a particular user based on the user ID that is used to run the report.
The following expression, when used as the default value for a parameter, collects the user ID of the person running the report:
=User!UserID
Custom Code
You can use custom code within a report. Custom code is either embedded within a report or stored in a custom assembly which is used in the report. For more information about custom code, see Adding Custom Code to a Report (Visual Studio Report Designer).
The following example calls the embedded code method ToUSD, which converts the StandardCost field value to a dollar value:
=Code.ToUSD(Fields!StandardCost.Value)
See Also
Concepts
Using Expressions in a Report (Visual Studio Report Designer)
Defining Report Parameters in a Report (Visual Studio Report Designer)
Adding Page Headers and Page Footers to a Report (Visual Studio Report Designer)
Adding Style and Formatting to a Report (Visual Studio Report Designer)
Adding Interactivity, Visibility, and Navigation to a Report (Visual Studio Report Designer)