+ Reply to Thread
Results 1 to 16 of 16

New to Power Query Need to refence a cell for SQL query

  1. #1
    Registered User
    Join Date
    05-08-2018
    Location
    New York
    MS-Off Ver
    2016
    Posts
    20

    New to Power Query Need to refence a cell for SQL query

    I have a power query to a SQL database that works fine as stated below.
    let
    Source = Sql.Database("GVL03", "M2MData01", [Query="select frcpdate from rcmast where frcpdate > 01/01/1900)"])
    in
    Source

    What I want to do is reference a cell and replace the last part "Cast('01/01/1900'... With a date they put into a cell.

    I have created a table with the date in it and is in the Power Query and works

    then have tried to update the first Query Code with

    Source = Sql.Database("GVL03", "M2MData01", [Query="select frcpdate from rcmast where frcpdate > "
    & Excel.CurrentWorkbook(){[Name="BeginDate"]}[Content]{0}[BeginDate])])

    I get Error: Expression.Error: We cannot apply operator & to types Text and DateTime

    Any help would be appreciated, Thanks.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: New to Power Query Need to refence a cell for SQL query

    You can do something like this:
    Please Login or Register  to view this content.
    Cell B1 could contain a formula like: =TODAY() - 30 (to get last 30 days)
    and Cell B2 is = Text(B1,"dd/mm/yyyy")
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Registered User
    Join Date
    05-08-2018
    Location
    New York
    MS-Off Ver
    2016
    Posts
    20

    Re: New to Power Query Need to refence a cell for SQL query

    Confused. I can do a Dim statement inside a Power Query?

  4. #4
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: New to Power Query Need to refence a cell for SQL query

    My apologies. I assumed that this was part of a macro.

  5. #5
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Cool Re: New to Power Query Need to refence a cell for SQL query

    maybe try to define parameter, eg.
    NR is a number (in this case this is a year) but whole string is a text so change number to text

    Please Login or Register  to view this content.
    this is example only so adapt it to your needs

  6. #6
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: New to Power Query Need to refence a cell for SQL query


  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: New to Power Query Need to refence a cell for SQL query

    Is BeginDate a named range with the criteria you want to use in the query?
    If posting code please use code tags, see here.

  8. #8
    Registered User
    Join Date
    05-08-2018
    Location
    New York
    MS-Off Ver
    2016
    Posts
    20

    Re: New to Power Query Need to refence a cell for SQL query

    I will try to figure this out. If you could explain the let statements to me it might be helpful. It appears I can have several lets yes?

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: New to Power Query Need to refence a cell for SQL query

    This should actually be straightforward but I think we need to know what BeginDate actually refers to.

    Is it a column in a table that refers to multiple values?

    Is it a single value in a cell?

  10. #10
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168
    Last edited by sandy666; 05-09-2018 at 09:57 AM.

  11. #11
    Registered User
    Join Date
    05-08-2018
    Location
    New York
    MS-Off Ver
    2016
    Posts
    20

    Re: New to Power Query Need to refence a cell for SQL query

    OK, I will investigate tomorrow. Leaving for the day. Have to reboot servers tonight.

  12. #12
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Cool Re: New to Power Query Need to refence a cell for SQL query

    you can test:

    before
    Please Login or Register  to view this content.
    after
    Please Login or Register  to view this content.
    not tested!!! but maybe it will give you the right way

    I defined parameters randomly so don't laugh
    Last edited by sandy666; 05-09-2018 at 06:55 PM.

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: New to Power Query Need to refence a cell for SQL query

    Have you tried formatting the date parameter, i.e. Excel.CurrentWorkbook(){[Name="BeginDate"]}[Content]{0}[BeginDate])] here?

    Source = Sql.Database("GVL03", "M2MData01", [Query="select frcpdate from rcmast where frcpdate > "
    & Excel.CurrentWorkbook(){[Name="BeginDate"]}[Content]{0}[BeginDate])])

    You could use Power Query's Date.ToText for that.

    Source = Sql.Database("GVL03", "M2MData01", [Query="select frcpdate from rcmast where frcpdate > "
    & Date.ToText(Excel.CurrentWorkbook(){[Name="BeginDate"]}[Content]{0}[BeginDate])], "yyyy-mm-dd"))

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: New to Power Query Need to refence a cell for SQL query

    Not pretty but it worked for me.

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    05-08-2018
    Location
    New York
    MS-Off Ver
    2016
    Posts
    20

    Re: New to Power Query Need to refence a cell for SQL query

    NORIE YOU ARE A GENIUS!!!! Thank you so much. I can code in VBA and VB but I have never worked in Power Query until now. Thank you Thank you Thank you. And to all others, Thanks for the training sessions.

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: New to Power Query Need to refence a cell for SQL query

    I've only really started using Power Query and it seems quite complicated for some things.

    I mean look at what I needed just to get the date from a cell as a string in an acceptable format.

    PS Bet there's a far simpler way to do it, not found it yet though.

+ 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. Sub-Forum for Power Query & Power BI?
    By AliGW in forum Suggestions for Improvement
    Replies: 7
    Last Post: 08-26-2018, 05:25 PM
  2. VBA Macro Power Query with weblink and name from two cell value
    By Speedio in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-01-2018, 01:02 PM
  3. Replies: 0
    Last Post: 04-05-2018, 01:16 AM
  4. error in power query & power pivot
    By Baldev Kumar in forum Excel Charting & Pivots
    Replies: 8
    Last Post: 01-03-2018, 01:34 AM
  5. Replies: 2
    Last Post: 12-08-2017, 03:13 PM
  6. Power Query, Native Query Paramter
    By Kyle123 in forum Excel General
    Replies: 0
    Last Post: 12-08-2017, 09:29 AM
  7. MS Query to Power Query
    By PhilipJPSmith in forum Office 365
    Replies: 1
    Last Post: 09-06-2017, 10:39 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