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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
kaye123
Frequent Visitor

SQL Statement to limit Data import

I have this sql server with 5 database that contains millions of rows and it slows down the data IMPORT. I would like to import the data direct from the source with already limited data from the past 5 yrs (i.e. 2023, 2022, 2021, 2020, 2019) instead of filtering it from the query itself to lessen data processing. Column "Fiscal Year" is available in the database.

I am not familiar with SQL statement. Appreciate any help on how to code this. Thanks!

kaye123_0-1672847501089.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @kaye123 ,

 

I don't know what you are using the query language, so I will write an example in MSSQL for you to get the last 5 years. I recommend that you get the data by filtering it on the sql side.

select * from table_name where FY>=YEAR(GETDATE())-5

 

but if you want to get all the data then you have to add the following step to the data having the Power Query side

 

EzgiAslankara_0-1672848850383.png

 

= Table.SelectRows(#"Changed Type", each [FY] >= Date.Year(DateTime.LocalNow())-5)

 

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


Best regards 🙂

 

 

View solution in original post

4 REPLIES 4
kaye123
Frequent Visitor

Thanks everyone for the input. I have now used below SQL as my final code. Note FiscalYear is not a date but just a 4 digit year number. so I added a VARCHAR.

SELECT Column1, Column2, Column3
FROM DATAAll
WHERE FiscalYear >= CAST(YEAR(GETDATE()) - 5 AS VARCHAR(4))

Anonymous
Not applicable

Hi @kaye123 ,

If you retrieve the data from one table in SQL Server database and limit the date range, you can follow the suggestion from @amitchandak  and @Anonymous to write the below SQL statement and put it in SQL statement text area. And you can refer the following video to get it.

Pass parameter to SQL Queries statement using Power BI

yingyinr_1-1672909736462.png

However, if you need to get the limited data from multiple tables, you can consider to create a view in your sql server data source and refer that view in Power BI Desktop. Maybe there is other method can achieve the same requirement. Anyway, the method depends on your actual scenario. Please provide more details on it. Thank you. 

SQL Server CREATE VIEW

Best Regards

Anonymous
Not applicable

Hi @kaye123 ,

 

I don't know what you are using the query language, so I will write an example in MSSQL for you to get the last 5 years. I recommend that you get the data by filtering it on the sql side.

select * from table_name where FY>=YEAR(GETDATE())-5

 

but if you want to get all the data then you have to add the following step to the data having the Power Query side

 

EzgiAslankara_0-1672848850383.png

 

= Table.SelectRows(#"Changed Type", each [FY] >= Date.Year(DateTime.LocalNow())-5)

 

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


Best regards 🙂

 

 

amitchandak
Super User
Super User

@kaye123 , You write a sql like

 

select * from Table_name where FY = 2022

 

or you can use M Parameter.

 

Refer to three ways from Guyincube too

https://www.youtube.com/watch?v=qZOEDBedATA

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.