addfieldusingcontextx

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

Introduction

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

Creating the enrichment

For more information on creating an enrichment, see Using custom scripts.

Configuring the enrichment

To configure the enrichment, fill in the following fields. Required fields are marked with an asterisk (*).

Field Name

Specify the name of the new field 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 contain the enriched data. The enrichment is applied to the whole data extract, then the enriched data is output into the subtable you have named here.

This subtable is a temporary table, which means it 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.

Example

Enrichment configuration

Field Name

All clicks

Python Expression

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

Data table before enrichment

Ad Group

Clicks

Clicks other

media

7

5

ecommerce

3

3

media|social

18

4

Data table after enrichment

Ad Group

Clicks

Clicks other

All clicks

media

7

5

ecommerce

3

3

8

media|social

18

4

21