Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
rhawlor
Helper II
Helper II

Reference a value in a table to use in another SQL power Query

Hi All

 

I have a value held in another PQ table. 1 row 1 column containing an invoice number. - Is it possible to use this particular invoice number and use in a SQL query without having to go into the query and manually changing the invoice number?

 

e.g.

Table 1 with 1 column 1 row value = ABC123

Existing SQL Query select X,Y,Z from Database where value = Value from Table 1 (ABC123)

 

I did try to search for this, but I wasnt sure how to word this search for this this type of function, so apologies if this already exists. 

 

thank you

 

Rob

 

1 ACCEPTED SOLUTION

Double quote missing before the closing square bracket I think. 

I notice you're missing a table alias for your SHIP_DATE_RETURN, not sure if that will cause you any abiguity issues.

This is just the query portion, make sure it goes inside the correct ().

[Query="
SELECT rd.SERIAL_NUMBER
     , rd.REF_NUM
     , rd.MODEL_ASC
     , pm.MODEL
     , rd.MOTO_PART_NUMBER
     , rd.ASC_SITE_ID
     , ras.ASC_SITE_NAME
     , rd.REPAIR_TYPE
     , acm.ASC_CUSTOMER_NAME
     , rd.WARR_CODE
     , rd.RMA_CREATION_DATE
     , rd.EXPIRY_DATE
     , rd.RECV_DATE
     , rd.SHIP_DATE_RETURN
     , rd.UNIT_STATUS
     , rd.PROJECT_NAME
     , rd.FINAL_UNIT_DETERMINATION
     , rd.INVOICE_NUMBER
     , rd.INVOICE_DATE
     , ROUND((SHIP_DATE_RETURN - rd.RECV_DATE)) AS AGE
     , (rd.RECV_DATE + 30)                      AS TAT
     , rd.LABCOST
     , rd.FREIGHT_COST
FROM rdwadmin.rdw_repair_details rd
   , rdwadmin.rdw_part_master pm
   , rdwadmin.rdw_asc_customer_master acm
   , rdwadmin.rdw_asc_site ras
WHERE rd.moto_part_number = pm.moto_part_number
      AND rd.ASC_CUSTOMER_NUMBER = acm.ASC_CUSTOMER_NUMBER
      AND rd.ASC_SITE_ID = ras.ASC_SITE_ID
      AND rd.INVOICE_NUMBER = '"& #"Current-Invoice"[CurrentInvoice]{0} &"'

"]

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

View solution in original post

19 REPLIES 19
smpa01
Super User
Super User

@rhawlor  it works for me not sure why it does not for you.

 

Can you delete the follwoing portion and try

smpa01_0-1638198002118.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

I have deleted that portion and added the previous again. Still the same outcome.Capture.JPG

 

When i click ok oi get the below:

Invoice-Details.JPG

Yep, that one got me a few times too.

Try changing the '"&Table[Current-invoice]{0}&"' to '"&Table[#"Current-invoice"]{0}&"'.

The dash can cause a problem when referencing column names.

I recommend not using the dash or other symbols in column names.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

I have amended the code and now get past the original error and now get the Table wasnt recogonised. 

 

Capture.JPG

 

As per your recommendation, I then went and changed the column name from "Current-Invoice" to "CurrentInvoice" to see if this would help, but as it is trying to find the actual table i don't think at this stage the column name is the problem.

 

Below is what the Current-Invoice Table with CurrentInvoice column looks like:

 

Capture.JPG

 

Below is the final code with SQL:

Capture.JPG

KNP_0-1638265608555.png

This looks like your issue. Helps now that I know what the table is called. 😉

 

Change the above to...

'"& #"Current-Invoice[CurrentInvoice]{0} &"'

 

If you renamed your query you wouldn't need to reference the table with the #"".

Pay close attention to the quotes. Opens with single, double. Closes with double, single.

Make sure you don't remove the last ]) that closes off the "Query".

 

I think this will get you there.

 

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

So i amended the name, but it didnt like the extra ' at the end as you can see...

 

Capture.JPG

 

I removed the extra ' from the end the, PQ then started loading then came up with the below:

 

Capture.JPG

 

Though the name is spelled correctly?

Capture.JPG

Hi @KNP - Do you have any further suggestions on this, I think we are so close on this.

@rhawlor - sorry, I think this is my typo that has confused things.

I think it should be...

'"& #"Current-Invoice"[CurrentInvoice]{0} &"'

It was missing the double quote to close off the table reference.

Give that a try and let me know how you go.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

@KNP Thanks for the reply. 

 

I have added the change: Capture.JPGIt says there is an invalid literal

Capture.JPG

 

Here is the query section, not sure if this makes it easier?

 

[Query="select rd.SERIAL_NUMBER, rd.REF_NUM, rd.MODEL_ASC, pm.MODEL, rd.MOTO_PART_NUMBER, rd.ASC_SITE_ID, ras.ASC_SITE_NAME, rd.REPAIR_TYPE, acm.ASC_CUSTOMER_NAME, rd.WARR_CODE, rd.RMA_CREATION_DATE, rd.EXPIRY_DATE, rd.RECV_DATE, rd.SHIP_DATE_RETURN, rd.UNIT_STATUS, rd.PROJECT_NAME, rd.FINAL_UNIT_DETERMINATION, rd.INVOICE_NUMBER, rd.INVOICE_DATE, round((SHIP_DATE_RETURN - rd.RECV_DATE)) as AGE, (rd.RECV_DATE+30) AS TAT, rd.LABCOST, rd.FREIGHT_COST #(lf)from rdwadmin.rdw_repair_details rd, rdwadmin.rdw_part_master pm, rdwadmin.rdw_asc_customer_master acm, rdwadmin.rdw_asc_site ras#(lf)where rd.moto_part_number = pm.moto_part_number#(lf)AND rd.ASC_CUSTOMER_NUMBER = acm.ASC_CUSTOMER_NUMBER #(lf)AND rd.ASC_SITE_ID = ras.ASC_SITE_ID #(lf)AND rd.INVOICE_NUMBER = '"& #"Current-Invoice"[CurrentInvoice]{0} &"'])

 

Thanks, really apprecite your help on this 🙂

Double quote missing before the closing square bracket I think. 

I notice you're missing a table alias for your SHIP_DATE_RETURN, not sure if that will cause you any abiguity issues.

This is just the query portion, make sure it goes inside the correct ().

[Query="
SELECT rd.SERIAL_NUMBER
     , rd.REF_NUM
     , rd.MODEL_ASC
     , pm.MODEL
     , rd.MOTO_PART_NUMBER
     , rd.ASC_SITE_ID
     , ras.ASC_SITE_NAME
     , rd.REPAIR_TYPE
     , acm.ASC_CUSTOMER_NAME
     , rd.WARR_CODE
     , rd.RMA_CREATION_DATE
     , rd.EXPIRY_DATE
     , rd.RECV_DATE
     , rd.SHIP_DATE_RETURN
     , rd.UNIT_STATUS
     , rd.PROJECT_NAME
     , rd.FINAL_UNIT_DETERMINATION
     , rd.INVOICE_NUMBER
     , rd.INVOICE_DATE
     , ROUND((SHIP_DATE_RETURN - rd.RECV_DATE)) AS AGE
     , (rd.RECV_DATE + 30)                      AS TAT
     , rd.LABCOST
     , rd.FREIGHT_COST
FROM rdwadmin.rdw_repair_details rd
   , rdwadmin.rdw_part_master pm
   , rdwadmin.rdw_asc_customer_master acm
   , rdwadmin.rdw_asc_site ras
WHERE rd.moto_part_number = pm.moto_part_number
      AND rd.ASC_CUSTOMER_NUMBER = acm.ASC_CUSTOMER_NUMBER
      AND rd.ASC_SITE_ID = ras.ASC_SITE_ID
      AND rd.INVOICE_NUMBER = '"& #"Current-Invoice"[CurrentInvoice]{0} &"'

"]

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

Good spot on the table ref, it wasnt an issue as i have used this via sql into excel previously, but now i know its there I have amended it 🙂

 

I think we are almsot there! It started to work and then came up with something i have never seen before:

 

Capture.JPG

 

 

@KNP Thank you this is now fixed. I did the old google on the firewall piece and it was down to the privacy settings. - I can't thank you enough as this will save so much time!

 

Anyone else who comes across the firewall issue, just amend your privacy settings.

KNP
Super User
Super User

From memory, the syntax for the value in SQL is '" &Value& "'.

Depending on your exact scenario, you may want to drill down on that single value table to make it easier to reference, e.g. right click on the single value and choose 'drill down'.

 

Once you've drilled down, say the query of single value is called myValue.

 

I think what you're looking for is something like...

 

SELECT x, y, z FROM myTable WHERE myColumn = '" &myValue& "'

 

 

Hope this helps.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x

@rhawlor  did you have a chance look into above?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hi KNP

 

Thanks for the reply. This is the current scenario is:

 

We have an Excel spreadsheet which has a powerquery into our oracle database, each time I want to validate an invoice, I have to go into the SQL query and manually change the invoice number for which I want to validate then refresh the query afterwards.

 

What i was thinking was add the invoice number to a single tab in Excel, then have this loaded as a PQ table like below:

 

Capture.JPG

 

In a seperate table I have SQL Query which pulls all the rows of data that relate the invoice reference that is held in 'Current-Invoice' after hitting refresh.

 

Hope that all makes sense 🙂

 

@KNP  and if OP wants to pass on directly a query to SQL

 

 

let
    Source = Sql.Database("serverName", "dbName", [Query="select * from tbl where WO='"&Table[C1]{0}&"'"])
in
    Source

 

 

 

Table is following

smpa01_0-1637874858245.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hi @smpa01 

 

Apologies, i only just read your reply. I have tried to pass to the SQL query and did have a couple of issues with PQ not liking the code, but it seems to have accepted an edited version. 

 

Invoice-Details.JPG

However it does not bring any data back.

Current-Invoice.JPG

 

Invoice.JPG

 

Really appreciate your support 🙂

@rhawlor  can you try this

rd.INVOICE_NUMBER='"&Table[Current-invoice]{0}&"'"
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

@smpa01 

 

This didnt work and caused "Expression.SyntaxError: Invalid identifier."Invoice-Details.JPG

517bc8b5-b2c7-4122-98bf-35cf4345d81b.jpg

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors