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
- Product Ideas
- 2.1K Ideas Exchange
- Connect
- 1.3K Connectors
- 309 Workbench
- 7 Cloud Amplifier
- 10 Federated
- Transform
- 664 Datasets
- 120 SQL DataFlows
- 2.3K Magic ETL
- 825 Beast Mode
- Visualize
- 2.6K Charting
- 89 App Studio
- 46 Variables
- Automate
- 196 Apps
- 486 APIs & Domo Developer
- 94 Workflows
- 24 Code Engine
- AI and Machine Learning
- 23 AI Chat
- 4 AI Projects and Models
- 18 Jupyter Workspaces
- Distribute
- 119 Domo Everywhere
- 283 Scheduled Reports
- 11 Software Integrations
- Manage
- 143 Governance & Security
- 11 Domo Community Gallery
- 49 Product Releases
- 13 Domo University
- Community Forums
- 41 Getting Started
- 31 Community Member Introductions
- 116 Community Announcements
- 5K Archive