DEV Community

M Maaz Ul Haq for DataSort

Posted on Originally published at datasort.app

Automating Excel Merges with Power Automate: A Deep Dive into Workflow Automation and Data Cleaning

Dealing with multiple Excel or CSV files is a common task in business. Whether it is sales reports from different regions, customer data across various campaigns, or financial records by month, the need to combine these files into a single, cohesive dataset is constant. Manually copying and pasting or even using complex formulas can quickly become a time sink, prone to errors, and a source of frustration.

What if you could automate this repetitive process? Imagine setting up a workflow that automatically merges your Excel files for you. That is where Microsoft Power Automate comes in. And when your data is messy, inconsistent, or riddled with duplicates, AI tools can take your automation to the next level.

This guide will walk you through building robust workflows in Power Automate to combine your Excel workbooks. We will also explore how AI can address the often overlooked challenge of data cleaning and standardization, turning disparate data into a clean, unified source.

Why Automate Excel Merges?

The benefits of automating Excel data consolidation extend beyond simply saving time. Consider these advantages:

  • Time Savings: Free up hours spent on manual data handling, allowing you to focus on analysis and strategic tasks.
  • Reduced Errors: Eliminate human error from copy-pasting, formula mistakes, or missing data.
  • Consistency: Ensure data is merged and formatted uniformly every time, regardless of who runs the process.
  • Scalability: Easily handle increasing volumes of files without proportional increases in manual effort.
  • Timeliness: Get up-to-date consolidated reports faster, enabling quicker decision-making.

The Old Way: Manual Merges and VBA Limitations

For years, consolidating data meant either painstaking manual copy-pasting, using VLOOKUP or INDEX/MATCH across sheets, or resorting to VBA (Visual Basic for Applications) scripts. Manual methods are slow and error-prone, especially with large datasets or many files.

VBA offered a significant improvement, providing custom macros to automate repetitive tasks within Excel. A typical VBA script for merging files might loop through a folder, open each workbook, copy its data, and paste it into a master sheet. While powerful, VBA has its own set of challenges:

  • Requires Coding Skills: Writing and debugging VBA requires specific programming knowledge.
  • Maintenance Overhead: Scripts need updating if file paths, sheet names, or data structures change.
  • Limited Integration: Primarily lives within Excel. Integrating with other applications like SharePoint, email, or cloud storage is complex or impossible without additional tools.
  • Security Concerns: Macros can sometimes be flagged as security risks, requiring users to enable content.
Sub MergeExcelFiles()
    Dim folderPath As String
    Dim fileName As String
    Dim wbMaster As Workbook
    Dim wsMaster As Worksheet
    Dim wsData As Worksheet
    Dim lastRow As Long

    ' Set the path to the folder containing your Excel files
    folderPath = "C:\Your\Data\Folder\"

    ' Create a new master workbook
    Set wbMaster = Workbooks.Add
    Set wsMaster = wbMaster.Sheets(1)
    wsMaster.Name = "CombinedData"

    ' Get the first file in the folder
    fileName = Dir(folderPath & "*.xlsx")

    ' Loop through all Excel files in the folder
    Do While fileName <> ""
        If fileName <> wbMaster.Name Then ' Avoid merging the master itself if it's in the same folder
            With Workbooks.Open(folderPath & fileName)
                Set wsData = .Sheets(1) ' Assuming data is on the first sheet
                lastRow = wsMaster.Cells(Rows.Count, 1).End(xlUp).Row

                ' Copy header only once from the first file, then copy data only
                If lastRow = 1 And wsMaster.Cells(1,1) = "" Then ' Check if master is empty
                    wsData.UsedRange.Copy wsMaster.Cells(1, 1)
                Else
                    wsData.UsedRange.Offset(1).Copy wsMaster.Cells(lastRow + 1, 1)
                End If

                .Close SaveChanges:=False
            End With
        End If
        fileName = Dir ' Get the next file
    Loop

    MsgBox "Files merged successfully!"
End Sub
Enter fullscreen mode Exit fullscreen mode

This VBA example demonstrates the logic, but it needs careful handling of headers, different sheet names, and error cases. It also runs only when explicitly triggered within Excel.

Automating Excel Merges with Power Automate: A Step-by-Step Guide

Power Automate (formerly Microsoft Flow) provides a low-code, cloud-based platform to automate workflows across various applications and services, including Excel. It excels at connecting different systems and orchestrating actions, making it ideal for automating file consolidation from diverse sources.

