Forum Discussion

BBHouston's avatar
BBHouston
Helper I
5 years ago
Solved

How to create a supervisor hierarchy table:

Let's say I have a table that looks like this:

 

Staff NameSupervisor
Ryan*blank*
MarthaRyan
EricaRyan
DavidMartha
SamanthaErica
KyleErica

 

How do I build a table or table visual that looks like this? 

Lv. 1Lv. 2Lv. 3
RyanEricaSamantha
  Kyle
 MarthaRyan



 

4 Replies

  • CNENFRNL's avatar
    CNENFRNL
    Community Champion

    Hi, BBHouston , you might want to try a solution in Power Query

    let
    
        Path = (emp) as text => 
            let
                sup = suplist{List.PositionOf(emplist, emp, Occurrence.First)},
                recursion = if List.PositionOf(emplist, sup) = -1 then "" else @Path(sup) & "|" & sup
            in
                recursion,
    
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCqpMzFPSUVKK1YlW8k0sKslIBPLAgiAR16LMZBQBl8SyzBSgAFQpSCg4MTcxD6IPohwk6F2Zk4oQiAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Staff Name" = _t, Supervisor = _t]),
        emplist = List.Buffer(Source[Staff Name]),
        suplist = List.Buffer(Source[Supervisor]),
    
        #"Added Hierarchy" = Table.AddColumn(Source, "Lv", each Path([Staff Name]) & "|" & [Staff Name]),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Added Hierarchy", "Lv", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Lv0", "Lv1", "Lv2", "Lv3"}),
        #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Staff Name", "Supervisor", "Lv0"}),
        #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Lv3] <> null))
    in
        #"Filtered Rows"