Deprecated: Creation of dynamic property CF\WordPress\DataStore::$logger is deprecated in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php on line 23
Deprecated: Creation of dynamic property CF\WordPress\Proxy::$pluginAPI is deprecated in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/Proxy.php on line 31
Deprecated: file_get_contents(): Passing null to parameter #2 ($use_include_path) of type bool is deprecated in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/codelights-shortcodes-and-widgets/codelights.php on line 20
Warning: session_start(): Session cannot be started after headers have already been sent in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/automatic-video-posts/vendor/ternstyle/wordpress/src/class/wordpress.php on line 53
Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758
Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758
Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758
Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758
Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758
Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758
Deprecated: Creation of dynamic property ternplugin\youtube_video::$post is deprecated in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/automatic-video-posts/vendor/ternstyle/plugin/src/class/youtube_video.php on line 64
Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758
Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758
{"id":2088,"date":"2020-07-22T09:12:19","date_gmt":"2020-07-22T16:12:19","guid":{"rendered":"http:\/\/bonbonsguide.com\/wp\/?p=2088"},"modified":"2023-11-11T08:53:11","modified_gmt":"2023-11-11T15:53:11","slug":"pass-a-parameter-to-sql-query-in-power-query","status":"publish","type":"post","link":"https:\/\/bonbonsguide.com\/wp\/blog\/2020\/07\/pass-a-parameter-to-sql-query-in-power-query\/","title":{"rendered":"Solved: Pass a Parameter to a SQL Query in Power Query"},"content":{"rendered":"I’ve done a fair amount of research around dynamic and parameterized queries, specifically around an Excel query parameter, as in trying to pass a parameter to a SQL query in Power Query.\u00a0 <\/strong>After much trial and error, I finally have found a solution that works.\u00a0 Here’s the quick and easy way to do this and harness the power of query folding.\u00a0 This solution works in Excel<\/strong> – the process is a bit different in Power BI.<\/p>\nPlease note that this post assumes you are at least moderately familiar with Power Query and SQL querying. Your mileage may vary depending on your situation.<\/p>\n
In my example, “SParameter” is the name of the parameter I am using, and it represents a store number (retail location identifier). The related field in my SQL table is [Store].<\/p>\n
Create the parameter:<\/strong><\/em><\/p>\n\n- Add a table, with a single record and column.\u00a0 This can be on any tab in your workbook.<\/li>\n
- Add this table into Power Query by selecting the table, then Data > From Table\/Range.\u00a0 When the query editor opens, if necessary, change the data type (in my case I need it to be text).<\/li>\n
- Right click on the record and select “Drill Down”.\u00a0 Since you have only a single record, this step will automatically create a parameter.<\/li>\n
- In the properties, I name this SParameter (you may enter the name of your choice for your parameter).\u00a0 Note that the name is case sensitive.<\/li>\n<\/ul>\n
Create the SQL query:<\/strong><\/em><\/p>\nBuild your SQL query as normal, but note that we are not<\/strong><\/span> going to put a where clause in referencing the new parameter here (see “Why Not Include the Parameter in the SQL Statement” <\/strong><\/em>section below).\u00a0 Here’s my simple query example – notice that I’m not including the store field in the criteria section of the query though this is the field my parameter will ultimately filter on.<\/p>\nSelect * FROM Storelist WHERE OpenDate < GetDate()-365<\/strong><\/p>\nIf you think like me, you may be pausing here – my table has millions of records and I don’t want them all pulling in – don’t worry, this is the beauty of the process.<\/p>\n
\n- Write your SQL statement in Power Query as noted above.<\/li>\n
- When the Query Editor returns the columns and record sampling, for the field you will be using your parameter, filter with any single value (Text Filters > Equals). This is simply a placeholder, to create the Power Query M formula for the next step.<\/li>\n
- You will now see that filtering in the formula bar.\u00a0<\/li>\n
- Replace the placeholder value with the name of your parameter, removing any quotes from the placeholder.\u00a0 This is how it looks for me: = Table.SelectRows(Source, each [Store] = SParameter<\/strong>).<\/li>\n
- Power Query is smart enough to modify its native query<\/span> to use the parameter, so it’s not going to pull in the millions of records and then filter after the fact.\u00a0 Success!!\u00a0\u00a0<\/strong>This is the power of\u00a0Query Folding!<\/strong><\/li>\n
- Side note – if you are wondering about GetDate()<\/strong>, this is the SQL current system date, so in a way this is technically a dynamic parameter<\/strong> as well.\u00a0 In my case, the query will only return stores with an open date that is older than 365 days based on today’s date<\/span>.\u00a0 You may find this useful for your query as well.<\/li>\n<\/ul>\n
Why Not Include the Parameter in the SQL Statement?<\/strong><\/em><\/p>\nIn the SQL statement, logic would lead us to add the store number\/SParameter as part of the criteria in the select statement.\u00a0 It may seem counter intuitive, but we cannot successfully put the parameter directly into the SQL code\/query.\u00a0 I have tried several times, but the challenge is that you are combining data sources and at this time, this is not allowed due to the built in security (SParameter portion is local, SQL portion is external).<\/p>\n
If you are tempted to test this yourself, the format would be Select * FROM Storelist WHERE Open < GetDate()-365 and Store = “&SParameter&”<\/span>.\u00a0 You will likely run into this error “Formula.Firewall:<\/strong> Query ‘SParameter (2)’ (step ‘Filtered Rows’) references other queries or steps, so it may not directly access a data source. Please rebuild this data combination<\/strong>.”<\/em><\/p>\nI truly hope this post helped you out.\u00a0 If so, please feel free to leave a comment below letting me know so, and if you’d like, add what you’d like me to cover in a future post. Also, feel free to share this with someone else who may find it useful.<\/p>\n
Please bookmark and subscribe to my blog!\u00a0 I am always curating and adding new, relevant content! Thanks so much!<\/p>\n
Also, be sure to check out Dose for Excel (click image below)!\u00a0 Add over 100 functions to Excel to increase your productivity and more!\u00a0 They have a free trial right now, so you can try it out today!\u00a0 Disclosure: this is an affiliate link, so I may earn a small commission if you decide to purchase the add-in.\u00a0 Thanks for supporting my blog!<\/em><\/p>\n\n\n<\/a>\n\n\n\n<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"I’ve done a fair amount of research around dynamic and parameterized queries, specifically around an Excel query parameter, as in trying to pass a parameter to a SQL query in Power Query.\u00a0 After much trial and error, I finally have found a solution that works.\u00a0 Here’s the quick and easy way to do this and […]<\/p>\n","protected":false},"author":1,"featured_media":917,"comment_status":"open","ping_status":"open","sticky":false,"template":"template-gutenberg.php","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":""},"categories":[101,21,51,137,138,217],"tags":[20,185,186,190,143,127,173,187,189,188],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/bonbonsguide.com\/wp\/wp-content\/uploads\/2018\/11\/digital-marketing-1433427_1920.jpg?fit=1920%2C1281&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7QB1z-xG","_links":{"self":[{"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/posts\/2088"}],"collection":[{"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/comments?post=2088"}],"version-history":[{"count":37,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/posts\/2088\/revisions"}],"predecessor-version":[{"id":6305,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/posts\/2088\/revisions\/6305"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/media\/917"}],"wp:attachment":[{"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/media?parent=2088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/categories?post=2088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/tags?post=2088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}