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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Dadoge
Frequent Visitor

Decomposition Tree Visual & Data Preparation

Hello,

 

I want to make a Tree Visual and hence need to prepare my data but I'm having trouble to do so.

 

Data Structure: I have several columns named 'Level 1', 'Level 2', 'Level 3', and 'Level 4'. Each column represents a different level of hierarchy, where 'Level 2' is tied to 'Level1',  'Level 3' is tied to 'Level 2', and 'Level 4' to 'Level 3'. One level is tied to another when there is a break between each rows like this :

Dadoge_2-1713267695475.png

Here, Company 2 and Company 3 are tied to Company 1. Company 4 is tied to Company 3 but not Company 2.

 

Dadoge_3-1713267783039.png

 

What I aim to achieve is having the table on the right. Each different Level 1 is considered a new branch of the Tree.  The Level in a branch all have the same contract number.

 

Thanks in advance,

 

 

2 REPLIES 2
v-cgao-msft
Community Support
Community Support

Hi @Dadoge ,

Try grouping and then filling down:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jc85DoAgEEDRq5ipKdiX2mMQCg/g0np7BWQcCYUJJCwv5BMjzPt6LNs5CWCAg99TSKWNzQtILObTRiU6QZ3snRo71Ryhum4ldZo4Qk15gjhTXLu2/T+cD9XZvs99+tC5QZ9/+9D5cV94+tCFn05wSOkC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Level 1" = _t, #"Level 2" = _t, #"Level 3" = _t, #"Level 4" = _t, #"Hierachy Level" = _t, #"Contract Number" = _t, Index = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Level 1", type text}, {"Level 2", type text}, {"Level 3", type text}, {"Level 4", type text}, {"Hierachy Level", Int64.Type}, {"Contract Number", Int64.Type}, {"Index", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Hierachy Level", "Contract Number", "Index"}, "Attribute", "Value"),
    #"Replaced Value" = Table.ReplaceValue(#"Unpivoted Columns","",null,Replacer.ReplaceValue,{"Attribute", "Value"}),
    #"Pivoted Column" = Table.Pivot(#"Replaced Value", List.Distinct(#"Replaced Value"[Attribute]), "Attribute", "Value"),
    #"Sorted Rows" = Table.Sort(#"Pivoted Column",{{"Index", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Contract Number"}, {{"Data", each Table.FillDown(_,{"Level 1","Level 2","Level 3","Level 4"})}}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"Hierachy Level", "Index", "Level 1", "Level 2", "Level 3", "Level 4"}, {"Hierachy Level", "Index", "Level 1", "Level 2", "Level 3", "Level 4"})
in
    #"Expanded Data"

vcgaomsft_0-1713334326251.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

Hello @v-cgao-msft,

 

First of all thank you for your quick answer !

 

I forgot to add the usecase where in a level 1, multiple level can contains level as shown below. I tried to adjust your code but the filling doesnt match with what i want. I feel like the index is crucial in what i want to achieve because of the order.  Can you help me on this ?

 

 

Dadoge_0-1713349531923.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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