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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
psoundararajan
Frequent Visitor

Obtain highest level parent recursively

Hello,

 

We have a table of relations, and each item has different levels of parent-child relations.

 

Input data:

ItemParent
ID1P1
ID2P2
ID3P2
ID4P3
P1S1
P1S2
P2S2
P3S3
P3S4
S1S1
S2S2
S3S3
S4S4

 

 

How do I obtain the following output from above?

ItemParent
ID1S1
ID1S2
ID2S2
ID3S2
ID4S3
ID4S4
P1S1
P1S2
P2S
P3S3
P3S4
S1S1
S2S2
S3S3
S4S4

 

An item can be linked to more than one parent. In the target table, I would like to have one row per item-ultimate parent combination. Thanks in advance.

 

 

17 REPLIES 17
techies
Super User
Super User

Hi @psoundararajan in power query, add a new blank query > advanced editor, add this, rename it as - GetUltimateParents

 

(child as text, tbl as table) as table =>
let
// Direct parents of the child
DirectParents = Table.SelectRows(tbl, each [Item] = child),

Result =
if Table.IsEmpty(DirectParents) then
#table({"Item","Parent"}, {})
else
Table.Combine(
List.Transform(
DirectParents[Parent],
(p) =>
if p = child then
// Stop if parent = child (self-loop)
#table({"Item","Parent"}, {{child, p}})
else
let
Sub = @GetUltimateParents(p, tbl),
Output =
if Table.IsEmpty(Sub) then
#table({"Item","Parent"}, {{child, p}})
else
Table.TransformColumns(Sub, {{"Item", each child, type text}})
in
Output
)
)
in
Table.Distinct(Result)

 

techies_0-1756325774445.png

Next create another blank query, add this

 

let
Source = relations, // replace with your input table’s query name
DistinctItems = List.Distinct(Source[Item]),
Expanded = Table.Combine(
List.Transform(DistinctItems, each GetUltimateParents(_, Source))
)
in
Expanded

 

techies_1-1756325943476.png

 

 

 

― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

Hi @psoundararajan,

Is solution provided by the community member @techies resolve your query, could you please confirm? It helps us ensure that the solutions provided are effective and beneficial for everyone.

Hope this helps clarify things and let me know what you find after giving these steps a try happy to help you investigate this further.

Thank you for using the Microsoft Community Forum.

Hi @psoundararajan,

Hope you had a chance to try out the solution shared earlier. Let us know if anything needs further clarification or if there's an update from your side always here to help.

Thank you.

Hi @psoundararajan,

Just wanted to follow up one last time. If the shared guidance worked for you, that’s wonderful hopefully it also helps others looking for similar answers. If there’s anything else you'd like to explore or clarify, don’t hesitate to reach out.

Thank you.

Hey, no. It is not working. I asked a question below, but received no response. The only way I could make it work (for now) is to duplicate the data source and do self-joins. 

Hi @psoundararajan,
Thank you for the follow-up question.

Attached file gives you the expected results using M query. If your source table is a summarized table (for example, aggregated counts or grouped data), then the script I provided will not work directly because it expects a flat list of item-parent relationships.

vkpolojumsft_0-1757333500729.png

If you still have any other questions, please share us the sample input file along with expected output.

Hope this clears it up. Let us know if you have any doubts regarding this. We will be happy to help.

Thank you for using the Microsoft Fabric Community Forum.

Hi @psoundararajan,

Just checking in to see if the issue has been resolved on your end. If the earlier suggestions helped, that’s great to hear! And if you’re still facing challenges, feel free to share more details happy to assist further.

Thank you.

Hi @psoundararajan,

Hope you had a chance to try out the solution shared earlier. Let us know if anything needs further clarification or if there's an update from your side always here to help.

Thank you.

Hi @psoundararajan,

Just wanted to follow up one last time. If the shared guidance worked for you, that’s wonderful hopefully it also helps others looking for similar answers. If there’s anything else you'd like to explore or clarify, don’t hesitate to reach out.

Thank you.

psoundararajan
Frequent Visitor

Hello, thanks for your responses. I'm trying these at the moment. Please allow me some time to get back. 

raju8004212
Frequent Visitor

@psoundararajan 

Hierarchy =
VAR Base = ADDCOLUMNS ( Relations, "OriginalItem", Relations[Item] )
RETURN
GENERATE (
Base,
PATH ( Base[Item], Base[Parent] )
)

try this and let me know if it works 

v-kpoloju-msft
Community Support
Community Support

Hi @psoundararajan,
Thank you for reaching out to the Microsoft fabric community forum. Also, thanks to @Shahid12523@wardy912, for those inputs on this thread.

Has your issue been resolved? If the response provided by the community member @Shahid12523, @wardy912,addressed your query, could you please confirm? It helps us ensure that the solutions provided are effective and beneficial for everyone.

Hope this helps clarify things and let me know what you find after giving these steps a try happy to help you investigate this further.

Thank you for using the Microsoft Community Forum.

Hi @psoundararajan,

Hope you had a chance to try out the solution shared earlier. Let us know if anything needs further clarification or if there's an update from your side always here to help.

Thank you.

Shahid12523
Community Champion
Community Champion

Load your Item-Parent table.

Create a function that, given an Item, recursively looks up its Parent until reaching the top (where parent = itself).

Expand results so you keep all combinations.


let
Source = YourTable,

// Recursive function
GetParents = (child as text) as table =>
let
DirectParents = Table.SelectRows(Source, each [Item] = child),
WithRecursive = Table.ExpandTableColumn(
Table.AddColumn(DirectParents, "UltimateParent", each GetParents([Parent])),
"UltimateParent", {"Parent"}
)
in
if Table.IsEmpty(DirectParents)
then #table({"Item","Parent"}, {{child, child}})
else WithRecursive,

// Apply recursion to all Items
DistinctItems = List.Distinct(Source[Item]),
Results = Table.Combine(List.Transform(DistinctItems, each GetParents(_)))
in
Results

Shahed Shaikh

Hello, should this also work if my source table is a summarised table? I'm unable to replace YourTable with the summarized table name.

wardy912
Solution Sage
Solution Sage

Hi @psoundararajan 

 

 In power query, add a new blank query and add the following (edit accordingly)

 

let
    Source = YourTableName,  // Replace with your actual table name
    GetUltimateParents = (item as text) as list =>
        let
            DirectParents = List.SelectRows(Source, each [Item] = item)[Parent],
            UltimateParents = List.Combine(List.Transform(DirectParents, each
                if _ = item then {_} else GetUltimateParents(_)
            ))
        in
            List.Distinct(UltimateParents),

    Result = Table.AddColumn(Source, "UltimateParents", each GetUltimateParents([Item])),
    Expanded = Table.ExpandListColumn(Result, "UltimateParents"),
    Renamed = Table.RenameColumns(Expanded, {{"UltimateParents", "Parent"}})
in
    Renamed

 

 I hope this helps, please mark as solved if it does, thanks!

Hello @wardy912 , should this also work if my source table is a summarised table? I'm unable to replace YourTable with the summarized table name.

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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