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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Fast Power Query Code for getting the lowest level children in Parent/Child Hierarchy

I am in search of m query that adds a column to display all Children at level 0 in a table with Parent/Child relation. I am already using a function but its painfully slow.

****fnParChAllChildren****

let
    Source = (ParChTable as table, ChildKey as text, ParentKey as text) as table =>
let
    AllChildren = Table.AddColumn(ParChTable, "AllChildren", each 
        List.Skip(
             List.Generate(()=>
             [Children={Record.Field(_, ChildKey)}],     // (List of) records as start values
             each List.Count([Children]) > 0,            // condition under which the next loop is execute
             each [ Children= List.Buffer(fnRecColumnAsList(
                                        Table.SelectRows(ParChTable, (ParChTable) => List.Contains([Children], Record.Field(ParChTable, ParentKey))) 
                                        , ChildKey))],   // Executable action
             each [Children])                            // (Selection of) return
         ,1)),                                           // Skip first element in list which is it's own key 
    LstAllChildren = Table.AddColumn(AllChildren, "LstAllChildren", each List.Combine([AllChildren])),
    Level = Table.Buffer(Table.AddColumn(LstAllChildren, "Level", each List.Count([AllChildren]))),
    Indent = Table.AddColumn(Level, "Indent", each List.Max(Level[Level])*4-[Level]*4),
    JustLevel0 = List.Buffer(fnRecColumnAsList(Table.SelectRows(Indent, each [Level]=0), ChildKey)),
    AllChildrenJustLevel0 = Table.AddColumn(Indent, "ChildrenJustLevel0", each List.Intersect({[LstAllChildren], JustLevel0}))
in
    AllChildrenJustLevel0
in
    Source
****fnRecColumnAsList*****

let
    Source = (Table as table, ColumnName as text) as list =>
 Table.ToColumns(Table.SelectColumns(Table, ColumnName)){0}
in
    Source

I forgot where I got it from but it works and I use column AllChildrenJustLevel0 generated by it. Only bad thing is its painfully slow.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I got it to work faster by putting Table.Buffer step just before calling it.

View solution in original post

1 REPLY 1
Anonymous
Not applicable

I got it to work faster by putting Table.Buffer step just before calling it.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.