Forum Discussion

cheid_4838's avatar
cheid_4838
Icon for Helper IV rankHelper IV
1 year ago
Solved

How to remove headers when importing from folder

I have a folder containing multiple .CSV files that I had to daily and then refresh when a new file is added.  The problem is that each file contains a header for each column.  I am currently getting...
  • v-dineshya's avatar
    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
    PromotedHeaders

    Note: 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