Formulas can be categorized into 6 levels. These levels determine when the actual statements in your formula will be executed. JReport Engine will make several passes over the data to determine when formulas are executed. Even though an individual formula appears to be a certain level, if it references anything in the higher level then it also becomes the higher level.
return CurrentDate()
is a constant formula. JReport will execute these formulas before fetching data from the dataset, so if you place it in the report header, it will be executed immediately as it is encountered.Record level pass one formulas are executed on each record immediately after the record is fetched from the dataset. They are often used in the detail panel of banded reports and tables such as calculating a value based on other DBFields such as return @PRICE - @COST
.
Record level pass two formulas are executed on each record of the dataset but only after the data has been grouped. For example, if a record level pass two formula is placed in the banded header, the group is considered to be the entire report. If it is placed in a group header such as Category, it will be the value after all of the detail rows have been processed for the group. For example, you might want to print in a group footer the group name and total such as return "Total for " + @CATEGORY + ": " + @Sum_Total;
.
return @Sum_Total / @Count_Detail
to print the average total sales on each line of the order.
These formulas are executed when breaking the group on which the summaries are defined which could be a group or the entire report for a report level summary in the banded header or banded footer.
These formulas will be calculated based on all of the records of the group or report. For example, if you are calculating an accumulated total that you display in a report footer or group footer, then it will work as expected. For example, you may want to calculate a total margin such as:
global number GrossProfit = 0;
GrosssProfit = GrossProfit + (@Price - @Cost);
return GrossProfit;
However, if you are showing a running total in the detail, you will 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 (see below).
The page level formulas are calculated in the exact sequence of the panels as they are laid out in JReport Engine. Thus if a page level formula includes any of the page level special fields, it will be calculated in the detail or group header and footer exactly in the order they are laid out. In the above example, to do a running total on each detail the code is as follows:
global number GrossProfit = 0;
PageNumber;
GrosssProfit = GrossProfit + (@Price - @Cost);
return GrossProfit;
PageNumber also controls the sequence of calculations around page breaks. If PageNumber is referenced, the calculation will be done before the page break. If PageNumber is not referenced, the calculation will be done after the page break. This is important when using formulas for setting properties.