Excel Power Query Merge Rows: Step-by-Step Guide
In this article
Merging rows in Excel Power Query is a powerful technique used to consolidate data from multiple sources or to reshape your dataset by combining related records into single entries. This process, often referred to as "Grouping" or "Appending," allows you to summarize large datasets, remove duplicates, or stack tables vertically without manually copying and pasting data. Before you begin, ensure that your source data is clean, with consistent headers and no merged cells within the raw data range, as Power Query relies on structured table formats to function correctly.
What You'll Need
- A Microsoft Excel workbook containing the data you wish to merge (ensure it is formatted as an Excel Table).
- Access to the Power Query Editor (available in Excel 2016 and later, or via add-in for older versions).
- A clear understanding of whether you want to append rows (stack tables) or group rows (summarize data).
Step-by-Step
- Prepare Your Data: Select your data range in Excel and press
Ctrl + Tto convert it into an official Excel Table. Name the table logically (e.g., "SalesData") using the Table Design tab. Repeat this for any additional tables you intend to merge. - Load Data into Power Query: Go to the Data tab on the ribbon. Click Get Data > From Table/Range. This opens the Power Query Editor window, displaying your data in a preview pane.
- Choose Your Merge Strategy: Decide if you are stacking tables (Append) or summarizing rows (Group By). For most "merge rows" queries involving distinct datasets, you will use Append. If you are consolidating duplicate entries into totals, use Group By.
- To Append Tables (Stack Rows): In the Power Query Editor, go to the Home tab and click Append Queries. Choose Append Queries as New if you want a separate result, or Append Queries to add to the current query. Select the second table from the dropdown menu. Ensure the columns align correctly; Power Query matches columns by name, not position.
- To Group Rows (Summarize): If merging involves combining duplicate rows, select the column(s) you want to group by. Go to the Transform tab and click Group By. Define the aggregation operation (e.g., Sum, Count, Average) for the value columns. This collapses multiple rows into one per unique key.
- Review and Adjust: Check the applied steps in the right-hand pane. Ensure no errors appear in the preview grid. If columns have different names, rename them in the source queries before appending to ensure they match.
- Close and Load: Click Close & Load in the top-left corner. The merged data will be inserted into a new worksheet or specified location in your Excel workbook.
Common Mistakes
One frequent error is attempting to merge tables where column headers do not match exactly. Power Query is case-sensitive and whitespace-sensitive; "Sales Amount" and "sales amount" will not merge automatically. Another common issue is forgetting to convert raw ranges into Excel Tables, which can cause reference errors when refreshing the query. Users also often confuse "Merge Queries" (which joins tables based on a key, like SQL Join) with "Append Queries" (which stacks rows), leading to unexpected results.
Tips
Always keep your source data in separate sheets or files rather than mixing raw data with calculations. Use descriptive names for your queries in the Power Query Editor to easily identify them during future edits. If you frequently merge data, consider creating a helper query that cleans the data first, then reference that cleaned query in your final merge step. This modular approach makes troubleshooting much easier.
Troubleshooting
If your merged data shows null values or errors, check the column types in the Power Query Editor. Mismatched data types (e.g., text vs. number) can prevent proper grouping or appending. Right-click the column header and select Change Type to enforce consistency. If the refresh fails, verify that the source file paths have not changed. Moving source files breaks connections; update the source path in the Applied Steps pane under Source. If performance is slow, disable background refresh in the connection properties.
Frequently Asked Questions
Can I merge more than two tables at once?
Yes. When using Append Queries, you can select multiple tables in the dialog box. Alternatively, you can chain append steps, adding one table at a time to the existing query.
What happens if column names differ slightly?
Power Query will treat them as separate columns, resulting in null values for missing data. You must rename the columns in the source queries to match exactly before performing the append or merge operation.
Is there a difference between Merge and Append?
Yes. Append adds rows from one table to another (vertical combination). Merge combines columns from two tables based on a matching key (horizontal combination, similar to VLOOKUP). Use Append for stacking data and Merge for joining related fields.
Do I need to save my workbook before running the query?
It is best practice to save your workbook before loading data into Power Query. This ensures that the file paths referenced in the queries remain valid and stable during the refresh process.
Can I undo a merge after closing Power Query?
Yes. Reopen the query in the Power Query Editor by going to Queries & Connections, right-clicking the query, and selecting Edit. You can delete the specific step from the Applied Steps pane to revert the change.