This page provides information about how to use custom calculations for the following scenario:
Data analysts want to create data calculations to enhance the marketing strategy. The focus is on creating custom calculations to track customer returns over the past 15 days and save this data as scores to improve marketing decisions. The aim is to apply insights to Campaigns, Metrics, and 360 Profiles.
Checklist for using custom calculations
Complete the following tasks to use custom calculations for the given scenario:
In the custom calculation file, click the default cell and change it from Code to Markdown.
Copy the following content in the cell:
# Return Orders Calculation
In this notebook we will go through an example of how to use the CustomCalculationsClient to perform a simple custom calculation for return orders in last 15 days.
## Description
The goal of this notebook is to compute the number of **return orders per customer in the last 15 days** using CDP Customer data, and **store the scores** in CDP for further integrations.
Verify that the content is displayed as follows:
Click Run or use the keyboard shortcut Shift+Enter to run the cell.
The application displays the content in Markdown and also displays a new cell.
Use Markdown to organize your notebook with clear and structured sections for ease of understanding. Select the cell and change it from Code to Markdown. To perform custom calculations, follow these steps and execute the given text at each stage:
Initialize SDK and create a new project in the data model.
### STEP 1 - Import the CustomCalculationsClient and libraries <a class="anchor" id="step-1"></a>
This step involves importing the A1 `CustomCalculationsClient` and initializing it.
Write the SQL statement and run the query to retrieve the results.
### STEP 2 - Prepare the SQL query and retrieve the data <a class="anchor" id="step-2"></a>
This step involves preparation of SQL query and it's execution to retrieve the desired data from CDP.
Verify the SQL results to ensure that they align with the business logic.
Save and integrate the results into the Snowflake data model for the new calculation project.
### STEP 3 - Saving scores to the CDP<a class="anchor" id="step-3"></a>
In this step, we save the dataframe of scores back to the CDP.
The system publishes and distributes your custom calculation to Actions, Metrics, and 360 Profiles for integration.
### STEP 4 - Surface Scored Data to the CDP<a class="anchor" id="step-4"></a>
* The model scores will still be surfaced to all modules of the application after the CDP pipeline refresh is complete.
Provide a display name and description for your calculation.
Cells before execution:
Cells after execution:
Set your custom calculation to synchronize and run daily to ensure consistent data updates.
Insert a code for each step. To execute a cell, click Run or press Shift+Enter, and proceed in a sequence from steps 1 to 5.
Select Code to convert the cell into a code environment.
Create a code cell and enter the following Python code to import libraries:
from a1mlclient import CustomCalculationsClient
client = CustomCalculationsClient(project_name="return-orders-calculation")
Import Libraries
Create a code cell and enter the following SQL query:
query = '''
SELECT
MASTERCUSTOMERID,
COUNT(*) AS SCORE,
CASE
WHEN SCORE > 15 THEN 'HIGH'
WHEN SCORE BETWEEN 10 AND 15 THEN 'MEDIUM'
ELSE 'LOW'
END AS VALUE_NAME
FROM STUDIO_TRANSACTIONSUMMARY
WHERE SUBTYPE = 'Returned' AND TRANSACTIONTIMESTAMP >= DATEADD(day, -15, GETDATE())
GROUP BY MASTERCUSTOMERID ORDER BY SCORE DESC;
'''
df = client.query_data(query)
df.head(25)
SQL Query Execution
Create a code cell and enter the following code to save data:
# Write the data to CDP
client.write_output_data(df = df, description = 'ML Studio Simple Custom Calculation')
Saving Scores to CDP
This step is automated. Code is not required to surface CDP data.
Create a code cell and add the following code:
client.update_output_name(
display_name="ML Studio Custom Calculation Demo",
description="ML Studio Custom Calculation Demo Description"
)
Frequency Setting: Choose from daily, weekly, or monthly intervals.
Time Setting: Specify the time in 24-hour HH:MM format, UTC (e.g., 18:00 = 6 PM UTC).
Notebook Path Setting: Define the path for the notebook to execute as per your chosen frequency and time.
Schedule Name Setting: Assign a name to your schedule for easy identification
Important
Locate the notebook path parameter after notebooks/ in your browser's URL. Copy and paste the value All_Custom_Cals_Test/custom_calculations_return_order_calculation_sample.ipynb into the notebook_path field of the Python object.
Insert and run the following code in a new code cell to stop the schedule:
#delete the schedule
client.delete_schedule(notebook_path = 'All_Custom_Cals_Test/custom_calculations_return_order_calculation_sample.ipynb')
client.delete_schedule(schedule_name = "sample_return_order_calculation_schedule")
Insert and run the following code in a new code cell to confirm the schedule removal:
You can examine the output data. The following command loads the current calculation results into a DataFrame for inspection:
#Reading Current Calculation Output
read_df = client.read_output_data()
print(read_df)
Listing available tables
You can identify available tables for SQL queries. The following command returns a DataFrame listing all accessible tables:
#Listing Available Tables
tbl_df = client.list_available_tables()
print(tbl_df)
Viewing Projects in Use
You can identify the scope and potential project interactions. The following command returns a list of active projects and retrieves the project names:
#Viewing Projects in Use
proj_names = client.list_project_names()
print(proj_names)
Checking Project Metadata
You can gain insights into a specific project. The following command returns previous outputs and metadata:
Replace "YourProjectName" with your project's actual name to understand its structure and computational history.
Using Custom Calculations
This page provides information about how to use custom calculations for the following scenario:
Data analysts want to create data calculations to enhance the marketing strategy. The focus is on creating custom calculations to track customer returns over the past 15 days and save this data as scores to improve marketing decisions. The aim is to apply insights to Campaigns, Metrics, and 360 Profiles.
Checklist for using custom calculations
Complete the following tasks to use custom calculations for the given scenario:
In the custom calculation file, click the default cell and change it from Code to Markdown.
Copy the following content in the cell:
# Return Orders Calculation
In this notebook we will go through an example of how to use the CustomCalculationsClient to perform a simple custom calculation for return orders in last 15 days.
## Description
The goal of this notebook is to compute the number of **return orders per customer in the last 15 days** using CDP Customer data, and **store the scores** in CDP for further integrations.
Verify that the content is displayed as follows:
Click Run or use the keyboard shortcut Shift+Enter to run the cell.
The application displays the content in Markdown and also displays a new cell.
Use Markdown to organize your notebook with clear and structured sections for ease of understanding. Select the cell and change it from Code to Markdown. To perform custom calculations, follow these steps and execute the given text at each stage:
Initialize SDK and create a new project in the data model.
### STEP 1 - Import the CustomCalculationsClient and libraries <a class="anchor" id="step-1"></a>
This step involves importing the A1 `CustomCalculationsClient` and initializing it.
Write the SQL statement and run the query to retrieve the results.
### STEP 2 - Prepare the SQL query and retrieve the data <a class="anchor" id="step-2"></a>
This step involves preparation of SQL query and it's execution to retrieve the desired data from CDP.
Verify the SQL results to ensure that they align with the business logic.
Save and integrate the results into the Snowflake data model for the new calculation project.
### STEP 3 - Saving scores to the CDP<a class="anchor" id="step-3"></a>
In this step, we save the dataframe of scores back to the CDP.
The system publishes and distributes your custom calculation to Actions, Metrics, and 360 Profiles for integration.
### STEP 4 - Surface Scored Data to the CDP<a class="anchor" id="step-4"></a>
* The model scores will still be surfaced to all modules of the application after the CDP pipeline refresh is complete.
Provide a display name and description for your calculation.
Cells before execution:
Cells after execution:
Set your custom calculation to synchronize and run daily to ensure consistent data updates.
Insert a code for each step. To execute a cell, click Run or press Shift+Enter, and proceed in a sequence from steps 1 to 5.
Select Code to convert the cell into a code environment.
Create a code cell and enter the following Python code to import libraries:
from a1mlclient import CustomCalculationsClient
client = CustomCalculationsClient(project_name="return-orders-calculation")
Import Libraries
Create a code cell and enter the following SQL query:
query = '''
SELECT
MASTERCUSTOMERID,
COUNT(*) AS SCORE,
CASE
WHEN SCORE > 15 THEN 'HIGH'
WHEN SCORE BETWEEN 10 AND 15 THEN 'MEDIUM'
ELSE 'LOW'
END AS VALUE_NAME
FROM STUDIO_TRANSACTIONSUMMARY
WHERE SUBTYPE = 'Returned' AND TRANSACTIONTIMESTAMP >= DATEADD(day, -15, GETDATE())
GROUP BY MASTERCUSTOMERID ORDER BY SCORE DESC;
'''
df = client.query_data(query)
df.head(25)
SQL Query Execution
Create a code cell and enter the following code to save data:
# Write the data to CDP
client.write_output_data(df = df, description = 'ML Studio Simple Custom Calculation')
Saving Scores to CDP
This step is automated. Code is not required to surface CDP data.
Create a code cell and add the following code:
client.update_output_name(
display_name="ML Studio Custom Calculation Demo",
description="ML Studio Custom Calculation Demo Description"
)
Frequency Setting: Choose from daily, weekly, or monthly intervals.
Time Setting: Specify the time in 24-hour HH:MM format, UTC (e.g., 18:00 = 6 PM UTC).
Notebook Path Setting: Define the path for the notebook to execute as per your chosen frequency and time.
Schedule Name Setting: Assign a name to your schedule for easy identification
Important
Locate the notebook path parameter after notebooks/ in your browser's URL. Copy and paste the value All_Custom_Cals_Test/custom_calculations_return_order_calculation_sample.ipynb into the notebook_path field of the Python object.
Insert and run the following code in a new code cell to stop the schedule:
#delete the schedule
client.delete_schedule(notebook_path = 'All_Custom_Cals_Test/custom_calculations_return_order_calculation_sample.ipynb')
client.delete_schedule(schedule_name = "sample_return_order_calculation_schedule")
Insert and run the following code in a new code cell to confirm the schedule removal:
You can examine the output data. The following command loads the current calculation results into a DataFrame for inspection:
#Reading Current Calculation Output
read_df = client.read_output_data()
print(read_df)
Listing available tables
You can identify available tables for SQL queries. The following command returns a DataFrame listing all accessible tables:
#Listing Available Tables
tbl_df = client.list_available_tables()
print(tbl_df)
Viewing Projects in Use
You can identify the scope and potential project interactions. The following command returns a list of active projects and retrieves the project names:
#Viewing Projects in Use
proj_names = client.list_project_names()
print(proj_names)
Checking Project Metadata
You can gain insights into a specific project. The following command returns previous outputs and metadata: