Python Scripting in Magic ETL | Iterating through close dates to get unique account id values
Hello! I'm trying to find the issue in my current python code that iterates through each date while maintaining a set of already logged accounts and then counting new accounts each day. I have sorted by the 'Close Date' , grouped by the 'Close Date' and the 'Owner Division', and have 'new accounts' and 'seen_accounts' variables. I was hoping a unique count on the 'new_accounts' would work, but I'm running into an error where I can't preview the execution. Error message is "Output data not found or not parsable".
This is my code below:
Best Answer
-
You need to tell the tile what you want it to output:
write_dataframe(unique_account_count_cumulative)
I also think you want to unindent your current last line to take it out of the loop, otherwise you're unnecessarily over-writing the same dataframe over and over.
Please 💡/💖/👍/😊 this post if you read it and found it helpful.
Please accept the answer if it solved your problem.
0
Answers
-
Wait, I forgot to write back my data frame!
1 -
Try this:
from domomagic import *
import pandas as pd
# Reading the data from Domo
input1 = read_dataframe('Existing - Sales Rev')
# Sorting the DataFrame by 'Close Date'
df = input1.sort_values(by='Close Date')
# Initialize a set to keep track of seen accounts
seen_accounts = set()
# Initialize a list to store the results
results = []
# Group by 'Close Date' and 'Owner ID.Division'
for date, group in df.groupby(['Close Date', 'Owner ID.Division']):
# Find new accounts that haven't been seen before
new_accounts = group[~group['Account ID'].isin(seen_accounts)]
# Count unique new accounts
unique_count = new_accounts['Account ID'].nunique()
# Append the results to the list
results.append({
'Close Date': date[0],
'Owner ID.Division': date[1],
'UniqueAccountCount': unique_count
})
# Update the set of seen accounts with the newly found ones
seen_accounts.update(new_accounts['Account ID'])
# Create a DataFrame from the results
unique_account_count_cumulative = pd.DataFrame(results)
# Write the output DataFrame back to Domo
write_dataframe(unique_account_count_cumulative, 'Output Dataset Name')** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0 -
You need to tell the tile what you want it to output:
write_dataframe(unique_account_count_cumulative)
I also think you want to unindent your current last line to take it out of the loop, otherwise you're unnecessarily over-writing the same dataframe over and over.
Please 💡/💖/👍/😊 this post if you read it and found it helpful.
Please accept the answer if it solved your problem.
0 -
from domomagic import *
import pandas as pd
# Reading the data from Domo
input1 = read_dataframe('Existing - Sales Rev')
# Convert input to a DataFrame
df = pd.DataFrame(input1)
# Sort the DataFrame by 'Close Date'
df = df.sort_values(by='Close Date')
# Initialize a set to keep track of seen accounts
seen_accounts = set()
# Initialize a list to store the results
results = []
# Group by 'Close Date' and 'Owner ID.Division'
for date, group in df.groupby(['Close Date', 'Owner ID.Division']):
# Find new accounts that haven't been seen before
new_accounts = group[~group['Account ID'].isin(seen_accounts)]
# Count unique new accounts
unique_count = new_accounts['Account ID'].nunique()
# Append the results to the list
results.append({
'Close Date': date[0],
'Owner ID.Division': date[1],
'UniqueAccountCount': unique_count
})
# Update the set of seen accounts with the newly found ones
seen_accounts.update(new_accounts['Account ID'])
# Create a DataFrame from the results
unique_account_count_cumulative = pd.DataFrame(results)
# Optionally write the output DataFrame back to Domo (if needed)
# write_dataframe(unique_account_count_cumulative, 'Output Dataset Name')** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.6K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 102 SQL DataFlows
- 626 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 755 Beast Mode
- 61 App Studio
- 41 Variables
- 693 Automate
- 178 Apps
- 456 APIs & Domo Developer
- 49 Workflows
- 10 DomoAI
- 38 Predict
- 16 Jupyter Workspaces
- 22 R & Python Tiles
- 398 Distribute
- 115 Domo Everywhere
- 276 Scheduled Reports
- 7 Software Integrations
- 130 Manage
- 127 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 11 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 110 Community Announcements
- 4.8K Archive