I have a datasource from a SQL Server database, through the SQL statement.
= Sql.Database("sql-exact", "001", [Query="select [_].[ordernr] as [OrderNummer],#(lf) [_].[debnr] as [DebiteurCode],#(lf) convert(date, [_].[orddat]) as [OrderDatum],#(lf) [_].[bdr_ev_val] as [OrderBedrag]#(lf)from #(lf)(#(lf) select [_].[ordernr],#(lf) [_].[debnr],#(lf) [_].[orddat],#(lf) [_].[bdr_ev_val]#(lf) from [dbo].[orkrg] as [_] WITH (NOLOCK)#(lf) where (([_].[ord_soort] = 'V' and ([_].[ord_soort] is not null)) and ([_].[fiattering] = 'J' and [_].[fiattering] is not null)) and [_].[orddat] < convert(datetime2, '2022-10-10 12:12:56.3000236')#(lf)) as [_]", CommandTimeout=#duration(0, 0, 1, 0), MultiSubnetFailover=true])
I want to filter on the date by using a dynamic parameter, where [orddat] < TODAY.
I cannot use any M query in the code, since SQL doesnât recognize DateTime.LocalNow().
I donât want to first get all the data and filter on date after, since getting the date for today locks or slows the database.
Hi @JoycevW ,
Thank you very much for posting your query in the forum.
I donât know if you have the possibility to create a view in SQL Server from the query you are using.
Example:
Hi @JoycevW, did the response provided by @jafernandezpuga and @AntrikshSharma help in solving your query? If not, how far did you get and what kind of help you need further? If yes, kindly mark as solution the answer that solved your query.
So if I understand correctly you then would advise me to use the query without a date filter to get all the date and then use Power Query M to filter on date?
Hi @jafernandezpuga, when using your code I cannot view native query, so it doesnât query fold. And een though the query works it does not filter the date, I still have orderdates from today. Possibly because the column orddat shows in date and time?
I have this code now:
let
Source = Sql.Database("sql-exact", "001", [Query="SELECT ordernr, debnr, orddat, bdr_ev_val, fiattering, ord_soort
FROM _Complies_vw_orkrg
WHERE (orddat < { fn NOW() })", CreateNavigationProperties=false]),
#"Filtered Rows" = Table.SelectRows(Source, each ([fiattering] = "J") and ([ord_soort] = "V")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"fiattering", "ord_soort"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"ordernr", "OrderNummer"}, {"debnr", "DebiteurNummer"}, {"orddat", "OrderDatum"}, {"bdr_ev_val", "OrderBedrag"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"OrderDatum", type date}}),
#"Filtered Rows1" = Table.SelectRows(#"Changed Type", each ([OrderBedrag] <> 0)),
#"Sorted Rows" = Table.Sort(#"Filtered Rows1",{{"OrderDatum", Order.Descending}})
in
#"Sorted Rows"
Hi @JoycevW,
As I have been able to prove, at the moment in which we use the SQL Statement section, the View Native Query option is deactivated.
We have 2 options:
Create a view on the database server that contains the query we need, and then load this view into PQ.
CREATE VIEW [dbo].[vFactInternetSales_Filtered]
AS
SELECT SalesOrderNumber, SalesOrderLineNumber, OrderDate, ProductKey, CustomerKey, OrderQuantity, UnitPrice, SalesAmount
FROM dbo.FactInternetSales
WHERE (CAST(OrderDate AS date) < CAST({ fn NOW() } AS date))
Load the table into PQ and perform the transformations in PQ:
let
Source = Sql.Database("JAFP\SQLEXPRESS", "AdventureWorksDW2016", [CreateNavigationProperties=false]),
dbo_FactInternetSales = Source{[Schema="dbo",Item="FactInternetSales"]}[Data],
#"Filtered Rows" = Table.SelectRows(dbo_FactInternetSales, each Date.From( [OrderDate] ) < Date.From ( DateTime.LocalNow() ) ),
#"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"SalesOrderNumber", "SalesOrderLineNumber", "OrderQuantity", "UnitPrice", "ExtendedAmount", "OrderDateKey", "CustomerKey"}),
#"Filtered Rows1" = Table.SelectRows(#"Removed Other Columns", each ([SalesOrderNumber] = "SO43697" or [SalesOrderNumber] = "SO43698" or [SalesOrderNumber] = "SO43699"))
in
#"Filtered Rows1"
Yes, so the only way to do this is to create a view on the server, where you can already filter the date. I was hoping I could do this only in PowerQuery. But that doesnât work unfortunelately. Thanks for all the help!
Hello @JoycevW , did the response above help solve your query? Itâs been a while since we got a response from you. In case there wonât be any activity on it in the next few days, weâll be tagging this post as Solved.
Hi @JoycevW,
In both options, the query changes every day.
In the second option, all the orders are loaded in power query as you can see in the Navigation step and in the next step is where we filter them.
If you donât want all the records to be loaded into Power Query, your option is the first, create the view in the database and the filtered data arrives in Power Query.