Home

Create dynamic Power Query SQL by using Excel table column?

%3CLINGO-SUB%20id%3D%22lingo-sub-211504%22%20slang%3D%22en-US%22%3ECreate%20dynamic%20Power%20Query%20SQL%20by%20using%20Excel%20table%20column%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-211504%22%20slang%3D%22en-US%22%3E%3CP%3EIs%20it%20possible%20to%20include%20the%20contents%20of%20an%20entire%20table%20column%20within%20the%20SQL%20code%20in%20Power%20Query%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EBackground%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EI%20created%20a%20spreadsheet%20to%20maintain%20an%20inventory%20of%20devices%26nbsp%3Bby%20serial%20number%20(sn).%20The%20inventory%20is%20in%20an%20Excel%20table.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESeparately%2C%20I%20use%20Power%20Query%20to%20connect%20to%20an%20external%20DB%20(Redshift)%20in%20order%20to%20return%20data%20only%20about%20the%20sn%20items%20I%20supply.%20A%20simplified%20version%20of%20the%20SQL%20I%20use%20is%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eselect%20sn%2C%20data2%20from%20table1%3CBR%20%2F%3Ewhere%3CBR%20%2F%3Edate_key%20BETWEEN%20'2018-01-01'%20AND%20GETDATE()%3CBR%20%2F%3Eand%3CBR%20%2F%3E(%3CBR%20%2F%3Esn%20%3D%20'AA001'%20OR%3CBR%20%2F%3Esn%20%3D%20'AA006'%20OR%3CBR%20%2F%3Esn%20%3D%20'AA012'%20OR%3CBR%20%2F%3Esn%20%3D%20'AA025'%3CBR%20%2F%3E)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20the%20list%20of%20sn's%20change%2C%20I%20need%20to%20paste%20the%20new%20list%20into%20the%20SQL.%26nbsp%3B%20To%20simplify%20things%20(because%20my%20list%20is%20hundreds%20of%20items%20long)%20I%20created%20a%20second%20table%20using%20Power%20Query%20to%20build%20the%20list%20of%26nbsp%3Bthe%20exact%20sn's%20I%20need%20based%20on%20various%20filters.%20Once%20the%20table%20is%20built%20by%20Power%20Query%20in%20Excel%2C%20I%20then%20use%20the%20following%20formula%20to%20add%20the%20proper%20syntax%20to%20the%20list%20of%20serial%20numbers%2C%20which%20also%20excludes%20the%20last%20%22OR%22%20statement%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3D%22sn%20%3D%20'%22%26amp%3B%5Bsn%5D%26amp%3BIF(INDIRECT(%22A%22%20%26amp%3B%20ROW()%20%2B%201)%3D%22%22%2C%22'%22%2C%22'%20OR%22)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eand%20creates%20the%20%22Query%22%20column%20as%20shown%20below%20based%20on%20the%20input%20from%20%22sn%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Esn%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BQuery%3CBR%20%2F%3EAA001%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20sn%20%3D%20'AA001'%20OR%3CBR%20%2F%3EAA006%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%3CSPAN%3Esn%3C%2FSPAN%3E%20%3D%20'AA006'%20OR%3CBR%20%2F%3EAA012%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%3CSPAN%3Esn%3C%2FSPAN%3E%20%3D%20'AA012'%20OR%3CBR%20%2F%3EAA025%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%3CSPAN%3Esn%3C%2FSPAN%3E%20%3D%20'AA025'%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can%20now%20cut%2Fpaste%20the%20%22Query%22%20column%20into%20Power%20Query%20to%20update%20my%20list%20of%20serial%20numbers.%20While%20I've%20automated%20this%20quite%20a%20lot%2C%20there%20is%20one%20last%20thing%20I'd%20like%20it%20to%20do%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EQuestion%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3ECan%20I%20somehow%26nbsp%3Breference%26nbsp%3Bthe%20%22Query%22%20column%20in%20my%20SQL%20statement%20so%20that%20I%20no%20longer%20need%20to%20directly%20update%20the%20SQL%20statement%20as%20new%20devices%20are%20added%20or%20removed%20from%20the%20device%20inventory%3F%20Since%20the%20Query%20column%20contains%20everything%20I%20need%20for%20the%20data%20between%20the%20parenthesis%20in%20Power%20Query%2C%20referencing%20it%20somehow%20should%20allow%20the%20SQL%20to%20become%20dynamic.%20Is%20this%20possible%3F%20Or%20is%20there%20an%20even%20better%20way%20to%20do%20what%20I'm%20doing%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-211504%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%20Desktop%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPower%20Query%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-215622%22%20slang%3D%22en-US%22%3ERe%3A%20Create%20dynamic%20Power%20Query%20SQL%20by%20using%20Excel%20table%20column%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-215622%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Michael%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20may%20use%20parameter%20in%20sql-native%20way%20if%20use%20Value.NativeQuery%20like%3C%2FP%3E%0A%3CPRE%3Elet%0A%20%20%20%20Parameter%20%3D%20%5BpFilterSN%20%3D%20Filter%5D%2C%0A%20%20%20%20Source%20%3D%20Sql.Database(%22MyServer%22%2C%20%22MyDatabase%22)%2C%0A%20%20%20%20Query%20%3D%20Value.NativeQuery(Source%2C%0A%20%20%0A%2F*%20SQL%20starts-----------*%2F%20%22%0Adeclare%20%0A%40myAnotherParameter%20bit%20%3D%200%3B%20--here%20could%20be%20any%20static%20parameter(s)%0A%0A%20%20%20SELECT%20*%20%20%0A%20%20%20%20%20%20FROM%20MyTable%0A%20%20%20%20%20%20WHERE%0A%20%20%20%40MyAnotherParameter%20%3D%200%20AND%20SN%20%3D%20%40pFilterSN%0A%20%20%20%20%20%20ORDER%20BY%20SN%0A%22%20%20%2F*%20SQL%20ends--*%2F%2C%20Parameter)%2C%0A%0A%20%20%20%20AnotherSort%20%3D%20Table.Sort(Query%2C%7B%7B%22SN%22%2C%20Order.Ascending%7D%7D)%2C%0A%0Ain%0A%20%20%20%20AnotherSort%0A%3C%2FPRE%3E%0A%3CP%3Eor%20combine%20query%20string%20with%20your%20parameter%20string%20like%3C%2FP%3E%0A%3CPRE%3Elet%0A%0A%20%20%20%20pFilterSN%20%3D%20Filter%2C%0A%20%20%20%20Source%20%3D%20Sql.Database(%22MyServer%22%2C%20%22MyDatabase%22%2C%0A%20%20%20%20%20%20%5BQuery%3D%22%0Adeclare%20%0A%40myAnotherParameter%20bit%20%3D%200%3B%20--here%20could%20be%20any%20static%20parameter(s)%0A%20%20%20SELECT%20*%0A%20%20%20%20%20%20FROM%20MyTable%0A%20%20%20%20%20%20WHERE%0A%20%20%20%40MyAnotherParameter%20%3D%200%20AND%20SN%20%3D%20%22%20%26amp%3B%20pFilterSN%20%26amp%3B%20%22%0A%20%20%20%20%20%20ORDER%20BY%20SN%22%5D)%2C%0A%20%20%20%20AnotherSort%20%3D%20Table.Sort(Source%2C%7B%7B%22SN%22%2C%20Order.Ascending%7D%7D)%2C%0Ain%0A%20%20%20%20AnotherSort%0A%20%20%20%20%20%20%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-215493%22%20slang%3D%22en-US%22%3ERe%3A%20Create%20dynamic%20Power%20Query%20SQL%20by%20using%20Excel%20table%20column%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-215493%22%20slang%3D%22en-US%22%3E%3CP%3ESergei%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat's%20brilliant!%20I%20didn't%20think%20about%20creating%20a%20connection%20that%20contains%20the%20string%20I%20need.%20That's%20going%20to%20work%20great!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20last%20part%20I%20need%20is%20the%20syntax%20for%20including%20Query1%20within%20my%20custom%20SQL%20statement%20as%20part%20of%20my%20WHERE%20parameter.%20How%20do%20I%20reference%20that%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-211670%22%20slang%3D%22en-US%22%3ERe%3A%20Create%20dynamic%20Power%20Query%20SQL%20by%20using%20Excel%20table%20column%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-211670%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Michael%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20may%20generate%20you%20parameter%20to%20filter%20sn%3As%20by%20Power%20Query%20(let%20say%20%22Filter%22)%20like%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Elet%0A%20%20%20%20Source%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22Table1%22%5D%7D%5BContent%5D%2C%0A%20%20%20%20ToText%20%3D%20Table.TransformColumnTypes(Source%2C%7B%7B%22sn%22%2C%20type%20text%7D%7D)%2C%0A%20%20%20%20AddString%20%3D%20Table.AddColumn(ToText%2C%20%22Custom%22%2C%20each%20%22sn%20%3D%20%22%20%26amp%3B%20%22'%22%20%26amp%3B%20%5Bsn%5D%20%26amp%3B%20%22'%20OR%20%22)%2C%0A%20%20%20%20RemoveSN%20%3D%20Table.SelectColumns(AddString%2C%7B%22Custom%22%7D)%2C%0A%20%20%20%20Custom%20%3D%20Text.Combine(RemoveSN%5BCustom%5D%2C%20%22%22)%2C%0A%20%20%20%20Parameter%20%3D%20Text.Start(Custom%2CText.Length(Custom)-4)%0A%0Ain%0A%20%20%20%20Parameter%3C%2FPRE%3E%0A%3CP%3Eif%20serial%20numbers%20you'd%20like%20to%20select%20are%20in%20Table1.%20Text%20string%20with%20your%20filter%20is%20the%20result%20of%20it%3C%2FP%3E%0A%3CPRE%3EpFilterSN%20%3D%20Filter%3C%2FPRE%3E%0A%3CP%3Eand%20combine%20with%20your%20SQL%20query.%20The%20only%20point%20you%20shall%20use%20Ignore%20privacy%20setting.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Michael Rothstein
New Contributor

