Convert an Indented Text Hierarchy into Excel Columns with Power Query
Question details
Convert an indented department list into separate parent, child, and grandchild columns.

- Product
- Microsoft Excel Power Query
- Device & OS
- not provided
- Scenario
- A text file uses consistent leading spaces to represent department hierarchy levels.
- Observed behavior
- The desired table shows each department name in its hierarchy column, with applicable parent values carried down.
Check that the file uses spaces rather than tabs and the same number of spaces for each indentation level. The example below assumes two spaces per level, no more than three levels, and a parent followed by its children and grandchildren.
Transform the text file with an Excel Power Query
Use this method to create a refreshable table with parent, child, and grandchild columns.
The query keeps one output row per nonblank source line. It fills parent and child names down, then clears the child value on parent rows so a previous branch does not appear there.
In Excel, select Data > Get Data > From Other Sources > Blank Query. Open the query's Advanced Editor.
Replace the editor contents with the following code. Change the file path to your text file and change SpacesPerLevel if your list uses a different number of spaces: let SpacesPerLevel = 2, Source = Table.FromList(Lines.FromBinary(File.Contents("C:\Departments.txt"), null, null, 65001), Splitter.SplitByNothing(), {"Line"}), Nonblank = Table.SelectRows(Source, each Text.Trim([Line]) <> ""), AddLevel = Table.AddColumn(Nonblank, "Level", each let n = Text.Length([Line]) - Text.Length(Text.TrimStart([Line], {" "})) in if Number.Mod(n, SpacesPerLevel) <> 0 or n / SpacesPerLevel > 2 then error "Check indentation or hierarchy depth" else n / SpacesPerLevel, Int64.Type), AddName = Table.AddColumn(AddLevel, "Name", each Text.Trim([Line]), type text), AddParent = Table.AddColumn(AddName, "Parent", each if [Level] = 0 then [Name] else null, type nullable text), AddChild = Table.AddColumn(AddParent, "ChildTemp", each if [Level] = 1 then [Name] else null, type nullable text), FillDown = Table.FillDown(AddChild, {"Parent", "ChildTemp"}), AddChildResult = Table.AddColumn(FillDown, "Child", each if [Level] = 0 then null else [ChildTemp], type nullable text), AddGrandchild = Table.AddColumn(AddChildResult, "Grandchild", each if [Level] = 2 then [Name] else null, type nullable text), Result = Table.SelectColumns(AddGrandchild, {"Parent", "Child", "Grandchild"}) in Result
Select Done, check the preview against the original list, and select Home > Close & Load to add the table to Excel.
After editing the source text file, select Data > Refresh All in Excel to update the table.

Separate an Indented List with WPS Spreadsheets Formulas
Paste the department list into WPS Spreadsheets and use formulas to identify indentation and carry parent and child names into separate columns.
- 1. Paste the source lines: Open WPS Spreadsheets, enter headers in row 1, and paste one department line per cell in column A starting at A2. Verify that the leading spaces are preserved and that the first line is a parent.
- 2. Calculate the level: In B2, enter =(FIND(LEFT(TRIM(A2),1),A2)-1)/2. Replace 2 with the number of spaces used for each level.
- 3. Carry down the parent: In C2, enter =IF(B2=0,TRIM(A2),C1) to show the current parent.
- 4. Add child columns: In D2, enter =IF(B2=0,"",IF(B2=1,TRIM(A2),D1)). In E2, enter =IF(B2=2,TRIM(A2),"").
- 5. Fill and check: Select B2:E2 and drag the fill handle down through the list. Check that levels are 0, 1, or 2 and that each grandchild follows its own child.

Frequently Asked Questions
What if my list uses four spaces per level?
In the Power Query Advanced Editor, change SpacesPerLevel = 2 to SpacesPerLevel = 4. In the WPS formula, replace the final divisor 2 with 4.
Can the query handle tabs instead of spaces?
Not as written. The query counts leading space characters, so replace tabs with a consistent number of spaces in the source file before refreshing.
Why does a child name appear under the wrong parent?
Check the source order. A grandchild must follow its own child; otherwise, filling down can carry a child from an earlier branch. Correct the text file and select Data > Refresh All.
Does the query support more than three hierarchy levels?
The example supports levels 0, 1, and 2. Deeper indentation produces an error; the query must be extended with additional columns and fill-down logic for more levels.




