Forum Discussion

JohnHedges's avatar
JohnHedges
Frequent Visitor
4 years ago
Solved

Files from a folder - adding columns

Hi,   I have three csv files in a folder that Power BI is magically appending as a data source.   I want to add a couple of columns to the files, but everything I've tried to add them seems to br...
  • lbendlin's avatar
    lbendlin
    4 years ago

    Here is an example that combines all .csv files in C:\temp.  It assumes that the files have the same structure, and provides dummy column headers - you may want to provide your own.  It also doesn't strip out potentially existing column headers inside the files - you can filter those out if needed, or modify the code for the AddColumn accordingly.

     

    let
        Source = Folder.Files("C:\Temp"),
        #"Filtered Rows" = Table.SelectRows(Source, each ([Extension] = ".csv")),
        #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Custom", each Csv.Document([Content])),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6"}, {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6"})
    in
        #"Expanded Custom"