Created
June 10, 2023 14:13
-
-
Save ncalm/05e1561d8aac52f27cc3e619246e40db to your computer and use it in GitHub Desktop.
This Power Query function handles headers which have been split onto multiple rows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (Source as table, header_condition as function) as any => | |
| let | |
| // two row header | |
| hh = Table.SelectRows(Source, header_condition) , | |
| // list of new column headers | |
| h = List.Accumulate( | |
| Table.ToColumns(hh), | |
| {}, | |
| (a,b) => List.Combine({a,{Text.Combine(b," ")}}) | |
| ), | |
| // get the data records (without the two header rows) | |
| data = Table.FromRecords(List.Difference(Table.ToRecords(Source),Table.ToRecords(hh))), | |
| renames = List.Zip({Table.ColumnNames(data),h}), | |
| // use the new header rows | |
| tbl = Table.RenameColumns(data, renames), | |
| result = tbl | |
| in | |
| result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment