I have some data shown below. I wish to convert that to the table shown below, but with the information in each column in alphabetical order. Note, that while the Captain doesn't have every category listed against his position in the input table he needs them all listed in the output. The current scheme creates two tblDelegations which are then combined and manipulated to create the output. The Powerquery M Code that does that is shown at the bottom. I confess I don't fully understand how the code works and, so, would be grateful for any advice on how I can get from the input to the output!
Input:
Incorrect Output (needs column data in alphabetical order):
Relevant PowerQuery M Code:
Both tblDelegations are identical:
Input:
Copy of SY Ashley Accounts - TEST Ashley.xlsm | ||||
---|---|---|---|---|
I | J | |||
6 | Budget Holding Positions | Category | ||
7 | Captain | Capital Expense | ||
8 | Captain | Administration | ||
9 | Captain | Charter & Sale Related | ||
10 | Engineer | Comms & Subscription | ||
11 | Chief Steward | Crew | ||
12 | Engineer | Engineering Maintenance | ||
13 | Engineer | Fuel/Oil | ||
14 | Chief Officer | Hull & Deck Maintenance | ||
15 | Chief Steward | Interior Maintenance | ||
16 | Captain | Management | ||
17 | Chief Officer | Navigation | ||
18 | Captain | Payroll | ||
19 | Captain | Port Fees | ||
20 | Chief Officer | Safety & Security | ||
21 | Chief Officer | Tenders/Toys | ||
22 | Captain | Yacht Insurance | ||
23 | Chief Steward | Owner/Guest | ||
24 | Engineer | Refit | ||
25 | Chief Steward | VIP | ||
26 | Chief Officer | Ashley 2 | ||
Defaults |
Incorrect Output (needs column data in alphabetical order):
Copy of SY Ashley Accounts - TEST Ashley.xlsm | |||||||
---|---|---|---|---|---|---|---|
B | C | D | E | ||||
1 | Captain | Chief Officer | Chief Steward | Engineer | |||
2 | Capital Expense | Hull & Deck Maintenance | Crew | Comms & Subscription | |||
3 | Administration | Navigation | Interior Maintenance | Engineering Maintenance | |||
4 | Charter & Sale Related | Safety & Security | Owner/Guest | Fuel/Oil | |||
5 | Management | Tenders/Toys | VIP | Refit | |||
6 | Payroll | Ashley 2 | |||||
7 | Port Fees | ||||||
8 | Yacht Insurance | ||||||
9 | Comms & Subscription | ||||||
10 | Crew | ||||||
11 | Engineering Maintenance | ||||||
12 | Fuel/Oil | ||||||
13 | Hull & Deck Maintenance | ||||||
14 | Interior Maintenance | ||||||
15 | Navigation | ||||||
16 | Safety & Security | ||||||
17 | Tenders/Toys | ||||||
18 | Owner/Guest | ||||||
19 | Refit | ||||||
20 | VIP | ||||||
21 | Ashley 2 | ||||||
Delegations |
Relevant PowerQuery M Code:
Rich (BB code):
let
Source = Table.Combine({tblDelegations1, #"tblDelegations1 (2)"}),
#"Removed Duplicates" = Table.Distinct(Source),
FlatTable = Table.Sort(#"Removed Duplicates",{{"Budget Holding Positions", Order.Ascending}, {"Category", Order.Ascending}}),
ColForValues = "Category",
ColToPivot = "Budget Holding Positions",
PivotColNames = List.Buffer(List.Distinct(Table.Column(FlatTable,ColToPivot))),
#"Pivoted Column" = Table.Pivot(FlatTable, PivotColNames, ColToPivot, ColForValues, each _),
TableFromRecordOfLists = (rec as record, fieldnames as list) =>
let
PartialRecord = Record.SelectFields(rec,fieldnames),
RecordToList = Record.ToList(PartialRecord),
Table = Table.FromColumns(RecordToList,fieldnames)
in
Table
,#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames))
,#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames)
,#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
in
#"Expanded Values"
Both tblDelegations are identical:
Copy of SY Ashley Accounts - TEST Ashley.xlsm | ||||
---|---|---|---|---|
A | B | |||
1 | Budget Holding Positions | Category | ||
2 | Captain | Administration | ||
3 | Chief Officer | Ashley 2 | ||
4 | Captain | Capital Expense | ||
5 | Captain | Charter & Sale Related | ||
6 | Engineer | Comms & Subscription | ||
7 | Chief Steward | Crew | ||
8 | Engineer | Engineering Maintenance | ||
9 | Engineer | Fuel/Oil | ||
10 | Chief Officer | Hull & Deck Maintenance | ||
11 | Chief Steward | Interior Maintenance | ||
12 | Captain | Management | ||
13 | Chief Officer | Navigation | ||
14 | Chief Steward | Owner/Guest | ||
15 | Captain | Payroll | ||
16 | Captain | Port Fees | ||
17 | Engineer | Refit | ||
18 | Chief Officer | Safety & Security | ||
19 | Chief Officer | Tenders/Toys | ||
20 | Chief Steward | VIP | ||
21 | Captain | Yacht Insurance | ||
Sheet1 |