Forum Discussion
Power Query - Csv.Document - Select specific columns from csv file
- 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.
Hi, rohit_singh. I thank you for the fast and helpful answers. I believe I am using the correct delimiter (which is semicolon), as when I try another one (such as comma) it entirely messes up the data. I have tried this on another dataset and the result is the same: it always imports the first N columns, regardless of the columns names I've listed. You can see in the Excel Screenshot that the values of the column "CO_SH6" are not the ones that are imported in the Power Query. I don't know how to proceed with this.
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.