Custom Group

The Custom Group action lets you group and aggregate data based on a custom set of fields or calculations. This is useful when the type of thing you want to group by does not have its own table in the database.

For example, the perspective below shows data from three joined tables Courses, Sections, and Meetings. Between Courses and Sections, we have sandwiched a Custom Group that shows building codes and, for each building, the sum of durations of meetings in that building:

Courses, Sections, and Meetings subqueries with a Custom Group subquery sandwiched between Courses and Sections so that the list of sections and meetings are grouped by Building Code. The building is calculated with a formula, based on the first five characters of each meeting's Room Number.

There is no “Buildings” table in this example. Instead, we used a formula to derive a building code from the first five letters of each Room Number in the Meetings table.

The Custom Group action is also frequently used with single-table datasets, e.g. to select fields to group by for aggregation. For example, we can group labor statistics by state, and calculate average salaries across relevant data points:

A Custom Group used with a single-table dataset, to calculate average wage levels per state.

In the structure of a perspective, a Custom Group is a subquery that takes its data from inward-referencing formulas, rather than from an associated data table. Often, the formula is just a simple reference to the field to group by, with no actual calculations involved.

How to Use

In an existing perspective, select the field(s) you want to group by, and click Custom Group in the context menu. Then select the level where the grouping should occur.

The Custom Group action, used on a perspective initially showing Meetings by Sections by Courses, to insert a grouping by Room Number between Courses and Sections.

A Custom Group subquery can be placed within any existing one-to-many relationship above the referenced fields. If only one location is possible, it will be used automatically.

The new subquery appears with the name Custom Group in grey text in the table header. Each field in the new subquery contains a formula, pulling data to group by from nested subqueries.

Grouping and Aggregation

In a Custom Croup subquery, you can add formulas with aggregate functions such as sum and count. There is a shortcut action for some of these.

Continuing our example, we could find the total Capacity for each Room Number. We use the shortcut Sum action, and select the Custom Group subquery as the desired level:

Using the Sum action to place a sum formula in a Custom Group subquery.

When the number of distinct grouped-by values is small, a crosstab visualization can be useful.

Grouping by a Calculation

You can edit the formula that defines the grouping. This allows you to group by the results of a calculation. Most commonly, you might wrap the existing reference in some function.

Continuing the previous example, we might take the first five letters of each Room Number to construct a building code, using the left function. Notice how both the grouping and the result of the adjacent aggregate formula changes as the formula is modified:

Using the LEFT function in the formula that retrieves the value to group by in the Custom Group subquery, to group by the first five letters of each Room Number.

Note that grouped-by and aggregated values may be sourced from different subqueries. See above with e.g. Room Number and Capacity in Meetings and Sections, respectively.

For grouping purposes, a null value is enumerated as a distinct value, after other values. Null values may occur in the data itself, or arise when there are no records at a referenced location. Both cases are illustrated below:

Two other cases from the previous perspective, where null values appear in the Custom Group subquery. In the first case, the course 'Milton' has a meeting with a null value in the Room Number field, which is propagated through the Building Code formula that is used for grouping. In the second case, the course 'Embedded Computing' has a section with no meeting, yielding a null value when the Meetings subquery is referenced from the Building Code formula.

Null values can be filtered out if desired, as in any other subquery.

For date and time fields, functions such as year and month can be used to group by relevant periods in the calendar. The next example will illustrate this.

Multiple Custom Group Subqueries

A perspective can have any number of Custom Group subquery, in any combination.

For example, below we use the year and monthName functions in separate nested subqueries, to group Log Events by year and then by month:

Log events grouped by year and by month, with two nested Custom Group subqueries.

Custom Group subqueries can exist in independent branches of a perspective. The following example shows two one-to-many relationships in parallel, with a Custom Group on each.

A perspective with two parallel one-to-many relationships, each with a separate Custom Group inserted.

Grouping by Multiple Fields

Alternatively, you can select multiple fields for grouping in a single Custom Group. This will enumerate all combinations of values that occur together.

A single table grouped by two of its fields, using the Custom Group action.

You can hide specific fields in the Custom Group subquery to temporarily omit them from the grouping. If a field is sorted on, it will be included in the grouping even when hidden.

Grouped-by fields may come from different subqueries. For example, we can group Sections by Format and Classroom Type from the Sections and Room Data tables, respectively:

A Custom Group which references and groups by fields from two different subqueries.

Deleting a Custom Group

To delete a Custom Group subquery, right-click Custom Group in the table header, and click Delete Custom Group.

The Delete Custom Group action, shown when the context menu is opened on a Custom Group subquery or the remaining visible primitive field(s) in said subquery.

Subqueries below the deleted one will be moved up, preserving their existing field selections, filters, format options, and so on.

The Delete Custom Group action also appears e.g. when deleting the last group-by field.

When to Use

The Custom Group feature is most appropriate when you wish to group by a subset of fields in a table, rather than by the overall entity that is represented by each row.

For example, if we have tables called Courses and Meetings, and we wish to find the total meeting time for each course, then there is no need for a Custom Group. Instead, we use the Courses table as the starting point for the perspective, and add the aggregation there:

An example of a case where a Custom Group is not needed, because there is already a Courses table in the database and we wish to group by courses. An aggregation of total meeting durations is shown for illustration.

By contrast, if we see that many courses have been repeated from year to year, and we wish to find the total historical meeting time across courses with the same title, then we would use a Custom Group on the Title field.

A case where a Custom Group is needed because we wish to group by the title of courses rather than courses by themselves. Course titles may be repeated because a similar course may be taught in different terms. Total meeting durations per course title is shown as well.

Another use for the Custom Group action is to group data in a different order than would be dictated by join relationships in the database. For example, we can show Sections by Instructors by Courses, even though Instructors is not joined directly to Courses:

A case where Custom Group is used to group entities in a different order than what would be dictated by the join relationships between tables in the database. Normally, Courses contain Sections which have assigned Instructors, but here we show courses with each section list grouped by instructor. The grouped by field is Instructor ID, which is shown in first name/last name form using the Compact Join feature.

In this case we are grouping by Instructor ID, which ends up being displayed as a compact join.

Custom Group vs. Move to Root

The Custom Group and Move to Root actions both affect the hierarchy of subqueries within a perspective. The two actions differ as follows:

  • Custom Group inserts fields to be grouped by as formulas in a new subquery, between or above existing subqueries. The relative order of existing subqueries is unchanged.
  • Move to Root reorders existing subqueries relative to each other.