Sub topic 17
Combining All Files in a Folder
This chapter assumes you can import a CSV file, set data types, and append two queries with Table.Combine. If a new CSV lands in a folder every month, you can combine every file in one query instead of appending each by hand.
How the folder connector works
The From Folder connector lists every file in a folder as a table. Each row is one file, with columns such as Content (the file's binary data), Name, Extension, and Folder Path. The combine step then reads each Content value, parses it, and appends the results.
To start, choose Data > Get Data > From File > From Folder, then pick the folder. Excel shows a preview of the file list.
Combine the files
- In the preview, choose Combine & Transform Data.
- In the dialog, pick a sample file. Power Query uses it to work out the layout.
- Check the delimiter and encoding. Choose OK.
Power Query creates several items in the Queries pane:
- Sample File, Parameter1, and Transform Sample File: helper queries that describe how to read one file.
- Transform File: a function that applies those steps to any file.
- Your main query, which calls the function on every row and appends the results.
Any step you add to Transform Sample File, such as setting types or trimming text, applies to every file. Edit cleaning steps there. Edit them in the main query only for steps that should run after the files are combined.
What the main query does
Open the Advanced Editor on the main query. The key step looks like this:
let
Source = Folder.Files("C:\Data\Monthly"),
OnlyCsv = Table.SelectRows(Source, each [Extension] = ".csv"),
Invoked = Table.AddColumn(OnlyCsv, "Data", each #"Transform File"([Content])),
Combined = Table.Combine(Invoked[Data])
in
CombinedThe query lists the files, keeps only CSV files, runs the Transform File function on each file's Content, and appends the resulting tables. The generated query also adds a Source.Name column so you can see which file each row came from. This hand-written version leaves that column out.
Common errors
- Other files in the folder. A temporary file such as
~$sales.xlsxor a stray.txtfile breaks the combine. Filter onExtensionbefore you combine. - Columns that differ between files.
Table.Combinematches columns by name. A renamed column creates a new column full of nulls for the other files. Keep headers consistent. - Wrong sample file. The function copies the sample's layout. If the sample has an extra header row, every file gets that fix applied.
- Header rows repeated. If each file's first row is promoted separately, no stray header rows appear in the data. If you see header text in the rows, the promote step ran after combining. Move it into Transform Sample File.
Refreshing
When you add a new file to the folder and choose Refresh, the file list updates and the new file is combined automatically. No query edit is needed.
Exercise
A combined query has a column of nulls that was not in your data. One monthly file has "Amnt" where the others have "Amount". Why does this happen, and where do you fix it?
Show answer
Table.Combine matches columns by name, so "Amnt" becomes a separate column and "Amount" is null for that file's rows. Fix it by renaming the column to "Amount" in the Transform Sample File steps, or in the function, so the rename applies before the files are combined. Alternatively, correct the header in the source file.