Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
dstanisljevic
Helper I
Helper I

Creating hierarchy tree from data

Hello, need help in figuring out how to manipulate data that is returned to me where the parent column may have both the top level and a sub level. So if the data looks like this:

ParentName

1

A
2B
3C
41
52
63

 

Trying to transform it to look like:

ParentSubName
41A
52B
63C


Any suggestions? I'm this is probably a basic topic but wasn't able to find anything on it. Also, there are scenarios where the subs may be nested as well before it gets to the top level (10>9>7>D). Need to be able to account for that as well. Any help would be greatly appreciated! Thank you!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @dstanisljevic ,

I created a sample pbix file(see attachment), please check whether that is what you want.

1. Added an auxiliary table to get column values without parent elements (4, 5, 6)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWJVjICspzALGMgyxnMMgGyDMEsUyDLCMwyA7KMlWJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Parent = _t, Name = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Parent", type text}, {"Name", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if List.Contains(#"Changed Type"[Name], [Parent]) then null else [Parent]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Parent", "Name"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Custom] <> null)),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows",{{"Custom", "Name"}})
in
    #"Renamed Columns"

yingyinr_0-1624862673674.png

2. Append this table to the original table

yingyinr_1-1624863010049.png

3. Create some calculated columns to get the path and pathlength by using Parent and Child functions

Path = PATH('Table'[Name],'Table'[Parent])
Pathlen = PATHLENGTH('Table'[Path])

4. Create calculated columns to get the parent, sub and name values

nParent =
VAR _len =
    MAX ( 'Table'[Pathlen] )
RETURN
    IF ( 'Table'[Pathlen] = _len, PATHITEM ( 'Table'[Path], 1 ), BLANK () )
Sub = 
VAR _len =
    MAX ( 'Table'[Pathlen] )
RETURN
    IF ( 'Table'[Pathlen] = _len, PATHITEM ( 'Table'[Path], 2 ))
nName = 
VAR _len =
    MAX ( 'Table'[Pathlen] )
RETURN
    IF ( 'Table'[Pathlen] = _len, PATHITEM ( 'Table'[Path], 3 ))

yingyinr_2-1624863339895.png

Best Regards

View solution in original post

2 REPLIES 2
dstanisljevic
Helper I
Helper I

Should also add that not all values in the 'Parent' column exist in the 'Name' column.

Anonymous
Not applicable

Hi @dstanisljevic ,

I created a sample pbix file(see attachment), please check whether that is what you want.

1. Added an auxiliary table to get column values without parent elements (4, 5, 6)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWJVjICspzALGMgyxnMMgGyDMEsUyDLCMwyA7KMlWJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Parent = _t, Name = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Parent", type text}, {"Name", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if List.Contains(#"Changed Type"[Name], [Parent]) then null else [Parent]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Parent", "Name"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Custom] <> null)),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows",{{"Custom", "Name"}})
in
    #"Renamed Columns"

yingyinr_0-1624862673674.png

2. Append this table to the original table

yingyinr_1-1624863010049.png

3. Create some calculated columns to get the path and pathlength by using Parent and Child functions

Path = PATH('Table'[Name],'Table'[Parent])
Pathlen = PATHLENGTH('Table'[Path])

4. Create calculated columns to get the parent, sub and name values

nParent =
VAR _len =
    MAX ( 'Table'[Pathlen] )
RETURN
    IF ( 'Table'[Pathlen] = _len, PATHITEM ( 'Table'[Path], 1 ), BLANK () )
Sub = 
VAR _len =
    MAX ( 'Table'[Pathlen] )
RETURN
    IF ( 'Table'[Pathlen] = _len, PATHITEM ( 'Table'[Path], 2 ))
nName = 
VAR _len =
    MAX ( 'Table'[Pathlen] )
RETURN
    IF ( 'Table'[Pathlen] = _len, PATHITEM ( 'Table'[Path], 3 ))

yingyinr_2-1624863339895.png

Best Regards

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.