Forum Discussion
Batch Import multiple excel files with different column name from a folder
- 6 years ago
Hello k_mathana
this should be your code
give it a try and let us know if its working.
let Source = Folder.Files("C:\Users\k_mathana\Desktop\Sample"), #"Added Custom" = Table.AddColumn(Source, "Custom", each Excel.Workbook([Content], true)), #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Name", "Data", "Item", "Kind", "Hidden"}, {"Name.1", "Data", "Item", "Kind", "Hidden"}), ChangeColumnName =(tableint)=> let TransformTable = #table({"old", "new"},{{"Week ID2", "WEEK"}, {"Week ID", "WEEK"}}), FilterForWeek = Table.ToRows(Table.SelectRows(TransformTable, each List.Contains(Table.ColumnNames(tableint), _[old]))), RenameWeek = try Table.RenameColumns(tableint, FilterForWeek) otherwise tableint in RenameWeek, #"Filtered Rows" = Table.SelectRows(#"Expanded Custom", each ([Kind] = "Table")), GetAllColumnNames = List.Distinct(List.Combine(Table.TransformColumns(RenameColumnsData, {{"Data", each Table.ColumnNames(_)}})[Data])), RenameColumnsData = Table.TransformColumns ( #"Filtered Rows", { { "Data", (tableint)=> ChangeColumnName(tableint) } } ), #"Removed Other Columns" = Table.SelectColumns(RenameColumnsData,{"Data"}), #"Expanded Data" = Table.ExpandTableColumn(#"Removed Other Columns", "Data", GetAllColumnNames, GetAllColumnNames) in #"Expanded Data"In this line you can maintain your names to be renamed
The first value specifies the old, the second the new one. Be aware that Power query is case sensitive
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy - 5 years ago
Hello @k_mathana
this should be your code
give it a try and let us know if its working.
let Source = Folder.Files("C:\Users\k_mathana\Desktop\Sample"), #"Added Custom" = Table.AddColumn(Source, "Custom", each Excel.Workbook([Content], true)), #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Name", "Data", "Item", "Kind", "Hidden"}, {"Name.1", "Data", "Item", "Kind", "Hidden"}), ChangeColumnName =(tableint)=> let TransformTable = #table({"old", "new"},{{"Week ID2", "WEEK"}, {"Week ID", "WEEK"}}), FilterForWeek = Table.ToRows(Table.SelectRows(TransformTable, each List.Contains(Table.ColumnNames(tableint), _[old]))), RenameWeek = try Table.RenameColumns(tableint, FilterForWeek) otherwise tableint in RenameWeek, #"Filtered Rows" = Table.SelectRows(#"Expanded Custom", each ([Kind] = "Table")), GetAllColumnNames = List.Distinct(List.Combine(Table.TransformColumns(RenameColumnsData, {{"Data", each Table.ColumnNames(_)}})[Data])), RenameColumnsData = Table.TransformColumns ( #"Filtered Rows", { { "Data", (tableint)=> ChangeColumnName(tableint) } } ), #"Removed Other Columns" = Table.SelectColumns(RenameColumnsData,{"Data"}), #"Expanded Data" = Table.ExpandTableColumn(#"Removed Other Columns", "Data", GetAllColumnNames, GetAllColumnNames) in #"Expanded Data"In this line you can maintain your names to be renamed
The first value specifies the old, the second the new one. Be aware that Power query is case sensitive
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Yes ziying35 I can understand what you are trying to say. But what if I can maintain the information some where for example what ever like "Week ID", "WeekID", "week", "WEEK", "ID" should read as "Week" and before transforming in to table the rename should happen. I can't correct this in the base files because they are maintained by different departments and more over it is over 1500 excel file which accumulated over the years.
The best you can hope to do I believe k_mathana is do a separate process per department format, then combine the harmonized format at the end.
If that isn't helpful, what exactly are you requesting we help with? RIght now I am getting a "I need a crystal ball transformation" vibe. 😂
- k_mathana6 years ago
Helper II
edhans I am well of aware of given solution, I was seeking for a way to rename a column before we transform the data.
- edhans6 years ago
Community Champion
Column renames are easy k_mathana of course. The trick is figuring out the column name. You can do this with the Table.ColumnNames() function to get the column name, but that is usually based on the index of the column. However, you said the columns are not consistently located. Sometimes it is in Column 1, others Column 5, etc. You may also not be consistenly starting on a specific row.
It becomes very tricky. Like I said, it can be done, but would be very tedious coding, and I suspect you'd have to modify it every few weeks as someone invented a new column name. "Week1 2020" for example because that is what they felt like doing on a given day.