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
mat33delo
Regular Visitor

Recursion in Power BI

Hello All, 

 

I'm brainstorming ideas on how to run recursion in Power BI and wanted community input.

 

High level, I need to display all the orders of a specific item/lot-number but this includes orders of this item and all the finishedgood BOMs that this lot is a part of. Unfortunately, the new Finished Good can be part of another completely different BOM. Thus recursion is necessary to out put all the relevant orders connected to a specific lot since this parent child relationship can go multiple levels. 

 

So I have a table of all orders and a table of these BOMs

 

I don't think I can do this in Power BI but I think I can figure this out using Python.

 

If I used python, how would I embed this solution into a report?

 

Ideally, the user would use a slicer and choose specific lot then the python visual would use recursion and display a table of all the relevant orders. The user can then download the report. 

 

Is this approach possible and are there any other potential solutions?

 

 

1 ACCEPTED SOLUTION
rohit1991
Super User
Super User

HI @mat33delo ,

Power BI does not natively support recursion, but you can achieve this using one of three methods:

1. DAX Approach (Limited Depth)

Use DAX Parent-Child Hierarchy functions to track relationships.

  • Create a BOM path using PATH().
  • Use PATHCONTAINS() to filter relevant orders dynamically.

    BOM_Path = PATH(BOM[Child_Item], BOM[Parent_Item])

    Is_Relevant_Order =
    IF(PATHCONTAINS(BOM[BOM_Path], SELECTEDVALUE(Orders[Lot_Number])), 1, 0)

  • Works well for limited recursion depth but does not scale well for deep hierarchies.

2. Python Approach (Best for Deep Recursion)

Use Python inside Power BI to handle multi-level recursion dynamically.

  • Add a Python Visual and write a recursive function:
  • def find_orders(df, lot_number, bom_df):
    related_orders = set()
    def recurse(lot):
    children = bom_df[bom_df['Parent_Item'] == lot]['Child_Item'].tolist()
    related_orders.update(children)
    for child in children:
    recurse(child)

    recurse(lot_number)
    return df[df['Lot_Number'].isin(related_orders)]

    selected_lot = dataset['Lot_Number'].iloc[0] # Get selected slicer value
    result_df = find_orders(dataset, selected_lot, bom_data)
    result_df

3. Power Query (M) Approach (Precomputed Table for Performance)

If real-time recursion is not required, precompute the hierarchy using Power Query (M).

  • Flatten the BOM table before loading into Power BI.
  • Use joins and recursive logic in Power Query to structure relationships.
  • Refresh the dataset when BOM changes.

Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

View solution in original post

1 REPLY 1
rohit1991
Super User
Super User

HI @mat33delo ,

Power BI does not natively support recursion, but you can achieve this using one of three methods:

1. DAX Approach (Limited Depth)

Use DAX Parent-Child Hierarchy functions to track relationships.

  • Create a BOM path using PATH().
  • Use PATHCONTAINS() to filter relevant orders dynamically.

    BOM_Path = PATH(BOM[Child_Item], BOM[Parent_Item])

    Is_Relevant_Order =
    IF(PATHCONTAINS(BOM[BOM_Path], SELECTEDVALUE(Orders[Lot_Number])), 1, 0)

  • Works well for limited recursion depth but does not scale well for deep hierarchies.

2. Python Approach (Best for Deep Recursion)

Use Python inside Power BI to handle multi-level recursion dynamically.

  • Add a Python Visual and write a recursive function:
  • def find_orders(df, lot_number, bom_df):
    related_orders = set()
    def recurse(lot):
    children = bom_df[bom_df['Parent_Item'] == lot]['Child_Item'].tolist()
    related_orders.update(children)
    for child in children:
    recurse(child)

    recurse(lot_number)
    return df[df['Lot_Number'].isin(related_orders)]

    selected_lot = dataset['Lot_Number'].iloc[0] # Get selected slicer value
    result_df = find_orders(dataset, selected_lot, bom_data)
    result_df

3. Power Query (M) Approach (Precomputed Table for Performance)

If real-time recursion is not required, precompute the hierarchy using Power Query (M).

  • Flatten the BOM table before loading into Power BI.
  • Use joins and recursive logic in Power Query to structure relationships.
  • Refresh the dataset when BOM changes.

Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

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.