Sub topic 10
Custom Summary Formulas
Custom summary formulas let you calculate new values from the summaries already in a report. You can compute ratios, averages, and changes between groups without creating a field on the object. This chapter assumes you can build a summary report and a matrix report, and that you know how groupings and summary fields (sum, average, minimum, maximum, record count) work.
What a custom summary formula is
A custom summary formula is a calculation that runs on the grouped results of a summary, matrix, or joined report. It does not run on individual rows. It takes values that Salesforce has already summarized, such as AMOUNT:SUM or RowCount, and combines them with arithmetic, logical functions, or group-comparison functions.
Compare this with a formula field on an object. A formula field is evaluated once per record. A custom summary formula is evaluated once per group, or once per cell in a matrix. That difference explains most of the behavior and most of the errors you will see.
| Feature | Formula field (object) | Custom summary formula (report) |
|---|---|---|
| Evaluated | Per record | Per group, subtotal, or grand total |
| Stored on the object | Yes | No, it lives only in the report |
| Can use summarized values | No | Yes |
| Available in other reports | Yes | No |
| Can compare to other groups | No | Yes, with `PREVGROUPVAL` and `PARENTGROUPVAL` |
Where you can create one
Custom summary formulas are available in summary, matrix, and joined reports. A tabular report has no groupings, so it has nothing to summarize. In a joined report, each formula belongs to a single block.
In Lightning Experience, open the report in the report builder. In the Fields pane, find Add Summary Formula (in the dropdown next to the Fields heading, or on the Summary Formulas folder). Then complete these steps:
- Enter a column name. This is the label that appears in the report.
- Choose a format: Number, Currency, or Percent.
- Choose the decimal places.
- Choose where the formula is displayed. You can show it at all summary levels, or only at specific grouping levels. In a matrix, you can pick the levels for rows and columns.
- Write the formula in the editor, using the field picker for summary fields and functions.
- Select Check Syntax, then save.
The "where displayed" setting matters. A formula that makes sense at the Stage level may be meaningless at the grand total. Choosing the right level avoids confusing numbers.
Referencing summaries
Formulas refer to summaries by a field token followed by a colon and the summary type. The most common tokens are:
AMOUNT:SUM,AMOUNT:AVG,AMOUNT:MIN,AMOUNT:MAXfor a summarized field. These only appear in the picker if you have already added that summary to the report.RowCountfor the number of records in the group.FIELD_NAME:SUMstyle tokens for other fields, shown in the picker.
You can only use summaries that exist in the report. If AMOUNT:AVG is not in the picker, add the Average summary to the Amount column first, then return to the formula editor.
Example: average amount per group
The first formula divides the summed Amount by the number of records in each group.
AMOUNT:SUM / RowCountIn a report grouped by Stage, each Stage row shows its own total divided by its own record count. At the grand total, the formula uses the grand total of Amount divided by the total record count. This is a true weighted average across all records.
You may wonder why you would not simply use AMOUNT:AVG. The built-in average is fine when it is available. The formula version is useful when you want to divide by something else, such as a different count, or when you need the result inside a larger expression, such as a ratio of two averages.
A key point is that averaging averages gives the wrong answer. If you show the average of the Stage averages at the grand total, small groups count as much as large ones. Calculating SUM / RowCount at each level avoids this problem, because the sums and counts roll up correctly.
Ratios and percentages
Ratios are the most common use. Suppose a summary report has two summaries: the sum of Amount, and the sum of Amount for won deals. You can show a win rate by Owner:
WON_AMOUNT:SUM / AMOUNT:SUMHere WON_AMOUNT stands for a custom or standard field available in your report type. Set the format to Percent. The formula editor then displays the result as a percentage.
Guard against division by zero. If a group has no records or a zero total, the formula produces an error value and the cell appears blank or with a dash. Wrap the division in an IF:
IF(AMOUNT:SUM = 0, 0, WON_AMOUNT:SUM / AMOUNT:SUM)The IF function works like the formula-field version: a condition, a value if true, and a value if false.
Comparing groups: PREVGROUPVAL
PREVGROUPVAL returns the value of a summary for the previous group at a chosen grouping level. It is the standard way to show change over time.
AMOUNT:SUM - PREVGROUPVAL(AMOUNT:SUM, CLOSE_DATE)In a report grouped by Close Date (for example, by Calendar Month), each row shows its summed Amount minus the summed Amount of the group before it. The second argument names the grouping to look across. Salesforce looks at the previous group at that level in sort order.
Remember these details:
- The first group has no previous group. The formula returns zero or blank for the previous value, so the first row's change equals its full amount. Check your first row before trusting the result.
- "Previous" depends on sort order. If you sort groups by Amount descending instead of by date, the previous group is the one ranked above, not the earlier month.
- The grouping you name must exist in the report. If you name a grouping that is not present, Check Syntax reports an error.
You can express the change as a percentage of the prior period:
IF(PREVGROUPVAL(AMOUNT:SUM, CLOSE_DATE) = 0, 0,
(AMOUNT:SUM - PREVGROUPVAL(AMOUNT:SUM, CLOSE_DATE)) / PREVGROUPVAL(AMOUNT:SUM, CLOSE_DATE))Comparing to a parent: PARENTGROUPVAL
PARENTGROUPVAL returns the value of a summary at a higher grouping level. Use it to compute a share of the parent. In a report grouped by Stage and then by Owner, this shows each Owner's share of their Stage:
AMOUNT:SUM / PARENTGROUPVAL(AMOUNT:SUM, STAGE_NAME)Set the format to Percent. The values within each Stage add up to 100%. In a matrix, the same function lets you compute each cell as a share of its row or column total, depending on which grouping you name.
Formulas in matrix reports
In a matrix report, a formula is calculated for each cell where a row group and a column group intersect, and also for the row totals, column totals, and grand total. When you choose where the formula is displayed, you decide which of these appear.
Consider a matrix with Stage as rows and Close Year as columns. The formula AMOUNT:SUM / RowCount gives the average deal size in each Stage and year cell. Each total uses its own sum and count, so the totals are correct weighted averages rather than averages of the cells.
The same numbers can be reproduced in SOQL, which helps when you need to verify a result. A SOQL aggregate query returns the sum and the count for each group, and you divide them yourself:
SELECT StageName, SUM(Amount) total, COUNT(Id) cnt
FROM Opportunity
GROUP BY StageNameDividing total by cnt for each row gives the same value as the report formula for that Stage.
Limits and common errors
Know these constraints before you design a report around formulas.
- Formula count. A report can hold a limited number of custom summary formulas. Salesforce documents a cap of 10 per report. Check the current limit for your edition.
- No row-level data. You cannot reference a plain field value from a single record. Only summaries (
:SUM,:AVG,:MIN,:MAX) andRowCountare allowed. - Summary must exist. A token only works if the report already includes that summary. Add it first.
- Formula size. Formulas have a character limit, so keep expressions short. Move repeated logic into a separate formula and reference the result if needed.
- Not filterable like fields. You can use a formula in a report's summary-level filter in some cases, but you cannot use it in a row-level filter. Row filters run before grouping, and the formula does not exist yet at that point.
- Charts. A formula can usually serve as the value axis of a chart, but only for the displayed levels. A formula shown at the wrong level may produce an empty or misleading chart.
- Dashboards. Dashboard components read the report's summaries. If the formula depends on
PREVGROUPVAL, sorting changes can change the displayed values.
Debugging checklist
When a formula gives an unexpected number, check these items in order:
- Does the formula appear at the level you are looking at? The display setting may hide it at other levels.
- Does each summary in the formula exist in the report?
- Is the group sort order what you assumed, especially with
PREVGROUPVAL? - Could a denominator be zero in some groups?
- Do the report filters exclude records you expected? Formulas use only the filtered data.
- Does the SOQL aggregate for the same groups match? A mismatch usually points to a filter or sharing difference, since reports respect the running user's access.
How the pieces fit
The order explains the rules above. Filters run first, so formulas only see filtered records. Grouping comes before summaries, so formulas can only use group-level values. The formula is the last step, which is why it cannot be used in a row-level filter.
Exercise
A summary report on Opportunities is grouped by Close Date (calendar month) and shows AMOUNT:SUM. You add the formula AMOUNT:SUM - PREVGROUPVAL(AMOUNT:SUM, CLOSE_DATE). The January row shows the same number as its Amount sum, and a colleague says the formula is broken. What is the cause, and how would you change the formula so January does not mislead readers?
Show answer
January is the first group, so it has no previous group and PREVGROUPVAL returns zero or blank. The change therefore equals the full amount. The formula is working as designed. To avoid misleading readers, you could leave the first row blank by hiding the formula at that level, or note the limitation in the report description. You could also add a prior-period filter so that an earlier month appears as the first group and is then excluded from view with a chart or dashboard filter.
Exercise
A matrix report has Stage as rows and Close Year as columns. You want each cell to show average deal size. Why is AMOUNT:SUM / RowCount safer than averaging the cell values at the totals?
Show answer
AMOUNT:SUM / RowCount is calculated at each level from that level's own sum and count, so the row totals, column totals, and grand total are true weighted averages. Averaging the cell values would give a small cell the same weight as a large one and produce a wrong total.