DEV Community

M Maaz Ul Haq for DataSort

Posted on Originally published at datasort.app

Consolidating Multi-Store Shopify CSV Exports Without Breaking Relational Order Data

Running multiple Shopify storefronts across distinct regional markets offers significant scale, but it introduces major data engineering challenges when consolidating reporting. Month-end financial analysis requires aggregating raw CSV files across different regions into a unified schema for revenue and tax tracking.

Treating Shopify exports like flat spreadsheets often leads to broken financial calculations due to the non-trivial schema Shopify utilizes for multi-item orders.

Why Standard Spreadsheet Merging Breaks Shopify Order Data

A default Shopify orders_export.csv does not represent a single row per transaction. Instead, it exports one row per line item. If an order contains multiple items:

  • The order total, taxes, and shipping fees are populated only on the primary line row.
  • Subsequent rows leave monetary summary fields blank to prevent duplicate counting during aggregation.

When combining these files with rudimentary sheet merging or copy-paste operations, several structural issues arise:

  • Order ID Collisions & Missing Store Origin: Raw exports do not inherently contain the store name or identifier. Orders sharing common identifiers (e.g., #1001) from Store A and Store B collide.
  • Multi-Currency Inconsistencies: Regional stores export in local currencies (GBP, EUR, USD). Simply summing monetary columns without normalization distorts financial metrics.
  • Broken Aggregations: Basic pivot tables or database sums across merged line items often double-count or drop shipping and tax values depending on how empty secondary rows are treated.
  • Schema Drift: Custom application tags, locale-specific fields, or differing checkout configurations cause column headers to misalign across regional exports.

Limitations of VBA and Power Query

Historically, data teams relied on custom Visual Basic for Applications (VBA) macros or Power Query transformation steps to append regional datasets.

Sub MergeShopifyExports()
    Dim FolderPath As String, Filename As String
    Dim MasterSheet As Worksheet, TargetRow As Long
    FolderPath = "C:\ShopifyExports\"
    Filename = Dir(FolderPath & "*.csv")
    Set MasterSheet = ThisWorkbook.Sheets("MasterSales")
    TargetRow = MasterSheet.Cells(MasterSheet.Rows.Count, 1).End(xlUp).Row + 1

    Do While Filename <> ""
        ' Copy rows, append store source, handle offset columns
        Filename = Dir
    Loop
End Sub
Enter fullscreen mode Exit fullscreen mode

While functional, these methods break when:

  1. Column indices change due to newly installed store apps or updated Shopify export versions.
  2. Inconsistent date formats (e.g., MM/DD/YYYY vs. DD/MM/YYYY) lead to parsing errors during string-to-date conversion.

Programmatic Normalization: Python & Pandas

A resilient approach is to process exports via a dedicated script that enforces an explicit schema, extracts metadata from filenames, and aligns currency codes.

import pandas as pd
import glob
import os

path = "./exports"
all_files = glob.glob(os.path.join(path, "*.csv"))

dataframes = []

for filename in all_files:
    # Extract store identifier from filename (e.g., 'US_orders.csv' -> 'US')
    store_id = os.path.basename(filename).split("_")[0]

    df = pd.read_csv(filename, dtype={"Name": str, "Zip": str})
    df["Store_Identifier"] = store_id

    # Forward-fill order level fields for line-item grouping if required
    order_level_cols = ["Total", "Subtotal", "Shipping", "Taxes"]
    # Ensure existing columns match before forward filling
    valid_cols = [col for col in order_level_cols if col in df.columns]

    dataframes.append(df)

# Concatenate aligning on column names rather than position
master_df = pd.concat(dataframes, axis=0, ignore_index=True)

# Standardize datetime values
master_df["Created at"] = pd.to_datetime(master_df["Created at"], errors="coerce")

# Export consolidated dataset
master_df.to_csv("master_orders_consolidated.csv", index=False)
Enter fullscreen mode Exit fullscreen mode

Data Validation Checklist

Before loading consolidated data into a reporting pipeline or data warehouse, verify:

  1. Deduplication: Verify unique combinations of Store_Identifier and Order ID (or Name) to guard against duplicated webhook syncs or re-exported files.
  2. Type Casting: Preserve leading zeros in zip codes and SKU strings by enforcing string types during ingestion.
  3. Currency Conversion: Map orders against historical FX rate tables using the order timestamp before aggregating gross merchandise value (GMV).
  4. Handling Blank Fields: Distinguish between genuine null values (e.g., unfulfilled items) and intentional blank cells representing line-item continuations.

Top comments (0)