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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
gancw1
Resolver II
Resolver II

Load most recent file from SharePoint folder

I would like to load the most recent CSV file from a SharePoint folder.

i tried using the method below but it seems the file and path is hardcoded. 

 

let
Source = SharePoint.Files("https://musux.sharepoint.com/sites/poc", [ApiVersion=15]),
#"Filtered Rows" = Table.SelectRows(#"Source", each Text.StartsWith([Folder Path], "https://musux.sharepoint.com/sites/poc/Doc")),
#"Sorted Rows1" = Table.Sort(#"Filtered Rows",{{"Date created", Order.Descending}}),
#"Top Row Table" = Table.FirstN(#"Sorted Rows1",1),
OpenBin = #"Top Row Table"{[Name="Report_1533371.csv",#"Folder Path"="https://musux.sharepoint.com/sites/poc/Doc"]}[Content],
#"Imported CSV" = Csv.Document(OpenBin,[Delimiter=",", Columns=45, Encoding=1252, QuoteStyle=QuoteStyle.None])
in
#"Imported CSV"

 How do change the code so that it takes the name and path from the "Top Row Table" ?

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Remember you can address rows and columns in Power Query. Here is an example to open the newest Excel file.  You can adjust it for csv.  Note there is no need for the "keep only first row" transform.

 

 

 

let
    Source = SharePoint.Files("https://your.sharepoint.com/sites/yoursite", [ApiVersion = 15]),
    #"Filtered Rows" = Table.SelectRows(Source, each ([Extension] = ".xlsx")),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Date modified", Order.Descending}}),
    #"First File" = #"Sorted Rows"{0}[Content],
    #"Imported Excel Workbook" = Excel.Workbook(#"First File")
in
    #"Imported Excel Workbook"

 

 

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

Remember you can address rows and columns in Power Query. Here is an example to open the newest Excel file.  You can adjust it for csv.  Note there is no need for the "keep only first row" transform.

 

 

 

let
    Source = SharePoint.Files("https://your.sharepoint.com/sites/yoursite", [ApiVersion = 15]),
    #"Filtered Rows" = Table.SelectRows(Source, each ([Extension] = ".xlsx")),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Date modified", Order.Descending}}),
    #"First File" = #"Sorted Rows"{0}[Content],
    #"Imported Excel Workbook" = Excel.Workbook(#"First File")
in
    #"Imported Excel Workbook"

 

 

Thanks. I am new to M and did not know about the syntax for addressing row and column!

 

How do i store the file name in another table so that I can display the name of the file ?

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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.

Top Solution Authors
Top Kudoed Authors