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
msksenthil
Helper III
Helper III

Power Query Loop issue

I would like to create the loop and concatenate the list records in power query function but I'm getting error. 

 

M-Code

(loop as number, T1 as table, CurString as any) => let
CurrentLoop = loop + 1,
T1ColumnCount = 5,
T1ColumnValues = Table.ColumnNames(T1),
CurrentString =if (CurrentLoop = 0) then Text.From(T1ColumnValues{CurrentLoop}) else Text.From(CurString) & Text.From(T1ColumnValues{CurrentLoop}),
OutputString = if (CurrentLoop = T1ColumnCount) then CurrentString else @Query2(CurrentLoop, T1, CurrentString)
in
OutputString

 

Error:

Expression.Error: There weren't enough elements in the enumeration to complete the operation.
Details:
[List]

 

Many thanks in advance

4 REPLIES 4
mahoneypat
Microsoft Employee
Microsoft Employee

I suspect this is due to the fact that counting in power query starts at zero.  when you get to loop = 5, you are actually pulling the name of the 6th column, which doesn't exist.

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Bingo! It's working great. But when I make it dynamic like "T1ColumnCount =Table.ColumnCount(T1)" it's showing the below error

 

Expression.Error: There weren't enough elements in the enumeration to complete the operation.
Details:
[List]

Fowmy
Super User
Super User

@msksenthil 

What is @Query2 in this context?

Can you share the complete file after removing confidential data?

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

@Fowmy 

Query2 is the own function name.

 

Sorry I couldn't attach the pbix. 

 

msksenthil_0-1621511613658.pngmsksenthil_1-1621511627966.png

Table format

msksenthil_2-1621511650727.png

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!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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.