Forum Discussion
Expanding Multiple Data Tables with Unknown Column Names
- 8 years ago
True :)
Your syntax would only work if you would reference a column that contains the column names already like here for example: https://www.mrexcel.com/forum/power-bi/952568-power-query-expand-all-columns.html
But in your case you have to include the Table.ColumnNames function to retrieve the column names first. If you want to include it into one step, it could look like so:
#"Expanded Data" = Table.ExpandTableColumn(#"Filtered Rows", "Data", List.Union(List.Transform(#"Filtered Rows"[Data]), each Table.ColumnNames(_)))
Hi Anonymous ,
not sure I can follow what you're describing here. Please post your M-code so that I can follow.
Below code works fine if I load data from excel i.e. selecting excel Data.xlsx
let
Source = Excel.Workbook(File.Contents(""E:\Project\TestFiles\Data.xlsx"), null, true),
#"Added Custom" = Table.AddColumn(Source, "PromoteHeader", each Table.PromoteHeaders([Data])),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}),
DistinctColumn = List.Union(List.Transform(Table.Column(#"Removed Columns",Table.ColumnNames(#"Removed Columns"){0}), each Table.ColumnNames(_))),
ExpandTable =Table.ExpandTableColumn(#"Removed Columns","PromoteHeader",DistinctColumn,DistinctColumn)
in
ExpandTable
But it is too much slow at last line if I load data from folder( in status bar it shows loding more than 500Mb data where as i have only 1 file in folder with size 1Mb. I have tried using Table.Buffer as well but it also runs slow.
let
Source = Folder.Files("E:\Project\TestFiles"),
Step1 = Table.SelectColumns(Source,{"Content"}),
Step2 = Table.AddColumn(Step1, "Custom", each Excel.Workbook([Content])),
Step3 = Table.SelectColumns(Step2, {"Custom"}),
Step4 = Table.ExpandTableColumn(Step3, "Custom", {"Name", "Data"}, {"Name", "Data"}),
Step5 = Table.RemoveColumns(Step5 ,{"Name"}),
Step6 = Table.AddColumn(Step6, "PromoteHeader", each Table.PromoteHeaders([Data])),
Step7 = Table.SelectColumns(Step6,{"PromoteHeader"}),
DistinctColumn = List.Union(List.Transform(Table.Column(Step7,Table.ColumnNames(Step7){0}), each Table.ColumnNames(_))),
Step8 =Table.ExpandTableColumn(Step7,"PromoteHeader",DistinctColumn,DistinctColumn)
in
Step8
- ImkeF7 years agoCommunity Champion
Hi Anonymous ,
your code doesn't work for me. Please check if you have added or removed some steps here before pasting into the thread.
What are you trying to achieve?:
- Dynamically retrieve the names of the first file and apply that to all files of the folder or
- Dynamically retrieve the names of ALL files in the folder and append all tables (with respective null values for columns that are not present in all tables)
- Anonymous7 years agoNot applicable
Hi ImkeF
I have a folder with multiple .xls file with same structure, it will change in future. I am trying to append all as a single file and but I don't need all the columns. I have around 40 columns and as per business we need only 20 columns. So I reffered your method of expanding the column dynamically. Below code works but it is too much slow.
let Source = Folder.Files("E:\Project\TestFiles"), Step1 = Table.SelectColumns(Source,{"Content"}), Step2 = Table.AddColumn(Step1, "Custom", each Excel.Workbook([Content])), Step3 = Table.SelectColumns(Step2, {"Custom"}), Step4 = Table.ExpandTableColumn(Step3, "Custom", {"Name", "Data"}, {"Name", "Data"}), Step5 = Table.RemoveColumns(Step4 ,{"Name"}), Step6 = Table.AddColumn(Step5, "PromoteHeader", each Table.PromoteHeaders([Data])), Step7 = Table.SelectColumns(Step6,{"PromoteHeader"}), DistinctColumn = List.Union(List.Transform(Table.Column(Step7,Table.ColumnNames(Step7){0}), each Table.ColumnNames(_))), ExpandTable = Table.ExpandTableColumn(Step7,"PromoteHeader",DistinctColumn,DistinctColumn) in ExpandTableFor testing i just had 1 .xls file of 1.5Mb in folder but it was too slow and in status bar it was showing loading more than 500Mb( clueless how ). So again I tried using single excel file with below code and it worked perfectly fine.
let Source = Excel.Workbook(File.Contents("E:\Project\TestFiles\Data.xls"), null, true), #"Added Custom" = Table.AddColumn(Source, "PromoteHeader", each Table.PromoteHeaders([Data])), #"Removed Columns" = Table.SelectColumns(#"Added Custom",{"PromoteHeader"}), DistinctColumn = List.Union(List.Transform(Table.Column(#"Removed Columns",Table.ColumnNames(#"Removed Columns"){0}), each Table.ColumnNames(_))), ExpandTable =Table.ExpandTableColumn(#"Removed Columns","PromoteHeader",DistinctColumn,DistinctColumn) in ExpandTableNow my question is:- 1. Why it is too slow for the first code? I am mising anything.
2. How can it be fixed.
Hope the above explanation gives you idea what I am trying to achive.
- ImkeF7 years agoCommunity Champion
Hi Anonymous ,
I have some questions for you:
1) Why do you ignore the questions from my last post that would help me solve your problem?
2) Why do you pretend that your first codes worked if they couldn't? I have wasted my time with your faulty code and now you don't even bother to excuse for that. I've highlighted the corrections you've made below:
faulty code: let Source = Excel.Workbook(File.Contents(""E:\Project\TestFiles\Data.xlsx"), null, true), #"Added Custom" = Table.AddColumn(Source, "PromoteHeader", each Table.PromoteHeaders([Data])), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}), DistinctColumn = List.Union(List.Transform(Table.Column(#"Removed Columns",Table.ColumnNames(#"Removed Columns"){0}), each Table.ColumnNames(_))), ExpandTable =Table.ExpandTableColumn(#"Removed Columns","PromoteHeader",DistinctColumn,DistinctColumn) in ExpandTable correct code: let Source = Excel.Workbook(File.Contents("E:\Project\TestFiles\Data.xls"), null, true), #"Added Custom" = Table.AddColumn(Source, "PromoteHeader", each Table.PromoteHeaders([Data])), #"Removed Columns" = Table.SelectColumns(#"Added Custom",{"PromoteHeader"}), DistinctColumn = List.Union(List.Transform(Table.Column(#"Removed Columns",Table.ColumnNames(#"Removed Columns"){0}), each Table.ColumnNames(_))), ExpandTable =Table.ExpandTableColumn(#"Removed Columns","PromoteHeader",DistinctColumn,DistinctColumn) in ExpandTablefaulty code: let Source = Folder.Files("E:\Project\TestFiles"), Step1 = Table.SelectColumns(Source,{"Content"}), Step2 = Table.AddColumn(Step1, "Custom", each Excel.Workbook([Content])), Step3 = Table.SelectColumns(Step2, {"Custom"}), Step4 = Table.ExpandTableColumn(Step3, "Custom", {"Name", "Data"}, {"Name", "Data"}), Step5 = Table.RemoveColumns(Step5 ,{"Name"}), Step6 = Table.AddColumn(Step6, "PromoteHeader", each Table.PromoteHeaders([Data])), Step7 = Table.SelectColumns(Step6,{"PromoteHeader"}), DistinctColumn = List.Union(List.Transform(Table.Column(Step7,Table.ColumnNames(Step7){0}), each Table.ColumnNames(_))), Step8 =Table.ExpandTableColumn(Step7,"PromoteHeader",DistinctColumn,DistinctColumn) in Step8 correct code: let Source = Folder.Files("E:\Project\TestFiles"), Step1 = Table.SelectColumns(Source,{"Content"}), Step2 = Table.AddColumn(Step1, "Custom", each Excel.Workbook([Content])), Step3 = Table.SelectColumns(Step2, {"Custom"}), Step4 = Table.ExpandTableColumn(Step3, "Custom", {"Name", "Data"}, {"Name", "Data"}), Step5 = Table.RemoveColumns(Step4 ,{"Name"}), Step6 = Table.AddColumn(Step5, "PromoteHeader", each Table.PromoteHeaders([Data])), Step7 = Table.SelectColumns(Step6,{"PromoteHeader"}), DistinctColumn = List.Union(List.Transform(Table.Column(Step7,Table.ColumnNames(Step7){0}), each Table.ColumnNames(_))), ExpandTable = Table.ExpandTableColumn(Step7,"PromoteHeader",DistinctColumn,DistinctColumn) in ExpandTable?