Sub topic 5
Grouping rows
This chapter assumes you can open the Lightning report builder, pick a report type, and add columns and filters. It covers how row groupings turn a flat list of records into a structured report.
What a grouping does
A grouping collects rows that share the same value in a field. A tabular report lists every record once. When you group by a field such as Stage, the builder sorts the rows into one section per distinct value. Each section gets a header and a subtotal area.
Grouping is the step that turns a tabular report into a summary report. The next chapter covers the summary calculations. Here we focus on how the groups themselves behave.
Adding a grouping in the report builder
- Open a report and make sure the Outline panel is visible.
- In the Group Rows box, add a field, for example Stage.
- Optionally add a second field in Group Rows, for example Close Date. Set Group Dates By to Calendar Month.
- Run the report and check the section headers.
Any field type can be a grouping except long text and a few other types. Picklists, dates, owners, and lookup names are the most common choices.
Multiple levels of grouping
You can nest groupings. A summary report allows up to three levels. The first field is the outermost level, and each added field subdivides the one above it.
For example, grouping Opportunities by Stage and then by Owner gives one section per stage. Inside each stage, you get one section per owner. The order matters. Stage then Owner answers "who owns the deals in each stage." Owner then Stage answers "how are each person's deals spread across stages."
Sorting and limiting groups
Each grouping has a sort direction, ascending or descending. By default, groups sort by the grouped field's own values. Picklists sort in the order defined on the field, not alphabetically, which is useful for stages that follow a pipeline.
Dates need a granularity. Grouping by the raw Close Date produces one group per day. Choose a unit such as Calendar Month, Calendar Quarter, or Fiscal Year to get a manageable number of groups.
Common errors
- Too many groups. Grouping by a field with thousands of distinct values, such as Account Name across a large org, makes the report slow and hard to read. Add a filter first, or choose a field with fewer values.
- Blank group. Records with no value in the grouped field fall into a group labeled with a dash. This is normal. Filter them out if you do not want them.
- Wrong date granularity. Grouping by day when you meant month produces hundreds of sections.
- Unexpected order. If a nested grouping reads backward from what you intended, drag the fields in the Outline to reorder them.
Grouping and SOQL
Each grouping corresponds to a GROUP BY field in a SOQL aggregate query. Grouping Opportunities by Stage matches this query:
SELECT StageName, COUNT(Id) cnt
FROM Opportunity
GROUP BY StageNameExercise
A report lists Opportunities grouped by Close Date with no date granularity set, and it has 400 sections. What change would make it readable, and why?
Show answer
Set Group Dates By to Calendar Month, or Calendar Quarter. Without a granularity, the report creates one group for each distinct day. A coarser unit combines those days into a small number of groups.
Exercise
You want to see, for each Stage, which owners hold the most deals. In what order should you add the two groupings?
Show answer
Group by Stage first and Owner second. The first grouping is the outer level, so each stage becomes a section and the owners appear inside it.