Script 225: Set New Budget Strategy Targets at Month Start

Purpose

Pushes new Strategy Targets at start of month

To Elaborate

This Python script is designed to set new strategy targets for campaigns at the start of each month. It identifies strategies with a special token in their names and assigns new target values to them based on predefined reset values. The script then generates an output dataframe containing the strategies with changed targets.

Walking Through the Code

  1. The script begins by defining configurable parameters, including a token for identifying strategies that need target resets and a dictionary of strategy names and their corresponding reset values.
  2. The script then assigns column constants for various columns in the input and output dataframes.
  3. The user is prompted to provide a value for the “Target” column in the output dataframe.
  4. The script filters the input dataframe to include only strategies with the special token in their names.
  5. A temporary budget column is created in the input dataframe.
  6. The script overrides the values in the temporary budget column with the reset values for the corresponding strategies, if provided.
  7. The reduced input dataframe with the new target values is printed.
  8. The script prepares the output dataframe by selecting the strategy and temporary target columns.
  9. The script determines which rows have changed targets by checking if the temporary target is not null and different from the original target.
  10. The output dataframe is created by selecting the changed rows, renaming the temporary target column, and rounding the target values to 2 decimal places.
  11. If the output dataframe is not empty, it is printed. Otherwise, a message indicating an empty output is printed.

Vitals

  • Script ID : 225
  • Client ID / Customer ID: 1306916981 / 60268647
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Strategy
  • Output Columns: Strategy, Target
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Michael Huang (mhuang@marinsoftware.com)
  • Created by Michael Huang on 2023-06-28 09:09
  • Last Updated by Michael Huang on 2023-12-06 04:01
> See it in Action

Python Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#
# Set Campaign Budget Strategy Targets at Month Start
#
# Michael S. Huang
# 2023-06-28
#

########## CONFIGURABLE PARAMS - START ########## 

STRATEGY_RESET_TOKEN = "[MONTH_START_RESET_BUDGET]"

# define strategy target reset values for specific strategies
STRATEGY_TARGET_RESET_VALUES = {
    # example
    # "<strategy name>": <strategy target/budget in client currency>
    "Elem Edu [MONTH_START_RESET_BUDGET]": 1319.0,
    "Early Childhood [MONTH_START_RESET_BUDGET]": 3041.0,
    "Comp Sci [MONTH_START_RESET_BUDGET]": 4201.0,
    "MAT [MONTH_START_RESET_BUDGET]": 2286.0,
    "Teaching Bachelor’s [MONTH_START_RESET_BUDGET]": 2821.0,
}

########## CONFIGURABLE PARAMS - END ############

RPT_COL_STRATEGY = 'Strategy'
RPT_COL_TARGET = 'Target'
RPT_COL_GOAL = 'Goal'
RPT_COL_CONSTRAINT = 'Constraint'
RPT_COL_BID_CALCULATIONS = 'Bid Calculations'
BULK_COL_STRATEGY = 'Strategy'
BULK_COL_TARGET = 'Target'

outputDf[BULK_COL_TARGET] = "<<YOUR VALUE>>"


# Limit target reset to strategies with special token
target_reset_strategies = inputDf[RPT_COL_STRATEGY].map(lambda x: STRATEGY_RESET_TOKEN in x)
inputDf = inputDf.loc[target_reset_strategies].copy()

# define and clear tmp budget column
TMP_TARGET = BULK_COL_TARGET + '_'
inputDf[TMP_TARGET] = np.nan

# override with reset value, if provided
for strategy, new_target in STRATEGY_TARGET_RESET_VALUES.items():
    print(f"Strategy: {strategy}; New Target: {new_target}")
    inputDf.loc[ inputDf[RPT_COL_STRATEGY] == strategy, TMP_TARGET] = new_target

print("reduced inputDf with new target", tableize(inputDf))

# prepare output

cols = [BULK_COL_STRATEGY, TMP_TARGET]

# only considered changed if there is a different non-zero target
changed = inputDf[TMP_TARGET].notna() & (inputDf[TMP_TARGET] != inputDf[RPT_COL_TARGET])

# only select changed rows, and round budget to 2 decimal places
outputDf = inputDf.loc[ changed, cols ].copy() \
                  .rename(columns = { \
                    TMP_TARGET: BULK_COL_TARGET \
                  }) \
                  .round(2)

if not outputDf.empty:
  print("outputDf", tableize(outputDf))
else:
  print("Empty outputDf")

Post generated on 2024-03-10 06:34:12 GMT

comments powered by Disqus