+ Reply to Thread
Results 1 to 10 of 10

PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

  1. #1
    Registered User
    Join Date
    07-25-2019
    Location
    Salt Lake City, UT
    MS-Off Ver
    2016 32 bit
    Posts
    21

    PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

    I want to use a list of Excel table values to populate a table in a SQL Query within PowerQuery in Excel.

    I have a query (simplified) that looks like the following:

    (see attached image. The forum won't allow me to post the code within the BBC at all (shows up as SQL code), and won't let me post it as a screenshot inline until I've had more posts.) <--mini-rant (But I do understand why.)<--mini-reconciliation to my lot in life.


    Currently, I have an Excel table containing the list I need. I have brought it into PowerQuery as a table, set my field-value type to "ABC", filtered the table down to what I want, and drilled down on one column with the values I want (which turns into a list).

    Now, I want to use my list to populate the section in my code above "[Need code for here]" so that the results of the SQL query will show up in Excel as the result of my MS SQL query.

    This logic is basically serving as a filter list (via a join) for a larger query.
    Attached Images Attached Images
    Last edited by joeyslaptop; 10-24-2019 at 12:54 PM.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

    Have a read of thread below. I think this is similar to what you are trying to do?

    https://www.excelforum.com/excel-pro...cel-range.html
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    07-25-2019
    Location
    Salt Lake City, UT
    MS-Off Ver
    2016 32 bit
    Posts
    21

    Re: PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

    Thank you. It's really close. It's conceptually what I'm trying to do, but have converted my table into a list in PowerQuery. So, I just want to call on it as a list rather than calling on my preformatted source table.

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

    Ok, so you have query result as list already.

    In that case in your query to SQL, you can reference that query.

    Let's say that your list is named "ListA".
    You can use something like...
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-25-2019
    Location
    Salt Lake City, UT
    MS-Off Ver
    2016 32 bit
    Posts
    21

    Re: PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

    Wow - it's a real struggle to post here with all of the restrictions on new members and the "SQL Injection" warning. I can't post code or links. Please ignore my "..."s.

    Could the issue I'm encountering now be based on the INSERT... INTO clause? I have it as INSERT... INTO... MyTable VALUES... . The Values method requires the delimiters to look like this: ('thevalue'), ('thenextvalue')
    Last edited by joeyslaptop; 10-24-2019 at 05:16 PM.

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,860

    Re: PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

    type your links out instead of copying and pasting. You should be able to post code and surround it with code tags. Look at the rules for instructions for that. As to uploading files, do not use the paperclip--it is not functional. Click on Go Advanced in the lower right hand corner of the reply window and follow the wizard instructions.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  7. #7
    Registered User
    Join Date
    07-25-2019
    Location
    Salt Lake City, UT
    MS-Off Ver
    2016 32 bit
    Posts
    21

    Re: PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

    My SQL is using the following:

    Declare atTable as TABLE(ColumnHeaderName as varchar(255))
    INSERT... INTO atTable
    VALUES ('somevalue'), ('somevalue2'), ('SomeValue3') and so on;

    So, my atTable gets populated with the Excel table list values. I think that's the only problem left is figuring out how to append the "(" on the first value from the list. IOW, I think that I need some kind of concatenation of my values followed by the delimiter.

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

    Oh, didn't realize you are trying to "INSERT" into SQL table using M query code.

    DON'T

    This is extremely bad idea, as you can insert undesired data into table. It isn't easy to control it.
    As any change to query code will have the query sample refresh and it will insert each time.

    Having said that, to construct the string, you'd use List.Transform function.
    Ex:
    Please Login or Register  to view this content.
    EDIT: Instead, create API endpoint to perform insert operation on the server and use VBA or some other means to send info to endpoint.
    Last edited by CK76; 10-25-2019 at 09:31 AM.

  9. #9
    Registered User
    Join Date
    07-25-2019
    Location
    Salt Lake City, UT
    MS-Off Ver
    2016 32 bit
    Posts
    21

    Thumbs up Re: PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

    It worked! Thanks so much. I'm bookmarking this page for future reference.

    The application for this was inserting it into a temporary table (at)TempTable - not for a true INSERT. I've tried that before via VBA, and found that the results didn't always come through correctly on the other end. Luckily, I was testing and not working with production tables.

    This Excel table is being used as a join on the main dataset being output to another worksheet. The temp table is created at the beginning of the query, and then is used as a "LEFT JOIN (at)TempTable B ON A.description like ('%' & B.ColumnHeaderName & '%')".

    It creates a list of search terms that can be found within the text of a varchar-formatted field.

    So, an Excel user can type a list of terms into column A of the filters table, and then refresh the query results. The results look like this:

    Filter Criteria
    ---------------
    Ball
    Bat
    Bas


    Orig Dataset:
    Baseball | Ball
    Baseball | Mitt
    Baseball | Bat
    Baseball | Bases
    Baseball | Field
    Football | Football
    Football | Pads
    Football | Helmet
    Golf | Golf Ball
    Golf | Tee
    Golf | Basic Golf Cart


    Results after filter:
    Baseball | Ball
    Baseball | Bat
    Baseball | Bases
    Football | Football
    Golf | Golf Ball
    Golf | Basic Golf Cart


    I checked the results, and it appears to work perfectly.
    Last edited by joeyslaptop; 10-25-2019 at 01:37 PM.

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: PowerQuery in Excel: Use an Excel table to populate SQL table in PowerQuery

    ...not for a true INSERT...
    Ah, that makes more sense.

    You are welcome and thanks for the rep.

    If you are satisfied with the solution provided. Please mark the thread as solved by using thread tools found at top of your initial post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel - Powerquery Help with setting up Authentication through API
    By Desotronics in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-23-2019, 10:51 AM
  2. [SOLVED] Duplicate a pivot table built with a PowerQuery connection
    By FAZIWAZI in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 07-10-2019, 07:36 PM
  3. Excel dashboard with powerquery on a shared drive folder?
    By JimmyJimJim in forum Excel General
    Replies: 2
    Last Post: 05-22-2019, 05:03 PM
  4. Replies: 1
    Last Post: 05-11-2018, 02:54 PM
  5. PowerQuery: Expand all columns after table join
    By Barslund in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-25-2018, 04:59 AM
  6. Where / How to get PowerQuery and PowerPivot for Excel 2010?
    By Xceller in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-29-2014, 12:58 PM
  7. Question about PowerQuery in Excel
    By venumadhavchagarla in forum Excel General
    Replies: 0
    Last Post: 05-07-2014, 04:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1