· Par ExcelWorkflow · Power Query · 5 min de lecture

How to Combine Multiple Excel or CSV Files Automatically with Power Query

Step-by-step technical guide to consolidate dozens of monthly Excel or CSV files from a folder using Power Query. Eliminate manual copy-paste, handle schema drift, and build automated refresh pipelines.

How to Combine Multiple Excel or CSV Files Automatically with Power Query

The Recurring Multi-File Reporting Bottleneck

In corporate finance, supply chain, and operations departments, month-end closing frequently triggers a repetitive, error-prone chore: downloading regional or weekly export files, opening each workbook, copying data rows, and manually pasting them into a master consolidation spreadsheet.

This legacy manual workflow creates three structural vulnerabilities:
1. High Error Rate: Accidental omissions, duplicate pastes, misaligned column mappings, or broken cell formulas.
2. Operational Latency: Valuable analyst hours consumed by manual mechanics rather than variance analysis.
3. Fragility: The entire sheet breaks whenever an export adds an unexpected column or reorders fields.

Power Query, built directly into modern Excel (Data > Get Data) and Microsoft Power BI, solves this permanently via the From Folder connector.


Architectural Blueprint: The Folder Connector

Rather than hard-coding static references to specific filenames, Power Query monitors a target folder path (on local disk, network share UNC, or SharePoint/OneDrive library). Whenever a new monthly report is dropped into the folder, a single click on Refresh ingests, validates, standardizes, and appends the new records.

📁 C:\Data\Monthly_Sales\
   ├── Sales_2026_01.csv
   ├── Sales_2026_02.csv
   ├── Sales_2026_03.csv
   └── [New file dropped] ──► Click "Refresh Data" ──► Fully updated master dataset

Step 1: Source Repository and Reproducible Test Dataset

To follow along with a verified scenario, we will use three sales extract files exhibiting realistic schema variations (e.g., an extra discount column added in February).

Create a test folder C:\ExcelWorkflow\Sales\ and save the following two CSV files:

File 1: Sales_2026_01.csv

Date,Reference,Rep,Quantity,Amount,Region
2026-01-05,REF-101,Mark,12,1440.00,North
2026-01-12,REF-104,Sarah,5,850.50,South
2026-01-20,REF-102,David,8,960.00,West
2026-01-28,REF-101,Sarah,15,1800.00,South

File 2: Sales_2026_02.csv

Date,Reference,Rep,Quantity,Amount,Region,Discount
2026-02-03,REF-103,Mark,10,1200.00,North,0.05
2026-02-14,REF-104,David,7,1190.70,West,0.00
2026-02-22,REF-101,Sarah,20,2400.00,South,0.10

Observation: The February file introduces a new Discount attribute. We will ensure Power Query absorbs this column without failing.


Step 2: Ingest from Folder

  1. Open a blank Excel workbook.
  2. Navigate to Data > Get Data > From File > From Folder.
  3. Browse to C:\ExcelWorkflow\Sales\ and confirm.
  4. A preview dialog lists file metadata (Name, Extension, Date modified, etc.).
  5. Do not click Combine immediately. Click Transform Data.

Step 3: Filter Temporary Lock Files and Extensions

This step prevents the most common production crash: when a teammate has one of the Excel files open during refresh, Microsoft Excel creates a hidden lock file prefixed with ~$ (e.g., ~$Sales_2026_01.xlsx). If Power Query attempts to read this locked file, the refresh pipeline terminates.

In the Power Query Editor:
1. Click the filter dropdown on the Extension column > select only .csv (or .xlsx).
2. Click the filter dropdown on the Name column > Text Filters > Does Not Begin With > Enter ~$.
3. Select only the Content column (binary file payload) and the Name column (for file auditability).
4. Right-click the header > Remove Other Columns.


Step 4: Expand and Combine Binaries

  1. In the header of the Content column, click the Combine Files button (two downward arrows).
  2. Power Query analyzes the first file as a Sample File.
  3. Verify file origin (typically 65001: Unicode (UTF-8)) and delimiter (Comma).
  4. Click OK.

What Did Power Query Build Automatically?

In the left Queries pane, Power Query generates helper objects:
- Transform File from [Folder] group containing:
- Sample File: The reference structure.
- Parameter1: Injects each discovered file iteratively.
- Transform Sample File: The cleaning query evaluated against each individual file before appending.
- The primary consolidation query appending the transformed datasets.


Step 5: Resolve Heterogeneous Columns and Explicit Typing

Power Query joins tables by column header name, not physical column position:
- Since Discount did not exist in January, Power Query writes null for January rows and populates valid decimals for February.
- If headers vary slightly (e.g., Amount vs Total_Amount), normalize them inside the Transform Sample File query prior to combining.

Clean Production M Code

Here is the exact M expression executed in the main query:

let
    Source = Folder.Files("C:\ExcelWorkflow\Sales"),
    FilteredFiles = Table.SelectRows(Source, each not Text.StartsWith([Name], "~$") and [Extension] = ".csv"),
    KeptColumns = Table.SelectColumns(FilteredFiles, {"Content", "Name"}),
    InvokedTransform = Table.AddColumn(KeptColumns, "TransformedTable", each #"Transform Sample File"([Content])),
    RemovedContent = Table.RemoveColumns(InvokedTransform, {"Content"}),
    ExpandedData = Table.ExpandTableColumn(RemovedContent, "TransformedTable", 
        {"Date", "Reference", "Rep", "Quantity", "Amount", "Region", "Discount"}),
    TypedColumns = Table.TransformColumnTypes(ExpandedData, {
        {"Date", type date},
        {"Reference", type text},
        {"Rep", type text},
        {"Quantity", Int64.Type},
        {"Amount", type number},
        {"Region", type text},
        {"Discount", type number}
    })
in
    TypedColumns

Step 6: Test Dynamic Appending with a New Period

To verify pipeline resilience:
1. Save a new file into the directory: Sales_2026_03.csv.

Date,Reference,Rep,Quantity,Amount,Region,Discount
2026-03-02,REF-102,David,14,1680.00,West,0.00
2026-03-18,REF-101,Mark,9,1080.00,North,0.05
  1. In Excel, right-click anywhere inside the output table > Refresh (or press Alt + F5).
  2. March records appear appended beneath earlier periods immediately.

Production Deployment Checklist

  • [ ] Is the folder location accessible to all end-users via universal network paths (\\server\share\ or SharePoint sync) rather than local drive letters (C:)?
  • [ ] Is the temporary file filter (not Text.StartsWith([Name], "~$")) placed before binary expansion?
  • [ ] Are explicit types (type date, type number, Int64.Type) declared after column expansion?
  • [ ] Are missing optional columns handled gracefully (null values filled with 0 where appropriate)?

Frequently Asked Questions (FAQ)

Q: What happens if a file in the folder is corrupt?
A: Power Query fails the entire refresh. You can insulate the pipeline using the M construct try ... otherwise inside a custom column to quarantine corrupt binaries into an error log table while letting valid files load.

Q: Can this combine multi-sheet Excel workbooks?
A: Yes. In the Transform Sample File query, filter the navigation table to target the designated worksheet either by explicit name ([Item="DataSheet", Kind="Sheet"][Data]) or by sheet index.

Q: What is the maximum row capacity?
A: Traditional Excel sheets hold 1,048,576 rows. However, by loading the Power Query output directly into the Excel Data Model (Power Pivot) or Power BI Desktop, you can consolidate tens of millions of records with high compression.

Master Excel, Power Query & Power BI

Discover our practical courses designed to automate spreadsheets, clean data without code, and build audit-ready dashboards.

View Courses →