xyz333
xyz333

Reputation: 768

Import csv file in a child folder with ChDir to get relative path Power Query

I have an xlsm file in a parent folder, and I want to import whatever CSV file is in a child folder regardless of its label

I want to use the relative path of the xlsm file to do this

I am trying:

FP = ChDir (CurrentProject.Path) & "\SourceData\"&"*.csv",
Source = Csv.Document(File.Contents(FP),[Delimiter=",", Columns=18, Encoding=1252, QuoteStyle=QuoteStyle.None])

But I am getting the error:

Expression.Error: The name 'ChDir' wasn't recognized. Make sure it's spelled correctly.

What is the best way to do this?

Thanks

Upvotes: 0

Views: 243

Answers (1)

horseyride
horseyride

Reputation: 21318

this combines all the CSV files in the parent and subdirectories

You can modify as needed

let Source = Folder.Files("C:\directory"),  
#"Filtered Rows" = Table.SelectRows(Source, each [Extension] = ".csv"), 
#"Added CSVdata" = Table.AddColumn(#"Filtered Rows", "CSVdata", each Table.PromoteHeaders(Csv.Document([Content])), type table),
#"Added Custom" =  Table.AddColumn(#"Added CSVdata", "Custom", each let name = [Name] in Table.AddColumn([CSVdata],"SourceFile", each name)),   
#"Combined CSVdata" = Table.Combine(#"Added Custom"[Custom])
in #"Combined CSVdata"

Upvotes: 1

Related Questions