Sub topic 10
Custom summary formulas
This chapter assumes you can build a summary report with row groupings and summarize a field with sum, average, minimum, maximum, or record count. It also assumes you have seen a matrix report with row and column groupings. Here you add calculated values on top of those summaries.
What a custom summary formula is
A custom summary formula (CSF) is a calculation that runs on the grouped, summarized values in a report. It does not run on individual records. You can add one to a summary, matrix, or joined report. A tabular report has no groupings, so it cannot have one.
Formulas are written in the report editor and use summary fields as inputs. A summary field is written as FIELD:AGGREGATE, such as AMOUNT:SUM. The built-in RowCount is the number of records in the group.
Ratio example: average amount per group
Suppose an Opportunity summary report is grouped by Stage and shows the sum of Amount. To get the average opportunity size in each stage, divide the sum by the record count.
AMOUNT:SUM / RowCountSet the format to Number or Currency. Choose where the formula is displayed: at all summary levels, or only at specific groupings. In a matrix, you can also choose which row and column levels show it.
Comparing groups with PREVGROUPVAL
PREVGROUPVAL returns the summary value of the previous group at a grouping level you name. Use it for period-over-period change. In a report grouped by Close Date (for example by calendar month), this formula shows how the sum of Amount changed from the prior group:
AMOUNT:SUM - PREVGROUPVAL(AMOUNT:SUM, CLOSE_DATE)The first group has no previous group. Its comparison value is empty, so the formula result for that row is not a meaningful change. Check how your org displays it before you share the report.
How the formula relates to groupings
A formula is evaluated once for each group at the level where it is shown. The diagram shows the flow from records to a formula result.
This also explains a common limit. A formula can only use values the report already summarizes. If you need AMOUNT:AVG, the field must be summarized as an average in the report first, or you can compute it from sum and count as above.
Common errors
- Dividing by zero. If a group can have a count or total of zero, wrap the division with
IF. For example:IF(RowCount = 0, 0, AMOUNT:SUM / RowCount). - Using a field that is not summarized. The editor only offers fields you have already summarized. Add the summary first.
- Wrong display level. A formula shown at the wrong level can look like a wrong total. Check the level you selected.
- Averaging averages. Averaging per-group averages gives a different answer than the true overall average. Divide the overall sum by the overall count instead.
- Misreading
PREVGROUPVAL. It depends on the grouping name and sort order you pass, so confirm both.
Relating to SOQL
A formula adds nothing to the underlying query. The report summarizes first, and the formula combines those results. The SOQL aggregate below returns the inputs for the first formula:
SELECT StageName, SUM(Amount) total, COUNT(Id) cnt
FROM Opportunity
GROUP BY StageNameDividing total by cnt for each row gives the same result as the formula.
Exercise
A summary report is grouped by Owner and shows the sum of Amount and the record count. Write a custom summary formula for average amount per owner that avoids a divide-by-zero error.
Show answer
IF(RowCount = 0, 0, AMOUNT:SUM / RowCount)
Exercise
Why can't you add a custom summary formula to a tabular report?
Show answer
A custom summary formula works on summarized values for each group. A tabular report has no groupings, so there are no group-level summaries for the formula to use.