A Simple Formula Example
Consider a table with a two standard text field named "First Name" , "Last Name"
Each Column Contains Names. you can create a formula field with the formula concat("Mr.",field("First Name"),field("Last Name")), the resulting table would appear as follows:
First Name | Last Name | Full Name |
Ahmed | Abdullah | Mr. Ahmed Abdullah |
Khalid | Faisal | Mr. Khalid Faisal |
In this scenario, the formula field cells are calculated using the text field cell for each row.
Explaining a simple formula
Let's dissect the formula concat("Mr.",field("First Name"),field("Last Name")) to understand its components:
concat: Concat is one of many formula functions available, which joins together all the inputs into one piece of text.
(: An opening parenthesis indicates the beginning of input for a formula function.
'Mr.': This is the first input to concat, simply the text "Mr," enclosed in quotes.
,: As multiple inputs are provided to concat, each input is separated by a comma.
field('First Name'): This is the second input to concat, referencing the field in the same table named "First Name" For each cell in the formula field, this reference will be substituted with the value in the "First Name" for that row.
,: Again as multiple inputs are provided to concat, each input is separated by a comma.
field('Second Name'): This is the third and final input to concat, referencing the field in the same table named "Second Name" For each cell in the formula field, this reference will be substituted with the value in the "Second Name" for that row.
): A closing parenthesis indicates the end of the inputs to the concat function.
What is a function?
A function in a formula takes a specific number of inputs, depending on its type, performs calculations using those inputs, and produces an output.
Functions sometimes accept only certain types of inputs. For example, the datetime_format function only accepts two inputs: the first must be a date, and the second must be text.
All available functions are displayed in the expanded formula edit box, accessible when editing a formula field.
Using numbers in formulas
Formulas can perform numerical calculations using standard math operators such as +, -, *, and /. You can use whole numbers or decimal numbers directly in your formula, for example: (field('number field') + 10.005)/10.
Invalid Number Error
If you encounter an "Invalid Number" error in a formula cell, it means that the formula for that row attempted one of the following invalid operations:
Dividing a number by zero.
Trying to convert text to a number using the tonumber function and failing because the text wasn't a valid number.
Calculating a number larger than 10^50, the maximum allowed value.
Conditional calculations
If conditional calculations are necessary, you can use the if function and comparison operators. For instance, the formula IF(day(field('some date')) = 1, true, false) calculates whether a date field represents the first day of a month.
You can compare fields and sub-formulas using operators such as >, >=, <=, <, =, and !=.
Using Dates
To create a constant date within a formula, utilize the todate function, like so: todate('2020-01-01 10:20:30', 'YYYY-MM-DD HH:MI:SS'). The first argument is the desired date in text form, and the second is the format of the date text.
Using Date Intervals
Subtracting two dates yields the time difference between them: field('date a') - field('date b'). The date_interval function allows you to create intervals within the formula for manipulation.
For calculating a new date based on a date/time interval, employ the date_interval function like this: field('my date column') - date_interval('1 year').
