Forum Discussion

mgartell's avatar
mgartell
Regular Visitor
4 years ago
Solved

Power Query - Csv.Document - Select specific columns from csv file

Hi! I need help with the Csv.Document function from Power Query. I would like to import only specific columns from a CSV file, using the Column argument. However, when using this argument, I alway...
  • AlexisOlson's avatar
    AlexisOlson
    4 years ago

    Typically, when loading a CSV, it looks like this before promoting headers:

    Or like this after promoting headers:

     

    If you use the Columns argument in Csv.Document, it interprets a list of column names not as which columns to select but what the column names are (useful if you don't have headers in the file) and assumes you are listing them in order from left to right.

     

    Columns: Can be null, the number of columns, a list of column names, or a table type. If the number of columns is lower than the number found in the input, the additional columns will be ignored. If the number of columns is higher than the number found in the input, the additional columns will be null. When not specified, the number of columns will be determined by what is found in the input.

    I'd recommend not using this argument but rather selecting the columns you want after promoting headers. You can use the Choose Columns button for this and the query should end up looking like this:

    let
        Source = Csv.Document(File.Contents("C:\Users\aolson\Downloads\Sample.csv")),
        #"Promoted Headers" = Table.PromoteHeaders(Source),
        #"Removed Other Columns" = Table.SelectColumns(#"Promoted Headers", {"CO_NCM", "CO_UNID", "CO_PPI"})
    in
        #"Removed Other Columns"

    Use whatever list of column names you prefer instead of the three I have in my example above.