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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
LaurenceSD
Advocate II
Advocate II

SQL Querey using date parameter

Hi, I've got an SQL query that I'm wanting to add a date parameter to in order to speed up the loading in desktop.

 

Below is the whole query, I'm wanting to change the first & only date that appears 1/1/2000 with a parameter. I've called my parameter DateParameter and set it up as the below, but I'm not able to get the parameter into the query, if you're able to assist at all would be greatly appreciated

 

LaurenceSD_0-1676362942505.png

 

 

let
Source = Sql.Database("XX.X.XX.XX", "SD_Travel", [Query="declare @TravelFrom DATE = '1/1/2000' #(lf)Create table #tempData ( #(lf)BookId int, #(lf) ItemType varchar(100), #(lf) PrimaryId int, #(lf) SupplierId int, #(lf) Supplier varchar(50), #(lf) CurrencyId int, #(lf) Currency varchar(50), #(lf) CurrencyCode varchar(20), #(lf) CurrencySymbol varchar(10), #(lf) BuyingCost money, #(lf) NetCost money, #(lf) InvoiceAmount money , #(lf) InvoiceAmountGBP money, #(lf) PaidAmount money, #(lf) PaidAmountGBP money, #(lf) FlagApproved bit, #(lf) FlagPaid bit, #(lf) BookExchangeRate decimal(18,4), #(lf) PaymentExchangeRate decimal(18,4), #(lf) AdjustmentAmount money, #(lf) AdjustmentAmountGBP money, #(lf) CostTypeId int, #(lf) TravelDate DATE, #(lf) BusinessUnitId INT #(lf)) #(lf)--Itinerary cost data #(lf)Insert Into #tempData(BookId,ItemType, PrimaryId, SupplierId, Supplier, CurrencyId, Currency, #(lf) CurrencyCode,CurrencySymbol, BuyingCost, NetCost, Invoiceamount, #(lf) InvoiceAmountGBP, PaidAmount, PaidAmountGBP, FlagApproved, FlagPaid,BookExchangeRate,PaymentExchangeRate, #(lf) AdjustmentAmount, AdjustmentAmountGBP,CostTypeId,TravelDate,BusinessUnitId) #(lf)SELECT bs.bookid, 'Itin', CostDetail.CostId, SupplierContactId, Company, CostDetail.CurrencyId, CostDetail.Currency, #(lf) CostDetail.CurrencyCode, CurrencySymbol, NetCost, NetCostGBP, InvoiceAmount, #(lf) InvoiceAmountGBP, PaidAmount, PaidAmountGBP,FlagApproved, FlagPaid, #(lf) BookExchangeRate,CostDetail.PaymentExchangeRate, #(lf) AdjustmentAmount, AdjustmentAmountGBP,1, BS.StartDate, BS.BusinessUnitId #(lf)FROM SD_Travel.dbo.vwSupplierPayments CostDetail WITH(NOLOCK) #(lf) INNER JOIN SD_Travel.dbo.BookSum BS WITH(NOLOCK) ON BS.BookId = CostDetail.BookId #(lf)Where CostDetail.CostId IS NOT NULL #(lf) AND (NetCost<>0 OR CostDetail.InvoiceAmount <> 0) #(lf) AND BS.StartDate >= @TravelFrom #(lf)--Other cost data #(lf)Insert Into #tempData(bookid, ItemType, PrimaryId, SupplierId, Supplier, CurrencyId, Currency, #(lf) CurrencyCode, CurrencySymbol, BuyingCost, NetCost, Invoiceamount, #(lf) InvoiceAmountGBP, PaidAmount, PaidAmountGBP, FlagApproved, FlagPaid,BookExchangeRate,PaymentExchangeRate, #(lf) AdjustmentAmount,AdjustmentAmountGBP,CostTypeId,TravelDate,BusinessUnitId) #(lf)SELECT bs.bookid, SupplimentTitle, OtherCostId as PartyItinId, SupplierContactId, SupplierName, #(lf) CostDetail.CurrencyId, CostDetail.Currency, CostDetail.CurrencyCode,CurrencySymbol, #(lf) NetCost as BuyingCost, NetCostGBP, InvoiceAmount, InvoiceAmountGBP, #(lf) PaidAmount, PaidAmountGBP, FlagApproved, FlagPaid,BookExchangeRate, #(lf) CostDetail.PaymentExchangeRate, #(lf) AdjustmentAmount, AdjustmentAmountGBP,2, BS.StartDate, BS.BusinessUnitId #(lf)FROM SD_Travel.dbo.vwSupplierPaymentOthers CostDetail WITH(NOLOCK) #(lf) INNER JOIN SD_Travel.dbo.BookSum BS WITH(NOLOCK) ON BS.BookId = CostDetail.BookId #(lf)Where SuppTypeId In (1,5) AND SupplierContactId IS NOT NULL #(lf) AND (NetCost<>0 OR CostDetail.InvoiceAmount <>0) #(lf) and SuppGroupId<>3 #(lf) AND BS.StartDate >= @TravelFrom #(lf)--Experience Cost #(lf)Insert Into #tempData(Bookid,ItemType, PrimaryId, SupplierId, Supplier, CurrencyId, Currency, #(lf) CurrencyCode, CurrencySymbol, BuyingCost, NetCost, Invoiceamount, #(lf) InvoiceAmountGBP, PaidAmount, PaidAmountGBP, FlagApproved, FlagPaid,BookExchangeRate,PaymentExchangeRate, #(lf) AdjustmentAmount,AdjustmentAmountGBP,CostTypeId,TravelDate,BusinessUnitId) #(lf)SELECT bs.bookid, 'Experience', OtherCostId as PartyItinId, SupplierContactId, SupplierName, #(lf) CostDetail.CurrencyId, CostDetail.Currency, CostDetail.CurrencyCode,CurrencySymbol, #(lf) NetCost as BuyingCost, NetCostGBP, InvoiceAmount, InvoiceAmountGBP, #(lf) PaidAmount, PaidAmountGBP, FlagApproved, CostDetail.FlagPaid,BookExchangeRate, #(lf) CostDetail.PaymentExchangeRate, #(lf) AdjustmentAmount, AdjustmentAmountGBP,2, BS.StartDate, BS.BusinessUnitId #(lf)FROM SD_Travel.dbo.vwSupplierPaymentOthers CostDetail WITH(NOLOCK) #(lf) INNER JOIN SD_Travel.dbo.BookSum BS WITH(NOLOCK) ON BS.BookId = CostDetail.BookId #(lf) Inner Join SD_Travel.dbo.BookItineraryDetail ItinDetail On ItinDetail.ItinDetailId = CostDetail.DetailId and ItinDetail.ItemType='Experience' #(lf)Where SuppTypeId In (7) AND SupplierContactId IS NOT NULL #(lf) AND (NetCost<>0 OR CostDetail.InvoiceAmount <>0) #(lf) and SuppGroupId<>3 #(lf) AND BS.StartDate >= @TravelFrom #(lf)--Cancellation charges #(lf)Insert Into #tempData(Bookid, ItemType, PrimaryId, SupplierId, Supplier, CurrencyId, Currency, #(lf) CurrencyCode, CurrencySymbol, BuyingCost, NetCost, Invoiceamount, #(lf) InvoiceAmountGBP, PaidAmount, PaidAmountGBP, FlagApproved, FlagPaid,BookExchangeRate,PaymentExchangeRate, #(lf) AdjustmentAmount,AdjustmentAmountGBP,CostTypeId,TravelDate,BusinessUnitId) #(lf)SELECT bs.bookid, 'Supplement', OtherCostId as PartyItinId, SupplierContactId, SupplierName, #(lf) CostDetail.CurrencyId, CostDetail.Currency, CostDetail.CurrencyCode,CurrencySymbol, #(lf) NetCost as BuyingCost, NetCostGBP, InvoiceAmount, InvoiceAmountGBP, #(lf) PaidAmount, PaidAmountGBP, FlagApproved, CostDetail.FlagPaid,BookExchangeRate, #(lf) CostDetail.PaymentExchangeRate, #(lf) AdjustmentAmount, AdjustmentAmountGBP,2, BS.StartDate, BS.BusinessUnitId #(lf)FROM SD_Travel.dbo.vwSupplierPaymentOthers CostDetail WITH(NOLOCK) #(lf) INNER JOIN SD_Travel.dbo.BookSum BS WITH(NOLOCK) ON BS.BookId = CostDetail.BookId #(lf)Where SupplimentId = 630 AND SupplierContactId IS NOT NULL #(lf) AND BS.StartDate >= @TravelFrom #(lf)Select Distinct Bookid, ItemType,PrimaryId,SupplierId, Supplier, #(lf) BuyingCost Cost, NetCost, IsNull(InvoiceAmount,0) as Invoice, #(lf) IsNull(InvoiceAmountGBP,0) as InvoiceGBP, IsNull(PaidAmount,0) as Paid, IsNull(PaidAmountGBP,0) as PaidGBP, #(lf) BookExchangeRate, PaymentExchangeRate, #(lf) TravelDate, bu.BusinessUnitName,Currency #(lf)From #tempData PaymentSummary #(lf) INNER JOIN LTGMaster.dbo.BusinessUnitMaster bu ON bu.BusinessUnitId = PaymentSummary.BusinessUnitId #(lf)order by TravelDate #(lf)DROP TABLE #tempData #(lf)"]),

1 REPLY 1
watkinnc
Super User
Super User

Replace

DATE = '1/1/2000' #(lf)Create table

with

DATE = '"&DateParameter&"' #(lf)Create table

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors
Top Kudoed Authors