# Formula Levels

Logi Report categorizes formulas into six levels. These levels determine when Logi Report Engine executes the actual statements in your formulas. This topic introduces the Logi Report formula levels in detail.

Select the following links to view the different formula levels:

- Constant Level Formulas
- Record Level Pass One Formulas
- Record Level Pass Two Formulas
- Group Level Formulas
- Report Global and Global Formulas
- Page Level Formulas

Logi Report Engine makes several passes over the data to determine when it executes formulas. Even though an individual formula appears to be a certain level, if it references anything in the higher level, it also becomes the higher level formula.

## Constant Level Formulas

A constant level formula is a formula that does not refer to any DBFields, summaries, global variables, page level special fields (the value of the special field is ready at the time when Logi Report Engine generates the report), or other non-constant formulas. For example, `return CurrentDate()`

is a constant level formula.

Logi Report Engine executes constant level formulas before fetching data from the dataset, so if you place a constant level formula in the report header, Logi Report Engine executes it immediately as it encounters the formula.

## Record Level Pass One Formulas

A formula is a record level pass one formula if and only if the following are true:

- Refers to a DBField or any other formula which references a DBField.
- Does not refer to any page level special fields or formulas which refer to page level special fields.
- Does not refer to any global variable or any record level pass two formulas
- Does not refer to a summary field.

## Record Level Pass Two Formulas

A formula is a record level pass two formula if and only if the following are true:

- Refers to a DBField and a summary.
- Refers to any other record level pass two formula.

Logi Report Engine executes record level pass two formulas on each record of the dataset but only after the data has been grouped. For example, if you place a record level pass two formula in the banded header, the group is the entire report; if you place the formula in a group header such as Category, it is the value after all of the detail rows have been processed for the group. For example, you may want to display in a group footer the group name and total such as `return "Total for " + @CATEGORY + ": " + @Sum_Total;`

.

## Group Level Formulas

A formula is a group level formula when it refers to only summaries or other group level formulas. For example, you have two summaries, Sum_Total and Count_Detail, you could write a group level formula as: `return @Sum_Total / @Count_Detail`

, to display the average total sales on each line of the order.

Logi Report Engine executes group level formulas when breaking the group on which you define the summaries, which could be a group or the entire report for a report level summary in the banded header or banded footer.

## Report Global and Global Formulas

If a formula refers to a global variable or report global variable and does not refer to any page level special fields, there are several rules for when Logi Report Engine calculates the formula.

- If you place the formula in the report header or group header, Logi Report Engine calculates it when the group starts.
- If you place the formula in the page header, page footer, or detail panel, Logi Report Engine calculates it on each record after the data has been grouped the same as record level pass two formulas.
- If you place the formula in the report footer or group footer, Logi Report Engine calculates it when the group or report ends.

Logi Report Engine calculates report global and global formulas based on all records of the group or report. For example, if you calculate an accumulated total that you display in a report footer or group footer, it works as expected. For example, you may want to calculate a total margin such as:

`global number GrossProfit = 0;`

GrossProfit = GrossProfit + (@Price - @Cost);

return GrossProfit;

However, if you show a running total in the detail, you see the group or report total on every line rather than the expected running total. The solution is to convert the formula to a page level formula by adding a reference to any page level special fields.

You can use report global and global variables only in page reports.

## Page Level Formulas

Logi Report converts all the other formula types to a page level formula when the formula calls *next()*, *prev()*, *IsNoRecord()*, or references any of the page level special fields.

Logi Report Engine calculates page level formulas in the exact sequence of the panels as they display. Thus, if a page level formula includes any of the page level special fields, Logi Report Engine calculates it in the detail or group header and footer exactly in the order they are laid out. In the preceding example, if you want to do a running total on each detail, the formula expression is:

`PageNumber;`

global number GrossProfit = 0;

PageNumber;

GrossProfit = GrossProfit + (@Price - @Cost);

return GrossProfit;

PageNumber also controls the sequence of calculations around page breaks. When a formulas references PageNumber, Logi Report Engine performs the calculation before the page break; otherwise, it performs the calculation after the page break. This is important when you use formulas to control properties.