addfieldusingcontextx

Add a new column to the data extract with values calculated using data from the current, previous and next rows.

This guide explains how to configure the addfieldusingcontextx instruction. To learn about another instruction, go back to the Available custom script instructions overview.

Introduction

Use the addfieldusingcontextx instruction to add a new field to the data extract with values calculated using data from the current, previous and next rows.

Creating a custom script transformation using the addfieldusingcontextx instruction

To create and configure a custom script using the addfieldusingcontextx instruction, follow these steps:

  1. Create a custom script transformation.

  2. In the Instructions step, select the addfieldusingcontextx instruction.

  3. To configure the custom script instruction, fill in the following fields. Required fields are marked with an asterisk (*).

Field name*

Specify the name of the new column to add to the data extract.

Python expression*

Specify the formula used to calculate the new values. Write this formula as a one-line Python expression. In the formula, specify values in the table with the following prefixes:

  • Use the prefix prv to get the value from the row above the current row.

  • Use the prefix cur to get the value from the current row.

  • Use the prefix nxt to get the value from the row below the current row.

After the prefix, specify the columns to use in the formula. Use the following syntax:

  • If the column name does not contain whitespace, use dot notation. For example, prv.column_a.

  • If the column name contains whitespace, surround it with quotation marks. For example, prv['column b'].

If a row you specify in the formula is not available, the expression evaluates to NoneType.

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

All clicks

Python Expression

{cur.Clicks} + {prv['Clicks other']}

Data table before transformation

Ad Group

Clicks

Clicks other

media

7

5

ecommerce

3

3

media|social

18

4

Data table after transformation

Ad Group

Clicks

Clicks other

All clicks

media

7

5

ecommerce

3

3

8

media|social

18

4

21