Working with Calculated Fields

Another feature from the Field List window is the ability to create new calculated fields from fields already provided. A calculated field is basically a new column whose result will be obtained by manipulating (addition, division, multiplication, average, or some other mathematical operation) values from two or more available fields.

For example, if in the Field List window a field named UnitPrice is provided, representing the price of a single product in dollars, and a second field named QuantitySold is also provided, which stores the number of units of the product sold in a month, a new calculated field can be created using these two named MonthlyRevenue, which will be obtained by multiplying UnitPrice x QuantitySold.

To create a new calculated field in a template, select the Add Calculated Field option from the menu that appears when one of the fields from the Field List window is right-clicked. The other command (Edit Calculated Field) allows previously created calculated fields to be edited.

 

 

When Add Calculated Field is clicked, a new field called CalculatedField1 (by default) is shown in the list of fields and is identified by the icon: .

 

 

To define the mathematical expression to be applied to generate the value of the calculated field, right-click the new field and click Edit Expression to use the Expression Editor (see figures below).

 

 

 

Pre-defined functions, operations (multiplication, subtraction, etc.), fields in the report, and other elements can be used to create expressions that give origin to the values of this new column created through the calculated fields.

Another application would be a calculated field that displays only part of what is normally displayed in a column. This is useful for situations in which the code used in the system is very long and you want it to be displayed partially. Through the Substring function, you can indicate the column containing the string to be used as a base and the position of the beginning and end of the desired part. This part will be extracted to the calculated field that, when linked to a control, displays only the desired portion of the code.

One important aspect of creating expressions is to be sure the syntax is valid. For example, all referenced data fields in an expression should be bracketed off, as in [UnitPrice]. There are other formatting restrictions that should be observed. For example, constants representing dates should use hashes (#) so that the expressions are valid, as in: [DateRequested] > # 07/06/2011#.