Adding certain dates to data extracts

Use a Python expression to add certain dates to a data extract.

Introduction

Enter a Python expression into the addfieldx transformation to add certain dates to your data extract. For example, use a Python expression to add today's date or yesterday's date to a data extract.

The examples below demonstrate how the Python expressions can convert the dates from one format to another.

Configuring the Python expression

To add certain dates to your data extract, enter the following Python expressions into the transformation:

For today's date, enter:

  • __import__('datetime').datetime.today()

For yesterday's date, enter:

  • __import__('datetime').datetime.today() - __import__('datetime').timedelta(days=1)

Example 1 - Adding today's date

This example uses the addfieldx transformation to add a new column to your data extract and populate the new column with today’s date (the day on which the transformation is run). The dates are added in the following date and time format %Y-%m-%d %H:%M:%S.%f.

In this example the transformation is run at 10 am on 27 July 2022.

Notice that the times are different. This is because the python expression includes microseconds in the output. To remove the microseconds from the values in the column, change the format of the dates and time using another Python expression . For more information on how to convert times and date formats, see Converting dates from one format to another.

For more information on the addfieldx transformation configuration, see the addfieldxtransformation reference.

Transformation configuration

Field Name

Transformation Date

Python Expression

__import__('datetime').datetime.today()

Field Index

-1

Data table before transformation

Conversion ID

Purchase Date

4310177778

09/29/2021

4328218379

01/05/2022

5260818288

03/27/2022

2028958545

08/07/2021

4393034066

03/13/2022

4989141466

09/04/2021

4911856467

07/11/2022

Data table after transformation

Conversion ID

Purchase Date

Transformation Date

4310177778

09/29/2021

2022-07-27 10:00:00.020131

4328218379

01/05/2022

2022-07-27 10:00:00.020347

5260818288

03/27/2022

2022-07-27 10:00:00.020467

2028958545

08/07/2021

2022-07-27 10:00:00.020578

4393034066

03/13/2022

2022-07-27 10:00:00.020687

4989141466

09/04/2021

2022-07-27 10:00:00.020795

4911856467

07/11/2022

2022-07-27 10:00:00.020903

Example 2 - Adding yesterday's date

This example uses the addfieldx transformation to add a new column to your data extract and populate the new column with yesterday's date (24 hours before the transformation is run). The dates are added in the following date and time format %Y-%m-%d %H:%M:%S.%f.

In this example the transformation is run at 10 am on 27 July 2022.

Notice that the times are different. This is because the python expression includes microseconds in the output. To remove the microseconds from the values in the column, change the format of the dates and time using another Python expression . For more information on how to convert times and date formats, see Converting dates from one format to another.

For more information on the addfieldx transformation configuration, see the addfieldxtransformation reference.

Transformation configuration

Field Name

24 hours ago

Python Expression

__import__('datetime').datetime.today() - __import__('datetime').timedelta(days=1)

Field Index

-1

Data table before transformation

Conversion ID

Purchase Date

4310177778

09/29/2021

4328218379

01/05/2022

5260818288

03/27/2022

2028958545

08/07/2021

4393034066

03/13/2022

4989141466

09/04/2021

4911856467

07/11/2022

Data table after transformation

Conversion ID

Purchase Date

24 hours ago

4310177778

09/29/2021

2022-07-26 10:00:00.020138

4328218379

01/05/2022

2022-07-26 10:00:00.020352

5260818288

03/27/2022

2022-07-26 10:00:00.020469

2028958545

08/07/2021

2022-07-26 10:00:00.020512

4393034066

03/13/2022

2022-07-26 10:00:00.020695

4989141466

09/04/2021

2022-07-26 10:00:00.020802

4911856467

07/11/2022

2022-07-26 10:00:00.020915