Inserting a computed field
A computed field displays the result of an expression rather than stored data. For example, a database stores the prices of order items and the quantities that were ordered. To display the extended prices, specify the following expression to calculate the values:
row["pricequote"] * row["quantity"]
Table 9‑4 lists other examples of when to use computed fields and the types of expressions that you can specify.
Table 9‑4 Examples of expressions in computed fields
Uses for
computed fields
Examples of expressions
Display data that concatenates values from multiple fields
The following expression displays a customer’s first and last names, which the data source stores in two fields:
row["firstname"] + " " + row["lastname"]
The following expression displays a full address by concatenating values from four fields in the data source:
row["address"] +", " + row["city"] + ", " + row["state"] + " " + row["postalcode"]
Display data using a JavaScript or BIRT function
The following expression uses the JavaScript Date object to return the current date:
new Date().toLocaleDateString()
 
The following expression uses the BIRT BirtDateTime.diffDay( ) function to return the number of days between two dates:
BirtDateTime.diffDay(row["orderdate"], row["shippeddate"])
Display data that is calculated from multiple fields
The following expression calculates a customer’s available credit:
row["creditlimit"] - row["balance"]
JavaScript is a case-sensitive language. You must type keywords, function names, and any other identifiers with the correct capitalization. For example, type the Date( ) function as Date( ), not date( ) or DATE( ). If you need help constructing expressions using the correct syntax, choose objects, functions, and operators from the lower part of the expression builder. For more information about writing expressions or using the expression builder, see Writing expressions.
You can create a computed field using either of the following techniques:
*Define the computed field in the data set.
*Define the computed field in the report layout.
The first technique is preferable because:
*You can test the results of the calculation by choosing Preview Results in the data set editor.
*The computed field is available to any table, list, or chart that uses the data set. It appears in the list of fields for that data set.
*BIRT Report Designer processes the computed values once, rather than multiple times, if the same computed field is used in multiple places in the report.
To create a computed field in the report layout, drag a data element from the palette, and drop it in the desired location. Then, in New Data Binding, create a column binding that defines the expression that returns the computed values. Figure 9‑17 shows an example of a column binding that defines an expression, which uses the BirtDateTime.diffDay( ) function.
Figure 9‑17 A column binding that defines an expression
If you insert the data element directly on the page or in a grid, and you want to write an expression that refers to a data set field, first bind the data element to the appropriate data set. On the other hand, if you insert the data element in a table or a list, the data element has access to the data set bound to the table or list.