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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
schdef
Frequent Visitor

Parameterized SQL Query with query folding

I have a table loaded to my data model containing IDs. I want use these IDs to filter another query connecting to a SQL table. I tried to merge the SQL query with the ID table but then the query does not get folded as it should (which is a problem as my SQL source has millions of rows). Any practice that makes sense thinking of performance in particular... Thxs much!!

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hi @schdef,

 

You can tansfrom your data to text, then use it into sql query.

 

Sample: Convert region records to text.

 

let
    Source=data,
    Region = "'"&Text.Combine(List.Distinct(Source[Region]),"','")&"'"
in
    Region

 

Capture.PNGCapture2.PNG

 

Insert into sql query:

 

let
Source = Sql.Database("xxxxx", "xxxxx", [Query="SELECT * FROM Sales WHERE Region In ("&Region&")"])
in
Source

 

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

7 REPLIES 7
v-shex-msft
Community Support
Community Support

Hi @schdef,

 

You can tansfrom your data to text, then use it into sql query.

 

Sample: Convert region records to text.

 

let
    Source=data,
    Region = "'"&Text.Combine(List.Distinct(Source[Region]),"','")&"'"
in
    Region

 

Capture.PNGCapture2.PNG

 

Insert into sql query:

 

let
Source = Sql.Database("xxxxx", "xxxxx", [Query="SELECT * FROM Sales WHERE Region In ("&Region&")"])
in
Source

 

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

How would you do this for more than 1000 items in a list?

I am trying to send a list back to Oracle and there is a 1000 item limit in the where clause.

If anyone has any ideas about this or has gotten around it in any way please let me know.

I am very new to power query and welcome all feedback.

OR the requests together

 

where x in (,,,,,)

or x in (,,,,,)

or x in (,,,,,)

 

etc.

I am working in Excel Power Pivot, my parameter list is generated from a table in the worksheet. The list generates well, and it displays correctly in the Query that is supposed to go out to SQL. Everything looks good, except, I am running into  a Formula.Firewall error message complaining that my step 'Source' references other queries and I need to rebuild it.

 

For months, I have been trying to figure out a way to fold dynamic parameters back to SQL. This is the closest I have gotten. I can see how this should work. I can't get around the error message though.

 

Is there something I need to be aware of when connecting to the database? Has something changed in the software that prevents this from folding back as intended? Are there better ways today to accomplish this task?

Anonymous
Not applicable

Even i have same error refelecting ? I couldnt find solution for that . Is there anyother way to do it ?

Anonymous
Not applicable

Thanks Xiaoxin! Was hoping to get this adressed with query folding and applying filters dynamically but your solution works as well.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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