To add conditions to your script, use if and else statements. This returns the value specified before the if criterion when it is fulfilled. Otherwise, the statement returns the value specified after the else keyword.
Use the following syntax:
<value_1> if <condition> else <value_2>
This syntax is the same used in a standard Python lambda function. For more information, see Python lambda if-else examples. Use conditional commands in the Python Expression field in transformations such as convertx and addfieldx. See the examples below for more information:
Example 1 - if ... else
Example 2 - if ... else with regex
Example 3 - if ... else ...if ... else
EXAMPLE 1
Input:
Output:
Script:
convertx
'D' if {Device} == 'Desktop' else {Device}
addfieldx
'Successful' if {Clicks} > 200 else 'Unsuccessful'
EXAMPLE 2
Input:
Output:
Script:
convertx
{Campaign}.split{'_'}[1] if re.match('.*_.*', {Campaign}) else 'None'
addfieldx
{Campaign}.split('_')[0] if re.match('.*_.*', {Campaign}) else {Campaign}
EXAMPLE 3
Input:
Output:
Script:
addfieldx
'D' if {Device} == 'Desktop' else 'M' if {Device} == 'Mobile' else ''
Comments
0 comments
Article is closed for comments.