*Note: this article was written for version 2.2 and above*

Using a field type of Formula and Function from the Extension we can create complex calculations. Let’s look at different examples.

- MySQL Formula
- get_value()
- Functions
- JS Formula

**MySQL Formula**

This formula is built into the MySql query, which gives you the ability to use any mathematical MySql functions for calculations, as well as other functions and language operators. In the MySQL Formula field you can use any type of field.

**Simple calculations**

([36]+[54])/2

where 36 and 54 — id of numeric fields.

**Mathematical functions**

In the formula we can apply a mathematical function of MySql. For example, we need to round the calculation result to an integer:

FLOOR(([36]+[54])/2)

**The calculation formula with the condition**

To add a condition in the formula we need to use an **IF** statement.

if(expr1,expr2,expr3)

If * expr1* is

`TRUE`

(`expr1`

<> 0

and `expr1`

<> NULL

) then `IF()`

returns *; otherwise it returns*

`expr2`

*.*

`expr3`

`IF()`

returns a numeric or string value, depending on the context in which it is used.if([36]>0,([36]+[54])/2,0)

In this example, if the value 36 is greater than 0, then we do the calculation, otherwise, return zero.

If you need to return a string, such as a blank, or the entered text, then the string must be enclosed in single quotation marks, for example:

if([36]>0,([36]+[54])/2,'no data')

**Check the list choices in the IF statement**

It is very often necessary to make calculations only in the case if, for example the field “Project Status” has field type “Dropdown” and value “Completed”. In this case, the condition will be as follows:

if([11]=15,([36]+[54])/2,0)

where 11 is the ID for the Status field, and 15 is the ID of the value “Completed”.

Now let’s look at an example where we need to check multiple values of statues:

if([11] in (15,16,17),([36]+[54])/2,0)

where 15,16,17 are the ids of the choices values.

To check the value of the Boolean Field, use ‘true’ or ‘false’ example:

if([20]='true',([36]+[54])/2,0)

**Check the list choices with CASE**

The CASE statement is an analog of the switch statement in PHP. Its construction is as follows:

case when [11] =15 then ([36]+[54])/2) when [11] =16 then ([36]+[54])/3) end

**The calculation of the time difference**

Very often there is a need to create a calculation of time difference between Start Date and End Date of the Project or Tasks.

Dates are stored in a database in seconds, then calculate the number of days will be the following:

([300]-[159])/86400

where 300 is the ID of the field “End Date”, 159 — ID of the field “Start Date”, 86400 — the number of seconds in a day.

But as the date can be entered incorrectly or be missing altogether, then we need to do checks before the calculation. The correct formula would be as follows:

if([300]>0 and [159]>0 and [300]>[159],([300]-[159])/86400,0)

Note: you can use [TODAY] for current date.

**Merge several fields**

Using the CONCAT function, you can merge several fields into a string.

CONCAT([11],' - ',[36],' - ',id)

**Output date format using FROM_UNIXTIME**

FROM_UNIXTIME([12],'%Y-%m-%d')

where 12 is the identifier for the date field

**get_value()**

For fields of type List, you can set the values of each option, and with internal function get_value () you can use these values in the formula fields.

[36]+[54]+get_value([15])

where 15 is the identifier of a field of type List. If field 15 is not mandatory and can be empty, in mathematical expressions it is necessary to check the NULL result.

[36]+[54]+IFNULL(get_value([15]),0)

**Functions**

Functions help you to perform calculations using fields from other entities. To add a function in a formula, use brackets. Consider a simple example of using functions:

{12}+[5]

where 12 is the ID function and 5 the ID number field.

The following example will allow you to perform the calculation with several functions:

{12}+{13}

Also functions can be applied in conditions:

if([5]>0,{12},0)

if({1}={2},'<span style="color:green">In Stock</span>','NO')

**NULL in response **

If there are no records in the entity, the function will return NULL. When working with NULL, there are special rules that do not allow NULL to be used in mathematical expressions. In this regard, it is necessary to use the IFNULL check:

IFNULL({12},0)+[5]

**JS Formula**

Mathematical calculations occur directly in the form of adding/editing a record when entering data. The calculations take place using JavaScript, which allows you to use any mathematical functions and operators of JavaScript language.

**Simple calculations**

([36]+[54])/2

where 36 and 54 — id of numeric fields.

**Calculation by the formula with the condition**

([36]>5) ? ([36]+[54])/2:0

Custom Function

If you need more logic, then you can use the following example:

my_function(); function my_function() { my_value = [261]*[262]+get_value([260]) if( my_value>500) { return my_value*2 } else { return my_value }