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
vyask
Frequent Visitor

Loop / recursive / DAX / PowerQuery / Same Table loop

Hi, I am new to Power BI and I need help with creating a loop using DAX or Power Query.

Problem Statement:
I have only 1 table (which shows unidirectional links between workitems) with 4 columns.

 

SourceWorkItemIdParentWorkItemIdSourceWorkItemTypeParentWorkItemTypeCapID (OUTPUT)
1122Story1Feature166
2233Feature1Epic166
4455Theme1MBI10
3344Epic1Theme10
5566MBI1Cap10
111222Story2Cap1222
11112222Story3Cap22222
166Story4Cap166
772222Epic2Cap20

 

The WorkItemType hierarchy is not fixed. It can be Story -> Feature -> Cap or it can be Story -> Cap or anything else.

The maximum depth (in this order) is: Story -> Feature -> Epic -> Theme -> MBI -> Cap.

I need to show CapID against Stories and Features that are directly or indirectly linked to that Cap by adding 1 new column in the existing table.

I tried to do this using a python script which works fine in python but it times-out in Power BI and gives 1800000ms error, probably because of the table size.

 

Code Example with comments:

 

Cap = []                                                   // created a blank list
for indices,row in df.iterrows():                          // started a for loop    
     if row['SourceWorkItemType'] in ['Story','Feature']:  // if SourceType is Story/Feature
          if row['ParentWorkItemType'] == 'Cap':           // if current ParentType is Cap               
               Cap.append(row['ParentWorkItemId'])          // append current CapID to list Cap
          else:                                            // if ParentType is not Cap               
               for indices,row_1 in df.iterrows():         // nested for loop
                    if row['ParentWorkItemId'] == row_1['SourceWorkItemId']: // if ParentID is found in another row's SourceID 
                         if row_1['ParentWorkItemType'] != 'Cap':  // if ParentType is not Cap
                              row['ParentWorkItemId'] = row_1['ParentWorkItemId'] // then search new ParentID in another row's sourceID
                         else:                                // else if current Parent is Cap                                    
                              Cap.append(row_1['ParentWorkItemId'])  // add the current ParentID to Cap list         
     else:                  // if SourceType is not Story/Feature or if CapID is not found
          Cap.append(0)               // append 0 to the Cap list
print(Cap)                          // add Cap list as the 5th column

 

Request you to help me with this. Thanks in advance! 🙂

12 REPLIES 12

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.

Top Solution Authors
Top Kudoed Authors