ETLMCPData Conveyance Works
Log in Start free
Docs

Group by rule and aggregations

How the group rule collapses rows: which columns it adds up, which ratios it works out again, which extras it skips, and what each agg setting does.

A group rule turns many rows into one row per distinct combination of the columns you name in by. By default it adds up impressions, clicks, cost, conversions and conversion_value within each group, then works out ctr, cpc, cpm and cpa again from those totals and, where the totals allow it, adds a roas column. It never averages the ratios the platform sent. Extras that count people once, reach, total_users and new_users, are left out of the result, because a total of them would count the same person many times.

Writing the rule

The rule takes two settings:

  • by: a list of at least one column name. These become the key of each output row. An extra can be a key too, written with its prefix, such as extra.region.
  • agg: how the metric columns are combined, one of sum, max, min, avg or count. Leave it out and it is sum.

In JSON, a rule that rolls a daily campaign table up to one row per day and campaign reads:

{"op": "group", "by": ["date", "campaign_name"]}

When you save the pipeline, etlmcp checks both settings. An empty or missing by is refused with group (rule {…}) needs a non-empty 'by' list, and any other agg word with group (rule {…}) agg must be sum/max/min/avg/count. The braces hold the rule's position in the list. See transform validation errors for the full list.

What the output rows hold

Each output row has only these columns:

  • the by columns, with the group's values;
  • the five base metrics, combined with agg;
  • the ratios cpc, ctr, cpm and cpa, and roas, worked out from the combined metrics;
  • an extra set with the platform's other numeric metrics, combined with agg.

Every other column is dropped: account and campaign names, IDs, network and currency are gone unless you list them in by. If you need a name next to an ID, group by both. Each key value is turned into text, so a number you group by arrives as text (see which delivered columns are numeric). Rows with no value in a key column form their own group.

The output is sorted by the by columns, in the order you list them: rows with an empty key first, then the rest in text order. Because keys are text, 10 sorts before 9.

How the ratios are recomputed

For each group, etlmcp takes the combined base metrics and divides:

Column Worked out as
cpc cost divided by clicks
ctr clicks divided by impressions
cpm cost divided by impressions, times 1000
cpa cost divided by conversions
roas conversion value divided by cost

A ratio whose divisor is zero or missing is left out of that row rather than set to zero. roas is added only when the group's cost is not zero and it has a conversion value. A campaign's CPC across a week is the week's cost over the week's clicks, not the average of seven daily CPCs.

With agg set to max, min or avg, the ratios are still computed from the combined columns. Under max, cpc becomes the highest cost in the group divided by the highest clicks, which may come from different rows. Use sum when you need a correct rolled-up ratio.

Extras and the ones that are skipped

Numeric extras, such as a platform's video views, are combined with the same agg as the base metrics. Three are never combined: reach, total_users and new_users. Each counts distinct people, so the same person seen on two days or in two campaigns would be counted twice in a total. The TikTok connector's catalogue describes its reach as people who saw the ad that day, and the GA4 connector's catalogue lists total_users and new_users. After a group, those extras are absent from the rows. If you need reach or users for a longer period, pull them from the platform for that period instead of adding up daily values.

The agg settings

  • sum: totals.
  • avg: the plain mean of the non-empty values in the group, each row weighted the same.
  • max and min: the largest or smallest non-empty value.
  • count: accepted, but it combines nothing. The rows keep only their by columns, with no metrics, ratios or extras, so it gives you the list of distinct keys and no number of rows. A column made by a derive rule does not survive a group either, since only the keys, the base metrics, the ratios and the extras are kept.

A base metric with no number in any row of a group comes out empty for that group.

Currency

The group adds cost and conversion_value across every row in a group, whatever currency each row is in. If one pipeline's rows can hold more than one currency, put currency in by, or convert the amounts with an fx rule before the group. An fx rule with rates placed after a group that drops currency is refused when you save, because the amounts' currency is no longer known, unless an earlier fx rule with rates already converted them.

In the dashboard editor

The pipeline editor offers Group by as one of its rule kinds. You type the key columns separated by commas, for example date, campaign_name, and the editor saves a rule with by only, so it always sums. A group rule saved with another agg, which the editor cannot write, still runs: the editor shows it read-only as its JSON and keeps it as it is when you save.

Where to place it

Rules run in order, and a group changes which columns later rules can see. Put a filter before the group when you want to drop rows from the totals, and after it when you want to drop whole groups. A rename or select after a group works only on the columns the group kept.

Related