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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
808
New Member

M-Code looping

Im struggling to get a grip of Power Query. I've been trying to run all the tables (tables with the same structure btw) from sheet1 through one Power Query-question. I suspect it might be possible with some kind of m-code loop function magic.

My previous solution was to duplicate the power query script and change the input from "table1" to "table2" and so forth. I then figured it might be better of doing it the right way, since i ran int a bunch of problems. 

I would so much appreciate any clues. Thank you in advance!

6 REPLIES 6
Greg_Deckler
Community Champion
Community Champion

@808 You likely want to write a function in Power Query that you can call for each of your tables. There is no real "looping" in Power Query. There is recursion.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hey Greg! I really appreciate you taking the time to clarify that for me. Thank you!

The List.Generate function is very similar to classic looping.

 

That said, I try to avoid it if at all possible in favor of leveraging the functional nature of M. For example, if you have a list of tables you want to do the same transformation on, you'd write something like List.Transform(ListOfTables, TransformationFunction) rather than iterating through the list like a loop.

 

Here's an example (that I just finished writing) that transposes and promotes headers for a list of tables:

https://community.powerbi.com/t5/Power-Query/Transforming-data-with-repeated-date-headers/m-p/309681...

Hey there Alexis! I really tried to wrap my head around your suggestion, but I think I might have been vague describing my issue. In that case i'm sorry. Please let me try to explain with some context and screenshots.

 

 

 

 

 

 

thumbnail_image007.png

Workbook1, Sheet1

thumbnail_image006.png

Power Query flowchart

 1. In Workbook1, Sheet1, I have a set of tables with the same format, and I want to run them all through the "PQ 1" power query question as shown in the flowchart.

 

 

 

 

 

 

 

 

thumbnail_image005.png

Curated data when pasted in Workbook2

2. After that, I need to pass the data through three separate PQ-questions. The output from "PQ 2a" as shown above is to be pasted as ranges one after another into a separate workbook (Workbook2).

 

 

 

 

 

 

 

 

thumbnail_image001.png

Curated data when pasted in Workbook3

 

thumbnail_image002.png

Curated data when pasted in Workbook4

3. Furthermore, after passing through "PQ 2b", the data should be pasted into separate workbooks. Workbook3, 4, 5, and 6 since there were four tables this time. Examples of Workbook3&4 shown above).

 


4. Finally, after passing through "PQ 2c", I need the data consolidated into a single table and pasted into Workbook2 aswell.



I realise that doing this task requires some VBA coding along with dealing with the Power Query-hassel. Regarding Power Query, based on what I've mentioned, do you still think that using the List.Transform function is the best way to go? Or do you have any other suggestions?

I have some really basic knowledge of VBA coding, so if you have any suggestions or tips for the VBA part, I'd really appreciate it aswell. Thank you so much!

I'm having some trouble following. Can you link to a sample file, that is, upload an Excel file to Dropbox/Google Drive/OneDrive/SharePoint and share it via a publicly accessible link? Please include the expected result.

 

I think this should be possible without VBA but I'd like to tinker with an actual file to feel confident about that.

 

Language note: I think you mean "query" when you write "question". These are similar words but when the subject involves data tables and code that operates on it, the word "query" is the one that should be used (even though you get weird phrases like "Power Query query").

Thank you for the information. It's directly translated from Swedish. I also felt that it sounded a bit strange. Regarding Excel, great idea, I will definitely make one! I really appreciate this, you have no idea how valuable this is for me.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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