Forum Discussion
Combine multiple files in Power query editor
Based on your screenshot,
a) Change the "Transform file" query M code, as below code (Power Query Window > Home tab > Advanced Editor)
let
Source = (Parameter1) => let
Source = Csv.Document(Parameter1,[Delimiter=",", Columns=3, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Added Index" = Table.AddIndexColumn(Source, "File Index", 1, 1, Int64.Type)
in
#"Added Index"
in
Source
b) Change the "PowerBI" query M code , ... in the advanced editor
delete all lines from this line #"Invoke Custom Function1" till the end and copy the below code. This will give the lines you want.
FYI, I also removed empty data lines , as it wont be of any use
#"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files2", "Transform File", each #"Transform File"([Content])),
#"Removed Other Columns1" = Table.SelectColumns(#"Invoke Custom Function1", {"Name", "Transform File"}),
#"Expanded Transform File" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File", {"Column1", "Column2", "Column3", "File Index"}, {"Column1", "Column2", "Column3", "File Index"}),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Transform File",{{"File Index", Int64.Type}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Custom", each if Text.Contains([Column1], "Pid") then "Table 1 Row 1" else if Text.Contains([Column1], "Order placed") then "Table 2 begin" else null),
#"Duplicated Column" = Table.DuplicateColumn(#"Added Conditional Column", "Custom", "Custom - Copy"),
#"Sorted Rows" = Table.Sort(#"Duplicated Column",{ {"Name", Order.Ascending}, {"File Index", Order.Ascending}}),
#"Filled Down" = Table.FillDown(#"Sorted Rows",{"Custom"}),
#"Filtered Rows3" = Table.SelectRows(#"Filled Down", each ([Custom] = "Table 1 Row 1")),
#"Trimmed Text" = Table.TransformColumns(#"Filtered Rows3",{{"Column1", Text.Trim, type text}, {"Column2", Text.Trim, type text}, {"Column3", Text.Trim, type text}}),
#"Filtered Rows4" = Table.SelectRows(#"Trimmed Text", each ([Column1] <> null and [Column1] <> "") and ([#"Custom - Copy"] = null)),
#"Renamed Columns" = Table.RenameColumns(#"Filtered Rows4",{{"Column1", "Pid"}, {"Column2", "Pname"}, {"Column3", "Amt"}, {"Name", "Source"}}),
#"Removed Other Columns" = Table.SelectColumns(#"Renamed Columns",{"Source", "Pid", "Pname", "Amt"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Other Columns",{"Pid", "Pname", "Amt", "Source"})
in
#"Reordered Columns"
Thanks, sevenhills, your solution is working 100 % exactly, how I was expecting. But, later I realized the files uploaded by the business is not a CSV file, it's a .xls file, and when I tried to modify the logic as per excel, it is not working as expected, I am sure there is some link which I am missing as I am not that much good at M-query 😞
But, still, I am trying. I use the below the code change
let
Source = (Parameter1) => let
Source = Excel.Workbook(File.Contents("C:\Users\v-amkm\P1.xls"), null, true),
#"Added Index" = Table.AddIndexColumn(Source, "File Index", 1, 1, Int64.Type)
in
#"Added Index"
in
Source
I have uploaded the files to the same location (https://drive.google.com/drive/folders/1IbpAobUzsmHSwldGibVzfDUqHEglo_3z?usp=sharing)
Looking back to hear from your side
Thanks,