Power Query M SQL Database Query from Server on a table from multiple Databases with SQL Statement with date limit

DJHarris71

New Member
Joined
May 19, 2009
Messages
44
Hello. I need to pull data from a same formatted table on the same server from different databases with a SQL query, but the tables in the server have 3 years of data, and I only want 3 months.
I know that for one table I can do this:
VBA Code:
Source = Sql.Database("Server", "Database", [Query="Query with FROM Table WHERE [DATE]  >= DATEADD(DAY,-90, GETDATE())"])
But this limits me to one database.
/
I also know I can do this:
Code:
Source = Sql.Databases("server"),
Table = Source{[Name="Database"]}[Data],
Data = Table{[Schema="dbo",Item="Table"]}[Data],
Data90days = Table.SelectRows(Data, each Date.IsInPreviousNDays([Date], 90))
The problem here is that the command 'Data = Table{[Schema="dbo",Item="RawWorkStation"]}[Data],' still pulls all 3 years, which defeats the purpose.
/
I also know that I can put the these in a function, Such as:
Code:
GetTable = (Database as text) as table =>
let
  Source = Sql.Database("Server", Database, [Query="Query with FROM Table WHERE [DATE]  >= DATEADD(DAY,-90, GETDATE())"])
in
  Source
However, this method forces my query into a 'Dynamic' mode. Which then does not allow me to do a refresh schedule in the Power BI Cloud Service, which is required.
So, question 1 is: How can I do the above without this 'Dynamic' mode happening?? If you can't then see below..

Interestingly, if I do it this way, it does not force it into a 'Dynamic' mode, but it still pulls 3 years of data on the Data step.
Code:
Source = Sql.Databases("server"),
GetTable = (Database text) as table =>
let
  Table = Source{[Name=Database]}[Data],
  Data = Table{[Schema="dbo",Item="Table"]}[Data],
  Data90days = Table.SelectRows(Data, each Date.IsInPreviousNDays([Date], 90))
in
  Source
This is the only working solution I have, but I need to transform some of these fields (such as divide by 60 or 100), and these are things I can do right in the query. This is more efficient than doing this in the Power Query M transformation as each command creates a saved table variable, which is more memory usage, and is chargeable by MS, and just slower in general.
So the question here is: Is there way to add a query statement to this method?

Thanks,
Doug
 

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top