[英]Data not getting fetched from Source using ODBC Connection and passing the SQL Query from a variable
[英]Passing date variable in Power Query to ODBC source
我設法從 Excel 中提取了一個日期(用戶可以修改)。[一個名為 DateB 的命名范圍]。 我希望能夠將它傳遞給 Power Query 的 sql 部分中的一個變量,以便在將數據傳回之前可以進行各種檢查。 但是,我無法讓變量 @man_Date 接受我提取的日期。
RDP3 在那個階段正確返回 30/06/2020。
但是,在源代碼部分,如果我有“ & RDP3 & ”,我會被告知它不適用於 &s 如果我刪除它們,則會留下錯誤消息“預期令牌逗號”。
這是我在查詢開始時的代碼。 我也在子查詢中使用@Man_Date。
用於 RDP3 的正確語法是什么? @Man_Date 使用手動輸入 (@Man-Date = '2020-06-30') 我嘗試了各種日期格式,但似乎無法讓它工作。 似乎這兩個部分都是獨立工作的。 只是將它們綁在一起似乎是症結所在。
let
//
RDP = Excel.CurrentWorkbook(){[Name="DateB"]}[Content],
RDP2 = Table.TransformColumnTypes(RDP,{{"Column1", type date}}),
RDP3 = RDP2{0}[Column1],
//
Source = Odbc.Query("dsn=xxxxxxxxxxx", "
--PI Payments Due
SET NOCOUNT ON;
DECLARE @man_Date DATETIME --Manual Date
SET @man_Date = " & RDP3 & "
select qry1.*,
case when [Invoice Due Date] > @man_Date or datediff(d,[Invoice Due Date], @man_Date) = 0 then [Sterling Balance Due] else 0 end as 'Current',
case when datediff(d,[Invoice Due Date], @man_Date) > 0 and datediff(d,[Invoice Due Date], @man_Date) <= 30 then [Sterling Balance Due] else 0 end as '0-30',
case when datediff(d,[Invoice Due Date], @man_Date) > 30 and datediff(d,[Invoice Due Date], @man_Date) <= 60 then [Sterling Balance Due] else 0 end as '31-60',
case when datediff(d,[Invoice Due Date], @man_Date) > 60 and datediff(d,[Invoice Due Date], @man_Date) <= 90 then [Sterling Balance Due] else 0 end as '61-90',
case when datediff(d,[Invoice Due Date], @man_Date) > 90 and datediff(d,[Invoice Due Date], @man_Date) <= 120 then [Sterling Balance Due] else 0 end as '91-120',
case when datediff(d,[Invoice Due Date], @man_Date) > 120 then [Sterling Balance Due] else 0 end as 'Over 120'
from (
select VD.vcd_voucherno as 'Invoice No',VD.vcd_voucherdate as 'Date Raised', PTY_NmeParty as 'Name' ,
PTY_ValBuyerCreditLimit as 'Credit Limit',vd.vcd_currencycode as 'Currency',
vd.vcd_amount * case when vd.vcd_debitorcredit = 'D' then -1 else 1 end as 'Invoice Amount',
(coalesce(sum(SOC.sof_crsetoffamount),0) - coalesce(sum(SOD.sof_drsetoffamount),0)) as 'Amount Paid',
我現在了解到我只能將文本傳遞給 sql。 所以我使用下面的細分將日期轉換為 SQL 的文本形式。
let
//
RDC1 = Excel.CurrentWorkbook(){[Name="DateB"]}[Content],
RDC2 = Table.TransformColumnTypes(RDC1,{{"Column1", type date}}),
RDC3 = RDC2{0}[Column1],
RDC4 = Date.ToText(RDC3, "yyyy")&"-"&Date.ToText(RDC3, "MM")&"-"&Date.ToText(RDC3, "dd"),
// Source = Odbc.Query("dsn=xxxxxxxx", "#(lf)#(lf)--PI Payments Due#(lf)SET NOCOUNT ON;#(lf)DECLARE @man_Date DATETIME --Manual Date#(lf)SET @man_Date = **'" &Text.From(RDC4)& "'** #(lf)Select etc
RDC4 部分將其轉換為可用的文本日期並在整個查詢中使用。
希望能幫助別人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.