Here, we will outline two common scenarios for merging Excel files using Power Automate.

Scenario 1: Merging Files from a SharePoint Folder

This is a frequent need, especially for teams collaborating on data. Your Excel files might be stored in a shared SharePoint library, and you want to combine them automatically into a master file.

  • 1. Choose a Trigger: Start with a trigger. Common choices are 'Manually trigger a flow' for ad-hoc runs, or 'When a file is created or modified (properties only)' if you want the merge to happen whenever new data is added to your SharePoint folder. For scheduled merges, use 'Schedule recurrences'.
  • 2. Get Files from SharePoint: Use the 'Get files (properties only)' action from the SharePoint connector. Specify the Site Address and Library Name where your Excel files are located. This action retrieves metadata about the files.
  • 3. Initialize an Array Variable: Add an 'Initialize variable' action. Name it something like ExcelData, set its Type to 'Array', and leave its initial Value blank. This array will store the data from all your Excel files.
  • 4. Apply to Each File: Insert an 'Apply to each' control. For its 'Select an output from previous steps' field, choose the 'value' dynamic content from the 'Get files (properties only)' action. This will loop through each file found.
  • 5. Get Excel Table Rows: Inside the 'Apply to each' loop, add the 'List rows present in a table' action from the Excel Online (Business) connector. You will need to provide:
    • Location: The SharePoint site where your file is located.
    • Document Library: The library containing the file.
    • File: Use the 'Id' dynamic content from the SharePoint 'Get files' action.
    • Table: This is crucial. If your Excel files have data structured as a named Excel Table (recommended!), enter the table name (e.g., 'Table1'). If not, you may need to use 'Get file content' and then parse CSV or infer table.
  • 6. Append Data to Array: After 'List rows present in a table', add another 'Apply to each' loop. Its input should be the 'value' (rows) from the 'List rows present in a table' action. Inside this inner loop, use the 'Append to array variable' action. Set 'Name' to ExcelData and 'Value' to the 'Current item' dynamic content from the inner loop. This adds each row of the current Excel file to your main array.
  • 7. Create Consolidated Excel File: Outside the 'Apply to each' loops, add a 'Create file' action from the SharePoint connector. Specify where you want the new merged file to be saved. For 'File Content', you will need to convert your array variable into a CSV or JSON string. A common method is to use a 'Create CSV table' or 'Create HTML table' action, or even a 'Select' action to transform the array into a format suitable for a final Excel file.

Scenario 2: Merging Email Attachments

Often, reports or data come in as email attachments. Power Automate can automatically process these, too.

  • 1. Choose a Trigger: Use 'When a new email arrives (V3)' from the Outlook connector. Configure filters (e.g., 'From', 'Subject contains', 'Has Attachment' set to Yes) to target specific emails.
  • 2. Apply to Each Attachment: Inside the trigger, add an 'Apply to each' loop. For its input, select 'Attachments' from the email trigger.
  • 3. Filter for Excel Files: Inside the loop, add a 'Condition' action. Check if 'Attachment Name' (from the trigger) 'ends with' '.xlsx' or '.xls'.
  • 4. Get Excel Table Rows (if Excel file): In the 'If yes' branch of the condition, use 'Create file' in a temporary SharePoint or OneDrive folder (using 'Attachment Name' and 'Attachment Content' from the trigger). Then, use the 'List rows present in a table' action on this temporary file, similar to Scenario 1.
  • 5. Append Data: Use 'Append to array variable' as described in Scenario 1.
  • 6. Clean Up (Optional but Recommended): After processing the file, delete the temporary file from SharePoint/OneDrive.
  • 7. Create Consolidated Excel File: Outside all loops, create your final consolidated Excel file, similar to Scenario 1.

Key Power Automate Actions for Excel

  • List rows present in a table: Reads all rows from a specified table in an Excel workbook.
  • Add a row into a table: Appends a single row of data to an Excel table.
  • Create file (SharePoint/OneDrive): Creates a new file from content, useful for generating the merged output.
  • Get file content (SharePoint/OneDrive): Retrieves the binary content of a file.
  • Create CSV table: Converts an array of JSON objects into a CSV formatted string.
  • Get files (properties only) / Get file content: For retrieving files from cloud storage.

