Sub topic 5
Combining with Combine & Transform Data
This chapter assumes you can open the Power Query editor, connect to a folder, and filter the file list by extension and name. It covers the command that merges the files, what the command asks you, and what it produces.
What the command does
After you connect to a folder, Power Query shows the file list. Each row is one file, and the Content column holds the file as binary data. Combine & Transform Data turns that list into one table. It reads every file, applies the same steps to each one, and stacks the results.
You reach it in two ways:
- Choose Data > Get Data > From File > From Folder, pick the folder, and select Combine > Combine & Transform Data in the preview window.
- In the Power Query editor, click the Combine Files button (the double-arrow icon) in the header of the
Contentcolumn.
The Combine > Combine & Load option does the same work but loads the result straight to a worksheet. Use Combine & Transform Data when you want to clean the data first.
The Combine Files dialog
The dialog shows a preview built from one file. You set three things:
- Example File. The file Power Query uses as the model for all the others. By default it is the first file in the list.
- The object to extract. For a CSV file this is the file itself. For a workbook, you pick a sheet or table. Every other workbook must contain an object with the same name.
- File origin, delimiter and data type detection. For CSV files, these settings control the encoding, the separator, and how many rows Power Query samples to guess types.
Click OK, and Excel opens the editor with the combined result.
What you get
The command adds a group of queries to the Queries pane:
| Query | Type | Role |
|---|---|---|
| Parameter (for example `Parameter1`) | Parameter | Holds the example file's binary content |
| `Sample File` | Table | Takes the first file from the parameter |
| `Transform Sample File` | Table | The steps that clean one file |
| `Transform File` | Function | Runs those steps on any file |
| Your folder query | Table | Calls the function on every row and stacks the results |
The names can vary slightly between Excel versions. The important part is the pattern. You clean one sample file, and the function applies those steps to all files.
What the combined query looks like
The combined query now has a few new columns and steps. It adds a Source.Name column with the file name. It calls the function on each Content value and expands the resulting tables. The other file columns, such as Date modified, are removed.
Run a quick check before you continue:
- Scroll to the bottom of the data and confirm that rows from the last file appear.
- Filter
Source.Nameto see each file name once, or group by it and count rows. - Confirm the header row appears only once. A repeated header row inside the data means a file was read without promoting its headers.
Common errors at this step
- Temporary files. An open Excel workbook creates a hidden
~$name.xlsxfile in the folder. It is not a real workbook, so the combine fails on it. Filter these files out before you combine. - Wrong example file. If the first file is unusual, choose a typical file in the dialog.
- Subfolder files.
Folder.Filesincludes files from subfolders. Check theFolder Pathcolumn if you see rows you did not expect.
Exercise
You combined a folder of monthly CSV files. The result has a column header repeated as a data row in the middle of the table, and the column names are Column1, Column2 and so on in one file. Which part of the generated setup should you inspect first, and why?
Show answer
Inspect the Transform Sample File query. Its steps run on every file, so a missing or wrong header step there produces repeated or generic headers across the combined data. Fix it once in that query, and the change applies to all files.