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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
shamilka
Frequent Visitor

Getting only the latest database data instead of downloading the whole table in the beggining

Hi All, 

 

I'm using the following code to load the data to Power BI. Since my filteration at the the next step of the code, it will download the whole table. I want to download only the latest data for the past 15 days. Could you please let me know whether we can adjust the following code on the highlighted first row to extract the data only related the past 15 days? 

 

My code is given below. Please ammend the code accordingly. 

 

Thank you.

 

Code:

 

 AB_history = Source{[Schema="AB",Item="history"]}[Data],

    #"Removed Other Columns" = Table.SelectColumns(AB_history,{"partno", "serialno","del_date"}),

    #"Filtered Rows" = Table.SelectRows(#"Removed Other Columns", each ([vm] = "YA" or [vm] = "YE")),

    #"Ad_Date" = Table.AddColumn(#"Filtered Rows", "Date", each Date.AddDays(#date(1971,12,31), [created_date]), type date),

    #"FilteredLast15DaysFromToday" = Table.SelectRows(#"Ad_Date", each [Date] >= Date.AddDays(Date.From(DateTime.LocalNow()), -15))

in 

  #"FilteredLast15DaysFromToday"

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @shamilka ,

I will use SQL Server as datasource for an example:
Sample data (Today is 2024.8.20):

vjunyantmsft_0-1724144847313.png

When connecting to SQL Server:

vjunyantmsft_1-1724145063179.png

Here is the SQL Code in the SQL statement:

select * from Table_4 where date <= CAST(GETDATE() as date) AND date >= DATEADD(DAY, -15, CAST(GETDATE() as date));

And the final output is as below:

vjunyantmsft_2-1724145128842.png


Here is the M code in Advanced Editor:

let
    Source = Sql.Database("DM1", "Import", [Query="select * from Table_4 where date <= CAST(GETDATE() as date) AND date >= DATEADD(DAY, -15, CAST(GETDATE() as date));"])
in
    Source

 

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @shamilka ,

If you want to import only the last 15 days of data when Power Query connects to the data source, rather than importing all the data and then filtering for the last 15 days, you can either pre-filter the data source for the last 15 days before connecting to the data source, or you can enable an advanced setting when connecting to the data source to use some database code (e.g., SQL) to limit the data to only the last 15 days data can be imported.

That's why we need to know what your data source is. Because not all data sources support advanced settings.

vjunyantmsft_0-1724138999324.png

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Could you please write me a sample query to filter the data for the last 15 days please?

Anonymous
Not applicable

Hi @shamilka ,

I will use SQL Server as datasource for an example:
Sample data (Today is 2024.8.20):

vjunyantmsft_0-1724144847313.png

When connecting to SQL Server:

vjunyantmsft_1-1724145063179.png

Here is the SQL Code in the SQL statement:

select * from Table_4 where date <= CAST(GETDATE() as date) AND date >= DATEADD(DAY, -15, CAST(GETDATE() as date));

And the final output is as below:

vjunyantmsft_2-1724145128842.png


Here is the M code in Advanced Editor:

let
    Source = Sql.Database("DM1", "Import", [Query="select * from Table_4 where date <= CAST(GETDATE() as date) AND date >= DATEADD(DAY, -15, CAST(GETDATE() as date));"])
in
    Source

 

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

ahadkarimi
Solution Specialist
Solution Specialist

Hey @shamilka, could you let me know if the data is being loaded from SQL or ...?

Hi ahadkarimi,

 

I'm loading from the database

Anonymous
Not applicable

If you make a separate query (let's name it LastDate) that returns just the Date-15 days, using the Dates.AddDays method that you already know, you can then rearrange your original query by making your Date filter the first filter, and make your SelectRows statement each [Date] = LastDate. This should change your SQL to include "Where .Date = (the value of LastDate). If you check that step, you should see it folding.

 

--Nate

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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