excelcsvpowerqueryimport-from-csvget-transform

Import selected columns from .csv files in Power Query


I'm looking to import data with known required column headers, from a variable list of .csv files, some of which have columns I don't need. For example:

File 1 maybe has: NAME, ADDRESS, EMAIL

File 2 has: NAME, ADDRESS, EMAIL, CELL_PHONE

I need to append all csv files into a single table but I only want NAME, ADDRESS, and EMAIL data.

This needs to be a dynamic solution because in future the data supplier may add other new fields in their .csv files, and this is out of my control.


Solution

  • You can combine data from your CSV files then select the columns you want to keep:

    let
        Source = Folder.Files("C:\MyFolder\"),
        #"Filtered Rows" = Table.SelectRows(Source, each [Extension] = ".csv"),
        #"Added CSVdata" = Table.AddColumn(#"Filtered Rows", "CSVdata", each Table.PromoteHeaders(Csv.Document([Content])), type table),
        #"Combined CSVdata" = Table.Combine(#"Added CSVdata"[CSVdata]),
        #"Selected Columns" = Table.SelectColumns(#"Combined CSVdata",{"Project ID", "Material ID", "Material Description"})
    in
        #"Selected Columns"