I’ll walk you through the complete solution that addresses all three key aspects: CSV import aggregation, duplicate key error handling, and ETL preprocessing.
Step 1: ETL Preprocessing with Aggregation
First, you need to preprocess your CSV file to aggregate loyalty points by customer. Here’s a practical approach using Python pandas:
import pandas as pd
df = pd.read_csv('loyalty_source.csv')
aggregated = df.groupby('CustomerID').agg({'Points': 'sum', 'EnrollmentDate': 'min'}).reset_index()
aggregated.to_csv('loyalty_import_ready.csv', index=False)
This consolidates multiple program entries into single customer records with total points and earliest enrollment date.
Step 2: Duplicate Key Error Handling
The duplicate key error occurs because the CSV Import Utility processes records sequentially without in-memory deduplication. Even with ‘Update existing records’ enabled, it can’t handle duplicates within the same batch. Your options:
- Option A: Use the aggregated CSV from Step 1 (recommended)
- Option B: Enable the import tool’s ‘Skip duplicate records’ mode and run multiple passes, but this risks data loss
- Option C: Split CSV by program and import with custom merge logic in SAP CX workflows
Step 3: Validation and Import Sequencing
Before importing the aggregated file:
- Run a validation check:
SELECT CustomerID, COUNT(*) FROM staging_csv GROUP BY CustomerID HAVING COUNT(*) > 1 to confirm no duplicates remain
- Create a backup of existing loyalty data in SAP CX
- Use the Data Import Tool with these settings:
- Mode: Insert new records only (first run) or Update/Insert (if re-running)
- Batch size: 5,000 records (prevents timeout on large files)
- Error handling: Log errors but continue processing
- Monitor the import job logs for any foreign key violations or constraint errors
Step 4: Post-Import Reconciliation
After import completes:
- Compare record counts: source distinct customers vs imported records
- Validate total points: SUM(points) in source should match SAP CX loyalty totals
- Check for orphaned program enrollments if you need to preserve program-level detail
Alternative: Preserve Multi-Program Detail
If you need to maintain which points came from which program (Gold/Silver/Platinum), modify your data model:
- Import customers first (deduplicated list)
- Create a separate import for program enrollments with foreign key to customer
- Use SAP CX’s loyalty point allocation rules to sum across programs
This approach avoids aggregation loss but requires two import jobs with proper sequencing.
Error Log Analysis
For the specific error you encountered, enable detailed logging in the import tool settings. The error “Duplicate entry ‘CUST-10234’ for key ‘customer_loyalty.PRIMARY’” indicates the import reached the second occurrence of that customer ID at row 23,456. With aggregation, this customer would appear only once, eliminating the constraint violation.
The preprocessing ETL step is critical - it’s not just about deduplication, but proper business logic aggregation that preserves data integrity while meeting SAP CX’s import requirements.