Forum Discussion
Create dynamic Power Query SQL by using Excel table column?
Sergei,
That's brilliant! I didn't think about creating a connection that contains the string I need. That's going to work great!
The last part I need is the syntax for including Query1 within my custom SQL statement as part of my WHERE parameter. How do I reference that?
Hi Michael,
You may use parameter in sql-native way if use Value.NativeQuery like
let Parameter = [pFilterSN = Filter], Source = Sql.Database("MyServer", "MyDatabase"), Query = Value.NativeQuery(Source, /* SQL starts-----------*/ " declare @myAnotherParameter bit = 0; --here could be any static parameter(s) SELECT * FROM MyTable WHERE @MyAnotherParameter = 0 AND SN = @pFilterSN ORDER BY SN " /* SQL ends--*/, Parameter), AnotherSort = Table.Sort(Query,{{"SN", Order.Ascending}}), in AnotherSort
or combine query string with your parameter string like
let pFilterSN = Filter, Source = Sql.Database("MyServer", "MyDatabase", [Query=" declare @myAnotherParameter bit = 0; --here could be any static parameter(s) SELECT * FROM MyTable WHERE @MyAnotherParameter = 0 AND SN = " & pFilterSN & " ORDER BY SN"]), AnotherSort = Table.Sort(Source,{{"SN", Order.Ascending}}), in AnotherSort
- leandrofraMay 29, 2020Copper ContributorHi.
I tried this but I am getting the error "Formula.Firewall: Query 'Query1' (step 'Source') references other queries or steps, so it may not directly access a data source. Please rebuild this data combination.".
The parameter I am using return a text value (a list of codes) that I use as filter with a in clause ("... aufnr in (" & p_filter & ")"
Could you please help me understand why?- SergeiBaklanMay 29, 2020Diamond Contributor
Formula.Firewall it shall be bit another story. You may avoid it by 1) "ignore privacy" settings or 2) rebuild your queries. Simplest way, if query is using as parameter or like in another query, combine them in one. Like
let SourceParameter = ... // here are parameter query steps Source = ... // continue with main query // use SourceParameter somewhere inside in Result
- JonathanLewisMay 20, 2019Copper Contributor
Hi Sergei and Michael, I know this was posted a while but I am hopeful you two can help.
I am trying to do this using the Value.NativeQuery route and am receiving the following conversion error.
DataSource.Error: Microsoft SQL: Conversion failed when converting the nvarchar value '('MyDataStringHere)' to data type int.
Details:
DataSourceKind=SQL
DataSourcePath=sysqlwh1;PPI
Message=Conversion failed when converting the nvarchar value '('MyDataStringHere')' to data type int.
Number=245
Class=16The column I am using the parameter against is data type: int. I am not sure if this is the cause of the error.
My WHERE clause within the NativeQuery looks like this:
WHERE (T3.INTCOLUMN_1 = 1) AND ( T3.INTCOLUMN_2 IN (@Parameter) )
When I manually run this query in SSMS with my copied parameter I have no issues with the data type as either an int value or a text value in the WHERE clause.
Do you know how to resolve this error?
Thanks in advance!
-Jonathan