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

Dynamic query from SQL procedure

First part:

I have a query (SQL procedure)

 

Source= Sql.Database("server", "cataloge_item", [Query="EXEC procOderBySampleID @ID = '5134'"])

 

Instead of manually inputing 4-digit value (i.e. 5134) I want to write M code to retrive this value from other query (Query A, column 3). 

 

I solved that in VBA as:

ID1 = Worksheets("1").Cells(i, 3)
query = "procOderBySampleID @ID = '" & ID1 & "'"

 

I am new to M and don't have a clue how to tackle this.

 

Second part: 

Since Query A contains 100s of values I would like to make the query B dynamic in the sense that only selected/filtered data in power BI report (values of query A) are queried in B. Is this possible?

 

And to complicate things further, I would like to limit the query B to 5 results. This is mainly because I do not want to overload SQL proc.

 

Any help greatly appreciated!

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @davidz106 ,

 

I think you can try to load data from SQL Server with specific ID based on the filter of parameters.

Firstly, create a list of parameters with the ID you need.

RicoZhou_0-1655277584956.png

The whold M code.

let
    Source = Sql.Database("Server", "Database"),
    dbo_Sample = 
        let _ID = ID
        in
        Value.NativeQuery(
            Source,
            "SELECT * FROM Sample
            WHERE ID=@ID",
            [ID = _ID])
in
    dbo_Sample

Result is as below. ID =5134, we can load all data in ID =5134 to Power BI.

RicoZhou_1-1655277644521.png

For more details: Passing Parameters To SQL Queries With Value.NativeQuery() In Power Query And Power BI

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @davidz106 ,

 

I think you can try to load data from SQL Server with specific ID based on the filter of parameters.

Firstly, create a list of parameters with the ID you need.

RicoZhou_0-1655277584956.png

The whold M code.

let
    Source = Sql.Database("Server", "Database"),
    dbo_Sample = 
        let _ID = ID
        in
        Value.NativeQuery(
            Source,
            "SELECT * FROM Sample
            WHERE ID=@ID",
            [ID = _ID])
in
    dbo_Sample

Result is as below. ID =5134, we can load all data in ID =5134 to Power BI.

RicoZhou_1-1655277644521.png

For more details: Passing Parameters To SQL Queries With Value.NativeQuery() In Power Query And Power BI

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.