Power Query Problems

Convert an Indented Text Hierarchy into Excel Columns with Power Query

Elise WilliamsElise Williams Dec 2, 2026 868 views

Question details

Convert an indented department list into separate parent, child, and grandchild columns.

Convert an Indented Text Hierarchy into Excel 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.
Before you start

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.

Solution 1Recommended

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.

1
Open Power Query

In Excel, select Data > Get Data > From Other Sources > Blank Query. Open the query's Advanced Editor.

2
Enter the transformation

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

3
Review and load

Select Done, check the preview against the original list, and select Home > Close & Load to add the table to Excel.

4
Refresh after changes

After editing the source text file, select Data > Refresh All in Excel to update the table.

Transform the text file with an Excel Power Query
Check the hierarchy order: Each grandchild must follow its own child, and each child must follow its own parent. The query checks indentation depth but does not validate missing hierarchy levels.
Work with hierarchies in WPS Spreadsheets

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. 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. 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. 3. Carry down the parent: In C2, enter =IF(B2=0,TRIM(A2),C1) to show the current parent.
  4. 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. 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.
Separate parent, child, and grandchild names without writing a Power QueryOpen and save Microsoft Excel XLSX filesUse a familiar spreadsheet interface to review the resulting hierarchyWork with a free, lightweight office suite
microsoft office alternative - wps office

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.