Is it possible to include the contents of an entire table column within the SQL code in Power Query?

 

Background

I created a spreadsheet to maintain an inventory of devices by serial number (sn). The inventory is in an Excel table.

 

Separately, I use Power Query to connect to an external DB (Redshift) in order to return data only about the sn items I supply. A simplified version of the SQL I use is:

 

select sn, data2 from table1
where
date_key BETWEEN '2018-01-01' AND GETDATE()
and
(
sn = 'AA001' OR
sn = 'AA006' OR
sn = 'AA012' OR
sn = 'AA025'
)

 

When the list of sn's change, I need to paste the new list into the SQL.  To simplify things (because my list is hundreds of items long) I created a second table using Power Query to build the list of the exact sn's I need based on various filters. Once the table is built by Power Query in Excel, I then use the following formula to add the proper syntax to the list of serial numbers, which also excludes the last "OR" statement:

 

="sn = '"&[sn]&IF(INDIRECT("A" & ROW() + 1)="","'","' OR")

 

and creates the "Query" column as shown below based on the input from "sn"

 

sn             Query
AA001      sn = 'AA001' OR
AA006       sn = 'AA006' OR
AA012       sn = 'AA012' OR
AA025       sn = 'AA025'

 

I can now cut/paste the "Query" column into Power Query to update my list of serial numbers. While I've automated this quite a lot, there is one last thing I'd like it to do:

 

Question

Can I somehow reference the "Query" column in my SQL statement so that I no longer need to directly update the SQL statement as new devices are added or removed from the device inventory? Since the Query column contains everything I need for the data between the parenthesis in Power Query, referencing it somehow should allow the SQL to become dynamic. Is this possible? Or is there an even better way to do what I'm doing?

 

Thanks!

4 Replies

Hi Michael,

 

You may generate you parameter to filter sn:s by Power Query (let say "Filter") like 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ToText = Table.TransformColumnTypes(Source,{{"sn", type text}}),
    AddString = Table.AddColumn(ToText, "Custom", each "sn = " & "'" & [sn] & "' OR "),
    RemoveSN = Table.SelectColumns(AddString,{"Custom"}),
    Custom = Text.Combine(RemoveSN[Custom], ""),
    Parameter = Text.Start(Custom,Text.Length(Custom)-4)

in
    Parameter

if serial numbers you'd like to select are in Table1. Text string with your filter is the result of it

pFilterSN = Filter

and combine with your SQL query. The only point you shall use Ignore privacy setting.

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
      

 

@Sergei Baklan 

@Michael Rothstein 

 

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=16

 

The 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