kkardynalski
New Member
- Joined
- Jul 11, 2015
- Messages
- 8
A coworker alerted me to a problem he was having combining some csv files. I had a look and found the problem but my solution is clunky so I am asking this question to get a more elegant solution.
First the problem:
note: I am not using parameters in these queries, the <filepath> and <filename> are actual text of the respective folder and file(s).
I have four csv files from an internet source, loaded to a network drive. When I open any and all of them with this code they display as expected, showing me 92 columns with headers in the first three rows and data starting on row 4:
Source = Csv.Document(File.Contents(" <filepath\filename.CSV> "),[Delimiter=",", Columns=92, Encoding=1252, QuoteStyle=QuoteStyle.None])
However, opening from folder I get different results: only one column with text in the first three rows and Errors for the rest with the message: "DataFormat Error: there were more columns in the result than expected. Details: Count = 1"
The code for that is:
Source = Folder.Files(" <filepath> "),
#"Added Custom" = Table.AddColumn(Source, "Custom", each Csv.Document([Content])),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Column1"}, {"Custom.Column1"})
If I run this query on another folder with different csv files from the same internet source, I get exactly the results I expected.
Then the investigation (really just to verify what I already suspected):
I opened the csv files in Notepad++. The first three lines in the ones that work have "heading,,,,,,," etc. and the ones that don't work have just " heading " .
Now two questions:
1. Obviously I can create a query for each file in the directory and then merge them if needed. But given that these files are downloaded from an internet site, and some files from that site work and some don't, how can I write a query that gives me all the data without a lot of headaches and error checking?
2. Why do the files with the single column header rows open perfectly when the query is "From File"?
Thanks for your suggestions!
Kathy
First the problem:
note: I am not using parameters in these queries, the <filepath> and <filename> are actual text of the respective folder and file(s).
I have four csv files from an internet source, loaded to a network drive. When I open any and all of them with this code they display as expected, showing me 92 columns with headers in the first three rows and data starting on row 4:
Source = Csv.Document(File.Contents(" <filepath\filename.CSV> "),[Delimiter=",", Columns=92, Encoding=1252, QuoteStyle=QuoteStyle.None])
However, opening from folder I get different results: only one column with text in the first three rows and Errors for the rest with the message: "DataFormat Error: there were more columns in the result than expected. Details: Count = 1"
The code for that is:
Source = Folder.Files(" <filepath> "),
#"Added Custom" = Table.AddColumn(Source, "Custom", each Csv.Document([Content])),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Column1"}, {"Custom.Column1"})
If I run this query on another folder with different csv files from the same internet source, I get exactly the results I expected.
Then the investigation (really just to verify what I already suspected):
I opened the csv files in Notepad++. The first three lines in the ones that work have "heading,,,,,,," etc. and the ones that don't work have just " heading " .
Now two questions:
1. Obviously I can create a query for each file in the directory and then merge them if needed. But given that these files are downloaded from an internet site, and some files from that site work and some don't, how can I write a query that gives me all the data without a lot of headaches and error checking?
2. Why do the files with the single column header rows open perfectly when the query is "From File"?
Thanks for your suggestions!
Kathy