cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Sang Frequent Visitor
Frequent Visitor

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

Accepted Solutions
Sang Frequent Visitor
Frequent Visitor

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

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

View solution in original post

1 REPLY 1
Sang Frequent Visitor
Frequent Visitor

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

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

View solution in original post

Helpful resources

Announcements
Ask Amanda Anything Q&A

Ask Amanda Anything Q&A

Learn the answers to some of the questions asked during the Amanda Triple A event.

October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Top Kudoed Authors
Users Online
Currently online: 147 members 1,809 guests
Please welcome our newest community members: