    This documentation is valid for:

Formulas are expressions that contain a calculation which, once evaluated, returns a certain value. They are like functions.

In addition, they are a key way to share knowledge, when used to define the way everytime an attribute is going to be calculated. In such a case, the attribute is not stored in database (unless otherwise stated). Those special kinds of formulas are called "global formulas" or "formula attributes".

### What is a Formula

An expression that once evaluated, returns a certain value. For example:

`&today.Year() - PersonBirthDate.Year()`

where the formula calculates the person age, from: &today system variable, PersonBirthDate attribute, and the Year method. Or:

`Sum( InvoiceTotal, InvoiceDate >= &fromDate )`

where a sum of the values of the InvoiceTotal attribute is going to be triggered, for all records of the Invoice table whose InvoiceDate is bigger or equal than &fromDate variable value.

Thus, a formula is a calculation based on other attributes, constants and/or functions or methods

When an attribute or variable value can be calculated in this way, it can be associated to a formula. In addition, when this assignment is done at attribute definition level (that is, for the attribute, inside the transaction structure where it is specified), the attribute itself is known as "formula attribute" or abbreviated "formula".

There are two ways to define formulas:

1. Global Formulas (Global definition, at the Knowledge Base level)
2. Local / Inline Formulas (Local definition, within objects code)

### Classification of Formulas by navigation type

Formulas can be classified into three groups, depending on the type of calculation needed. This classification is valid either for global or local formulas.

• Horizontal Formulas:  Allow defining many kind of expressions (such as arithmetic, among others) involving a single record and it's associated in an extended table.
• Aggregate Formulas:  Allow defining some calculations or searches involving many records of a table (and it's associated by means of the extended table).
• Compound Formulas: Several horizontal and/or aggregate expressions combined.

All these kind of formulas may have trigger conditions.

Note: You don't need to indicate when defining a formula in GeneXus, if it belongs to one classification or another. This is only an external classification.

### Base table of a Formula

The table that is navigated in order to evaluate the formula is known as its base table. Depending on the kind of formula, the evaluation will consider only one record (horizontal formula) or many (aggregate formula). In the former (horizontal), the context in what the formula is, will affect the base table determination. In the latter, the context will affect the filters that will be applied in order to retrieve the meaningful records.

### Optimized generated code

Defining formulas it's even better than writing procedures and invoking them.

When you define a formula, GeneXus has the knowledge of its definition and is able to generate optimized sentences by combining the formula query with the query in which the formula is present.

On the other hand, if you define a procedure and you invoke it, the procedure code is not "visible" from the invoker, and GeneXus can't combine the knowledge and generate the most optimized code.