The formula functionality allows you to create calculated tags using simple mathematics and functions. They can be used to define derived quantities, such as differential pressures, temperature deltas, total flows, KPIs, etc.

A more general overview on Tags can be found here.

The formula tags can be found in the tag builder menu.

### Formula Submenu

**Formula-field: **In this field you can enter your desired formula using variables. Any entered variable will show up as a "Map Variable"-field in the section below.

Next to variables the field also accepts pre-defined functions you can use, see "Functions" below for more details.

**Note:** You can use any variable-name if it has not been assigned to a predefined function.

**Map Variable: **As soon as a variable is entered in the formula-field, you can map the variable to any existing tag in TrendMiner. The mapping-field allows you to quickly select a tag from your active list or to search for any tag.

Currently a maximum of 10 variables can be mapped.

**Timeshifts:** This checkbox allows you to include timeshifts for any mapped value. The default behaviour of the formula computation is to take values at the same timestamp for all mapped values.

### Formula Functions

You can enter certain keywords in the formula-field that act as a predefined function. Click the "?"-button to open the documentation of the built-in functions. Here you can see all available functions and learn how to use them.

**Example 1: **For this example, you want to return a different calculation depending on the temperature. You want to return the measurement with a correction-factor of 0.789 if the temperature is below 30 degC and a factor of 1.345 when the temperature is above. For this case you could make use of the "if" function.

From the help-menu you can see that the syntax for the "if" function is:

if(condition,value_if_true,value_if_false)

Here we have to define our condition (temperature above 30 degC) and what it should return if this condition is true (factor 1.345) or false (factor 0.789). The formula we need to enter would become:

if(temp>30, 1.345 * tagA, 0.789 * tagA)

Using this formula we could then map the temperature-measurement to the variable "temp" and the measurement we want to correct for to "tagA".

**Example 2:** In this example, you want to calculate a sum if at least 1 out of 3 valves is open. If all of them are closed, we want to display 0. We will use a combination of an "if" and "or" function to accomplish this.

From the help-menu you can see that the syntax for the "or"-function is:

or(expression1,expression2,…,expression10)

Note that the "or"-function returns TRUE if one of the expressions is evaluated as true. This is what we can feed directly into the "if" function as condition:

if(or(valve1="open", valve2="open", valve3=1), A+B, 0)

This formula will return a value of zero if all valves are closed and returns the calculation of the tags mapped to "A+B" if at least one of the valves is open.

**Note:** For numbers with scientific notation, you need to use the "*10^(-3)" notation as the letter "E" for the "E(-3)" is already taken for the variable mapping.

#### Formula Output

Output of a formula tag is always an analog (linearly interpolated) tag.

#### Tag Type & Input conversions

Discrete, Digital and Sting tags will first be converted to linear analog values when used in mathematical operations.

#### Computation

The computation of the formula tags is done on the basis of all existing indexation points of the mapped variables. An interpolated value between two points is used when no indexation point exists for other mapped variables at that specific timestamp.

When looking at smaller time windows (smaller than 300 times the index resolution), TrendMiner directly fetches data from the historian instead of using indexed values. In this scenario an ad hoc calculation is made based on the visualized data instead of the indexed values, which might be more fine-grained than the indexed values.

**Note:** Other TrendMiner functionalities always use the stored index values of the formula tags for further computations.

### Restrictions

The general restrictions as mentioned in the Tag Builder overview document are applicable to formula tags.

#### Variables

A maximum of 10 variables can be included for mapping of each formula.

#### Input and Output

- Discrete, Digital and Sting tags will first be converted to linear analog values when used in mathematical operations.
- Output of a formula tag is always an analog (linear interpolated) tag.