addfieldx
Add a new column to the data extract with values calculated using a Python expression.
This guide explains how to configure the addfieldx instruction. To learn about another instruction, go back to the Available custom script instructions overview.
Introduction
Use the addfieldx instruction to add a new column to the data extract with values calculated using a Python expression.
The Create or Edit Columns standard transformation enables you to add new fields to your data extract in a few easy steps, instead of using a custom script. For more information, see Create or Edit Columns.
If you want to use the addfieldx custom script instruction to select rows containing numbers, the values used in the instruction must be in numeric format. If necessary, use the convertnumbers custom script instruction to convert string values to a numeric format. Add the convertnumbers instruction to the same custom script transformation before the addfieldx instruction.
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 Configuration 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
-
Campaign and Ad Group
-
Python Expression
-
{Campaign} + '_' + {Ad Group}
-
Field Index
-
-1
Data table before transformation
Campaign |
Ad Group |
Clicks |
---|---|---|
Brand |
media |
7 |
Brand |
ecommerce |
3 |
Dashboard |
media|social |
18 |
Data table after transformation
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 |