For more in-depth documentation on Power Automate and Excel, refer to the official Microsoft Power Automate Excel actions reference. Additionally, the Power Automate Getting Started guide is an excellent resource for new users.

When Power Automate Isn't Enough: The Data Cleaning and Standardization Challenge

While Power Automate is excellent at orchestrating file operations and moving data, it has limitations when it comes to actively cleaning or transforming messy data beyond basic operations. If your source Excel files have inconsistencies, then simply merging them will result in a messy consolidated file. Common issues include:

  • Inconsistent date formats (e.g., 'MM/DD/YYYY' vs 'DD-MM-YY').
  • Variations in text fields (e.g., 'New York', 'NY', 'nyc').
  • Duplicate records across different files or within the same file.
  • Missing values that need intelligent imputation.
  • Different column names for the same type of data (e.g., 'Customer ID' vs 'Client_ID').
  • Extra spaces, special characters, or incorrect data types.

These challenges often require manual intervention or complex scripting, which defeats the purpose of automation. This is where AI-powered tools provide a significant advantage.

Beyond Power Automate: Addressing Data Cleaning and Standardization with AI

This is where AI-powered solutions can truly shine. Advanced AI can intelligently clean, sort, and merge messy Excel and CSV files instantly, going beyond simple concatenation to understand your data and provide a truly clean and unified output.

How AI Can Enhance Your Merging Process

AI solutions take on the heavy lifting of data quality, transforming raw data into actionable insights:

  • Intelligent Data Cleaning: AI-powered cleaning tools can automatically detect and fix common errors, standardize formats, correct spellings, and handle missing values, all without manual rules.
  • Smart Merging Capabilities: Intelligent merging capabilities can align data even if column names differ or schemas are slightly mismatched, understanding the context to ensure accurate merges.
  • Automated Duplicate Removal: Before or during a merge, automated duplicate removal features can identify and eliminate redundant entries, providing a clean, unique dataset.
  • Data Standardization: Data standardization, often combined with AI cleaning, ensures data is uniform and ready for analysis.
  • Instant Results: Upload your files and let AI do the work in seconds, not hours or days.

Instead of writing complex VBA or Power Automate expressions to handle every tiny data inconsistency, AI solutions can understand the intent and clean it for you. This means your merged data is not just combined, but also high-quality and reliable.

Combining Power Automate with AI for Ultimate Efficiency

The most powerful approach is to use Power Automate for what it does best, orchestrating file movement and triggers, and then leverage AI for its superior data cleaning and intelligent merging capabilities. You can set up a workflow where:

  • Power Automate automatically gathers new Excel or CSV files from email attachments, SharePoint, or other cloud storage.
  • These files can then be fed into an AI-powered data cleaning and merging system for standardization and intelligent consolidation.
  • The AI system outputs a pristine, consolidated Excel or CSV file.
  • Power Automate then takes this cleaned and merged file and saves it to its final destination, uploads it to a database (perhaps after converting it to SQL or JSON for further integration), or sends it as an email attachment.

This hybrid approach gives you the best of both worlds: automated workflow orchestration with intelligent, AI-driven data quality and merging.

By combining the robust automation capabilities of Power Automate with the intelligence of AI for data cleaning and transformation, developers and data professionals can create powerful, resilient data workflows. This hybrid approach ensures not only efficient data consolidation but also high-quality, reliable data ready for analysis and reporting.

Top comments (1)

Collapse
 
crdtcto profile image
Kane Lim

Hello Glad to see you, I am Kane Lim from Hong Kong. I have over 10 years of development experience. I am writing this because your post was interesting.

The hybrid architecture is the right direction, but I would avoid letting an LLM become the source of truth for data transformation. I would build a schema inference layer first, generating a canonical contract with typed fields, normalization rules, provenance, and confidence scores. Deterministic transformations should handle dates, identifiers, nullability, and formatting, while AI handles semantic column mapping, entity resolution, and ambiguous duplicates.

For Power Automate, I would make ingestion event driven and push normalized payloads into a queue before processing. That gives you idempotency, replayability, dead letter handling, and scalable parallelism. Every transformed record should retain source file, row identifier, transformation version, and validation status.

The strongest architecture is therefore orchestration, deterministic validation, AI assisted semantic resolution, then transactional persistence. That turns a fragile Excel workflow into an auditable data pipeline.

I would be glad to exchange ideas on building this into a production grade ingestion platform.