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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
kk1791
Frequent Visitor

Help with recursive column

Need help: I want to create a column "Result" which has the parent item along with its component item. If this is not a recursive problem, please let me know what type of syntax should I search in google to solve this. 

Thanks for any help in advance

kk1791_0-1655152671824.png

 

1 ACCEPTED SOLUTION
v-yalanwu-msft
Community Support
Community Support

Hi, @kk1791 ;

Here are two methods in power query.

1.Group by column.

vyalanwumsft_0-1655453780994.png

2.add conditional column.

vyalanwumsft_1-1655453818580.png

3.unpivot it.

vyalanwumsft_2-1655453884952.png

vyalanwumsft_3-1655453913136.png

 

The final show:

vyalanwumsft_4-1655453919739.png

M lanuage:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwMFTSUTIyMjEwANKGSrE6GIJG2ASNsQmaoApaYjPTEpuZltjMtMRtpqlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"End Item" = _t, #"PARENT ITEM" = _t, #"COMPONENT ITEM" = _t]),
    #"Grouped Rows" = Table.Group(Source, {"PARENT ITEM", "End Item"}, {{"min", each List.Min([COMPONENT ITEM]), type nullable text}, {"a", each _, type table [End Item=nullable text, PARENT ITEM=nullable text, COMPONENT ITEM=nullable text]}}),
    #"Expanded a" = Table.ExpandTableColumn(#"Grouped Rows", "a", {"COMPONENT ITEM"}, {"a.COMPONENT ITEM"}),
    #"Added Conditional Column" = Table.AddColumn(#"Expanded a", "Custom", each if [min] = [a.COMPONENT ITEM] then [PARENT ITEM] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"min", "PARENT ITEM"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"End Item", "Custom", "a.COMPONENT ITEM"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Reordered Columns", {"End Item"}, "Attribute", "Value"),
    #"Removed Columns1" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"})
in
    #"Removed Columns1"

Method2:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwMFTSUTIyMjEwANKGSrE6GIJG2ASNsQmaoApaYjPTEpuZltjMtMRtpqlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"End Item" = _t, #"PARENT ITEM" = _t, #"COMPONENT ITEM" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"End Item", Int64.Type}, {"PARENT ITEM", Int64.Type}, {"COMPONENT ITEM", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let a = [PARENT ITEM],
Component = Table.SelectRows(#"Changed Type",each [PARENT ITEM] = a)[COMPONENT ITEM]
 in List.Combine({ List.Distinct({[PARENT ITEM]}) ,Component})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"COMPONENT ITEM"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"End Item", "PARENT ITEM"}, {{"Count", each List.First([Custom]), type list}}),
    #"Expanded Count" = Table.ExpandListColumn(#"Grouped Rows", "Count"),
    #"Removed Columns1" = Table.RemoveColumns(#"Expanded Count",{"PARENT ITEM"})
in
    #"Removed Columns1"


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yalanwu-msft
Community Support
Community Support

Hi, @kk1791 ;

Here are two methods in power query.

1.Group by column.

vyalanwumsft_0-1655453780994.png

2.add conditional column.

vyalanwumsft_1-1655453818580.png

3.unpivot it.

vyalanwumsft_2-1655453884952.png

vyalanwumsft_3-1655453913136.png

 

The final show:

vyalanwumsft_4-1655453919739.png

M lanuage:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwMFTSUTIyMjEwANKGSrE6GIJG2ASNsQmaoApaYjPTEpuZltjMtMRtpqlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"End Item" = _t, #"PARENT ITEM" = _t, #"COMPONENT ITEM" = _t]),
    #"Grouped Rows" = Table.Group(Source, {"PARENT ITEM", "End Item"}, {{"min", each List.Min([COMPONENT ITEM]), type nullable text}, {"a", each _, type table [End Item=nullable text, PARENT ITEM=nullable text, COMPONENT ITEM=nullable text]}}),
    #"Expanded a" = Table.ExpandTableColumn(#"Grouped Rows", "a", {"COMPONENT ITEM"}, {"a.COMPONENT ITEM"}),
    #"Added Conditional Column" = Table.AddColumn(#"Expanded a", "Custom", each if [min] = [a.COMPONENT ITEM] then [PARENT ITEM] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"min", "PARENT ITEM"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"End Item", "Custom", "a.COMPONENT ITEM"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Reordered Columns", {"End Item"}, "Attribute", "Value"),
    #"Removed Columns1" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"})
in
    #"Removed Columns1"

Method2:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwMFTSUTIyMjEwANKGSrE6GIJG2ASNsQmaoApaYjPTEpuZltjMtMRtpqlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"End Item" = _t, #"PARENT ITEM" = _t, #"COMPONENT ITEM" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"End Item", Int64.Type}, {"PARENT ITEM", Int64.Type}, {"COMPONENT ITEM", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let a = [PARENT ITEM],
Component = Table.SelectRows(#"Changed Type",each [PARENT ITEM] = a)[COMPONENT ITEM]
 in List.Combine({ List.Distinct({[PARENT ITEM]}) ,Component})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"COMPONENT ITEM"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"End Item", "PARENT ITEM"}, {{"Count", each List.First([Custom]), type list}}),
    #"Expanded Count" = Table.ExpandListColumn(#"Grouped Rows", "Count"),
    #"Removed Columns1" = Table.RemoveColumns(#"Expanded Count",{"PARENT ITEM"})
in
    #"Removed Columns1"


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.