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

Try your skills in the Power BI Dataviz World Championship! Round one ends June 26. Join now

Reply
Anonymous
Not applicable

Create Function that loops on Parameter Values

Hi all,

 

I've created a Function to invoke a table from a SQL database. The function will load the table and keep only the columns that are listed in an Excel file. (but converted to a list).

I would like to automate this a bit further, As I will be doing this alot more.
How could I add a loop that would iterate all tables from a table/list untill they are all added. 

This would mean the parameter value for the function would be a list instead of text value as it is now. 
How should I proceed here? Cannot seem to get my head around this. 

The code of current function here :

( parameterTableName as text) => 

let
//parameterTableName = "v_ha_last_inpat_haward_curr_ward_nl" ,
    ColumnsNeeded = Table.SelectColumns( ColumnNames , parameterTableName ),
    #"Renamed Columns" = Table.RenameColumns(ColumnsNeeded,{{parameterTableName, "X"}}),
    #"Filtered Rows" = Table.SelectRows(#"Renamed Columns", each [X] <> null and [X] <> ""),

        ColumnList = Table.ToList ( #"Filtered Rows" ), 
        
        Source = Sql.Database(parameterServerName, parameterDatabase),
        Database = Source{[Schema="dbo",Item= parameterTableName]}[Data],

        #"Removed Other Columns" = Table.SelectColumns( Database , ColumnList )  
            
    in
        #"Removed Other Columns"

 

The Name of the list containing all tables I want to add in PQ.

AllTables

 

Hope you guys can help! 
Maybe @ImkeF 

Regards,
Robin

2 REPLIES 2
ImkeF
Community Champion
Community Champion

Hi @Anonymous ,
not completely sure about the requirement here:
If you say you want to iterate through a list: What shall happen with the results of each iterated item?: Do you want to create one big table from it (with a column with the list items) or do you want to create multiple queries?
If the latter, that's not possible, I'm afraid.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Hi, 

thank you for the quick response. 
The goal was multiple tables indeed. Guess I'll have to stick to adding them manually table per table.

 

Regards,

Robin

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

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.