Forum Discussion
How to remove headers when importing from folder
- 1 year ago
Hi cheid_4838 ,
Thank you for reaching out to the Microsoft Community Forum.
You want to combine all three files into one table with a single header row and no repeated headers from other files.
M code example you can paste directly into Power Query:
let
// Get all files in folder
Source = Folder.Files("C:\YourFolderPath"), // Change this to your folder path
// Keep only CSV files
CsvFiles = Table.SelectRows(Source, each Text.EndsWith([Extension], ".csv")),// Extract content from each file
AddContent = Table.AddColumn(CsvFiles, "Content", each Csv.Document(File.Contents([Folder Path] & [Name]), [Delimiter=",", Columns=3, Encoding=65001, QuoteStyle=QuoteStyle.None])),// Remove top row from each file (assumes it's the header)
RemoveTopRow = Table.TransformColumns(AddContent, {"Content", each Table.Skip(_,1)}),// Combine all tables together
CombineTables = Table.Combine(RemoveTopRow[Content]),// Use headers from one file (File1.csv in this case)
HeaderFile = Csv.Document(File.Contents("C:\YourFolderPath\File1.csv"), [Delimiter=",", Columns=3, Encoding=65001, QuoteStyle=QuoteStyle.None]),
PromotedHeaders = Table.PromoteHeaders(Table.Combine({HeaderFile, CombineTables}))
in
PromotedHeadersNote: Folder.Files(...) gets all CSV files. Each file is loaded, and the first row is skipped using Table.Skip. Then the contents of all files are combined into one table. Finally, headers from one known file (e.g., File1.csv) are used to promote column names correctly.
If my response has resolved your query, please mark it as the "Accepted Solution" to assist others. Additionally, a "Kudos" would be appreciated if you found my response helpful.
Thank you
It is rediculous that the Microsoft UI can't just have a radio button "All files contain the same header row please use that as the column titles and don't duplicate those as data rows". Having to dive into the guts of PowerBI to do something so simple isn't very efficient or streamlined.