addfieldx
On this page
Add a new column to the data extract with values calculated using a Python expression.
Introduction
Use the addfieldx instruction to add a new column to the data extract with values calculated using a Python expression.
When to use this instruction
Use addfieldx when:
-
You want to create a new field based on a Python expression that combines or transforms existing columns.
-
You need conditional logic (for example different behaviour per platform or channel).
-
You are creating derived metrics or labels that cannot be expressed with a standard transformation.
If you only need to add a column with the same fixed value in every row, consider addfield or the Create or Edit Columns standard transformation instead.
Creating a custom script transformation using the addfieldx instruction
To create and configure a custom script using the addfieldx instruction, follow these steps:
-
In the Instruction step, select the addfieldx instruction.
-
To configure the custom script instruction, fill in the required fields below, as well as any optional fields you require:
- Field name (required)
Specify the name of the new column to add to the data extract.
- Python expression (required)
Specify the formula used to calculate the new values. Write this formula as a one-line Python expression. To use values in the same row as the current row but from different columns, surround the column name in curly brackets (
{}). For example,{dimension_a} + '_' + {dimension_b}or({metric_1} + {metric_2}) / 100.- Field index (required)
Specify where to add the new column in the data extract. To add the column to the first position, specify 0. To add the column to the last position, specify -1.
- Subtable
Enter the name for a subtable that you want to use within this custom script.
A subtable is a temporary table that only exists for this custom script. You can apply additional instructions within the same custom script to the subtable. However, the subtable cannot be used in any other custom scripts.
If a subtable does not exist for the current custom script, the transformation is applied to the data extract, and the enriched data is output into the subtable. If the subtable already exists for the custom script, the subtable is used as the input for the transformation and optionally as the output.
Example
Transformation configuration
- Field Name
CampaignandAd Group- Python Expression
{Campaign} + '_' + {Ad Group}
- Field Index
-1
Data table before transformation
Scroll horizontally to see the full table.
Campaign | Ad Group | Clicks |
|---|---|---|
Brand | media | 7 |
Brand | ecommerce | 3 |
Dashboard | media|social | 18 |
Data table after transformation
Scroll horizontally to see the full table.
Campaign | Ad Group | Clicks | Campaign and Ad Group |
|---|---|---|---|
Brand | media | 7 | Brand_media |
Brand | ecommerce | 3 | Brand_ecommerce |
Dashboard | media|social | 18 | Dashboard_media|social |