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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
CHARLEA1
Frequent Visitor

Power Query Looping Calculation

Hi there, 

I was hoping someone could help me with looping in powerbi.

Logic is as follows - 

I have columns with supervisors as shown - 

powerbi example.jpg 

The custom column collates all the supervisors. 

Currently it is done with simple if statements - 

= Table.AddColumn(#"Reordered Columns", "Supervisors", each if([Supervisor 3]<>null) then [Supervisor 1] & " , " & [Supervisor 2] & " , " & [Supervisor 3]
else if([Supervisor 2]<>null) then [Supervisor 1] & " , " & [Supervisor 2]
else [Supervisor 1])

 

The thing is, this table is based off a column supervisor no. that was pivoted. If the database adds in an extra supervisor - i.e. Supervisor 4, when pivoted there will be an extra column and will throw an error with this calculation.

I would like a loop to dynamically calculate this concatenation so that I do not need to go in and manually change the code if there exceeds the amount of existing supervisors. 

Thinking something along the times where there is another custom column specifying number of supervisors then looping through and appending.

Any ideas would be largely appreciated. 

 

Many Thanks,

Ashling

1 ACCEPTED SOLUTION

= Table.AddColumn(#"Reordered Columns", "Supervisors", each Text.Combine(Record.ToList(Record.SelectFields(_,List.Select(Record.FieldNames(_),each Text.StartsWith(_,"Supervisor")))),", "))

this will combine all the columns that starts with Supervisor

View solution in original post

3 REPLIES 3
wdx223_Daniel
Community Champion
Community Champion

= Table.AddColumn(#"Reordered Columns", "Supervisors", each Text.Combine(Record.ToList(_),","))

Thanks - it is more efficient to be - 

= Table.AddColumn(#"Reordered Columns", "Supervisors", each Text.Combine({[Supervisor 1],[Supervisor 2],[Supervisor 3]},", "))

although not sure how to pass in those columns dynamically using Record.ToList?

= Table.AddColumn(#"Reordered Columns", "Supervisors", each Text.Combine(Record.ToList(Record.SelectFields(_,List.Select(Record.FieldNames(_),each Text.StartsWith(_,"Supervisor")))),", "))

this will combine all the columns that starts with Supervisor

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.