I want to transform the data from a balance sheet. The problem is that the data is layered, where the level of an item is given in the vertical order. However, for analysis purposes it is better to have the order of levels on the horizontal side (see pictures). To add more difficulty, the code of the first layer starts with AH and ends with TT, the second layer with only T, and the third layer starts with AH and ends in a number (thus without the T). This is just a small part of the data so how can I make an order on the horizontal level based on the code of each level?