r/ExcelPowerQuery • u/No-Ambition-6032 • Aug 28 '24
Record Offset in Power Query
Excel Version: 365
I have a table of data that contains a flattened hierarchy structure that contains Codes and Names.
At the lowest level, the code is 4 or 5 digits (a number).
At the next level up, the code ends with '_4' with the next level ending '_3' etc.
For each of the lowest level entries, I want to create columns that contain the code and names of each of the preceding levels.
Example Data:
| Code | Name | 1 Up | 2 Up |
|---|---|---|---|
| ABC_1 | Boss Level | ||
| DEF_2 | Number 2 | ABC_1 Boss Level | |
| DEF_3 | Tier 3 | DEF_2 Number 2 | ABC_1 Boss Level |
| 12000 | Business Unit | DEF_3 Tier 3 | DEF_2 Number 2 |
| 12300 | New business unit | DEF_3 Tier 3 | DEF_2 Number 2 |
Basically I want to calculate the columns 1 Up and 2 Up (etc...) based on the structure in the Code column.
Suggestions and ideas welcome.
1
u/johndering Aug 29 '24
Are the rows sorted as shown above - higher levels in sequence, followed by rows of business units? Will the 1st level up be either of 1, 2, 3 or 4; in your question, the lowest was 4, but in the example data, the lowest was 3?
1
u/johndering Aug 29 '24
Two self-merges using Index from 0 and another Index from 1 as merge keys.
First merge plus Fill Down generates 1 Up column. Second Merge generates 2 Up column.
Mayhap DAX way maybe better if the table is huge.
1
u/johndering Aug 29 '24
Will posting a cut-and-paste of the Power Query Advanced Editor script help you?