Sub topic 7
Building a Summary Report
This chapter assumes you can navigate Lightning Experience, know how Accounts relate to Opportunities, and have permission to create and run reports. It also assumes you have seen row groupings and summary functions (sum, average, minimum, maximum, record count) from the earlier chapters.
What a summary report is
A summary report is a tabular report with one or more row groupings. Each group shows its detail rows and a subtotal line. The subtotals are computed from the summarized fields you choose. Use this format when the question is "how much, or how many, per category?"
Build the report
This example answers: "What is the total and average value of open opportunities by stage?"
- In Lightning Experience, open Reports and select New Report.
- Choose the Opportunities report type and select Start Report.
- In the report builder, open the Outline panel. Set Show to All opportunities and set a Close Date range such as Current FY.
- Add a filter on Closed equals False so only open opportunities remain.
- Drag Stage into the Group Rows area.
- Add the columns Opportunity Name, Account Name, and Amount.
- Select the Amount column menu and choose Summarize. Tick Sum and Average.
- Select Save & Run.
Each stage now appears as a group heading. Under it are the matching opportunities and a subtotal line with the group's record count, summed Amount, and average Amount. A grand total sits at the bottom.
Add a second grouping level
You can nest up to three row groupings. Drag Owner under Stage. The report then shows, within each stage, one subgroup per owner. Order matters. The first grouping is the outermost level, so Stage then Owner reads differently from Owner then Stage.
Add a custom summary formula
A custom summary formula calculates a value from the group-level summaries. To show the average amount per group, create a formula named Avg Deal that uses this expression:
AMOUNT:SUM / RowCountRowCount is the number of records in the group. To compare each group with the one before it, in a report grouped by Close Date, use:
AMOUNT:SUM - PREVGROUPVAL(AMOUNT:SUM, CLOSE_DATE)Set the formula's display level to the grouping it applies to. If you choose the wrong level, the numbers come out wrong or the formula shows no value.
The same result in SOQL
A summary report is conceptually a SOQL aggregate query. This query returns the numbers on the subtotal lines of the report above:
SELECT StageName, SUM(Amount) total, COUNT(Id) cnt
FROM Opportunity
GROUP BY StageNameThe report adds the detail rows, the grand total, and the formatting on top.
Common errors
- Subtotals missing: You have not chosen a summary function for the column. Use the column menu and select Summarize.
- Counts look too high: The report type may join to a child object, such as Opportunity Products. Each child row repeats its parent, so the parent is counted more than once. Choose a report type that matches the level you want to count.
- Records missing: Check the Show scope and any filters. A report shows only records the running user can see.
- Blank group: Records with no value in the grouping field appear in a group labeled with a dash. Add a filter to exclude them if you do not want them.
Exercise
You group an Opportunities report by Stage and sum Amount. The Stage "Negotiation" shows 12 records, but you know only 8 opportunities are in that stage. What is the most likely cause, and how would you check it?
Show answer
The report type probably includes a child object, such as Opportunities with Products, so opportunities with several product lines are counted once per line. Check the report type in the report's settings. Switch to the plain Opportunities report type if you want to count opportunities, not product lines.
Exercise
Write a custom summary formula that returns the average opportunity amount per group in a summary report.
Show answer
AMOUNT:SUM / RowCount