ETLMCPData Conveyance Works
Log in Start free
Docs

Derive rule

How a derive rule adds a calculated column such as ROAS, which fields it can use, why a bad row gets null, and why the result arrives as text.

A derive rule adds one calculated column to every row: you give the new column a name in as and a formula in expr, such as roas from conversion_value / cost. Both are required. etlmcp works the formula out row by row, and when a row cannot be worked out, for example because it divides by zero or by an empty value, that row gets null instead of failing the run. In PostgreSQL the new column is created as text, so cast it to a number in your queries.

Writing the rule

A pipeline's rules are a list applied in order, and a derive rule is one entry in it:

{"op": "derive", "as": "roas", "expr": "conversion_value / cost"}

In the dashboard's pipeline editor, pick Derive and type the rule as one line, name = expression, for example roas = conversion_value / cost. The editor accepts a name made of letters, digits and underscores that does not start with a digit.

A pipeline holds at most 50 rules, and one expression holds at most 2000 characters.

If as names a column the row already has, the derive replaces that column's value. Any other name adds a column.

What the formula can use

The formula takes numbers, the operators + - * / % **, parentheses, comparisons, and the functions abs, min, max, round, floor, ceil, ln, log10 and sqrt. It cannot run code of its own.

When you save the pipeline or preview the rules, etlmcp tries each formula once against stand-in values, and it accepts only these field names:

  • the common metrics impressions, clicks, cost, conversions, conversion_value, ctr, cpc, cpm and cpa;
  • currency and date;
  • any platform extra, written extra. and the extra's key.

A name outside that list is refused with a message that ends unknown field {…}, with the name, in single quotes, in place of the braces. That includes text columns such as campaign_name, a column you renamed earlier (spend after renaming cost), and a column an earlier derive made: a second rule roas * 2 after the roas rule is refused, so write the whole formula again, as conversion_value / cost * 2. Multiplying text is refused too: currency * 2 gets cannot multiply text.

Each rule is checked on its own, and the message names the rule's place in the list, counting from 0. A derive with no as is refused with derive (rule 0) needs 'as'; one with no formula, or a blank one, is refused with a message that ends needs an 'expr' string.

How each row is worked out

The rule runs on every row the earlier rules leave. If the formula fails on a row, that row's new column is null and the run carries on. Empty values and errors work like this:

  • Division or remainder by zero gives null, and so does dividing by an empty value. With roas = conversion_value / cost, a row with cost and a conversion value of 0 gives 0, while a row with zero cost or an empty conversion value gives null.
  • Multiplying an empty value gives null. Adding or subtracting treats an empty value as 0, so cost + extra.shipping is just cost on a row with no shipping extra.
  • An extra that a row does not have counts as empty, since platforms leave out the extras a row has no value for.
  • A comparison with an empty value is false.
  • A comparison on its own, such as cost > 100, gives true or false.

How the column is delivered

etlmcp decides a column's type from where its values come from, never from a sample. The common metrics and a platform's catalogued extras are numbers; a derived column is text. In PostgreSQL, a new derived column is created as a text column, and each value is written as its text: 0.5, or True and False for a comparison. Null stays null. To sum or average a derived column in SQL, cast it to a number first. A column of an existing table keeps the type it was created with.

The one exception is a derive whose as is one of the common metrics, such as recomputing cpc: those columns are always delivered as numbers.

To shorten long decimals, add a round rule after the derive, or wrap the formula in round(…, 2).

Derive and group

A group rule keeps only the columns you group by, the metrics and numeric platform extras it combines, and the ratios it works out again from the totals, including roas. A column derived before a group is dropped unless you group by it. To get a ratio over a total that the group does not work out itself, such as conversions per click by campaign and day, put the derive after the group.

Where you see the result

Derived columns exist only in what a pipeline delivers. The stored rows that query_data returns are the data as pulled, before any rules, so a derived column is not among them. list_pipelines shows each pipeline's rules, derive rules included.

Related