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
SammiF1244
Helper I
Helper I

Power Query Folder Connection: Get newest file only and rename Query based on file name

I have a folder connection where I would like to get the newest file only. The first applied step should be to rename the query to the file name, which has a specific prefix and date that identifies the version of the data. The end goal is that each time there is a file update, the file will be dropped into the connected folder via SFTP. Since I cannot overwrite the last file, I want to build the query from the newest file using the file name as updated query name without any human intervention. Here is my current code: 

let
Source = Folder.Files("C:\Users\SFowler\OneDrive - Evolent Health, Inc\Continuous Improvement - Code Governance\Artifacts\Source Files\AutomationDiagnosisExpressLookupQuery"),
#"C:\Users\SFowler\OneDrive - Evolent Health, Inc\Continuous Improvement - Code Governance\Artifacts\Source Files\AutomationDiagnosisExpressLookupQuery\_PMWA_Diagnosis_10 31 2023 csv" = Source{[#"Folder Path"="C:\Users\SFowler\OneDrive - Evolent Health, Inc\Continuous Improvement - Code Governance\Artifacts\Source Files\AutomationDiagnosisExpressLookupQuery\",Name="PMWA_Diagnosis_10.31.2023.csv"]}[Content],
#"Imported CSV" = Csv.Document(#"C:\Users\SFowler\OneDrive - Evolent Health, Inc\Continuous Improvement - Code Governance\Artifacts\Source Files\AutomationDiagnosisExpressLookupQuery\_PMWA_Diagnosis_10 31 2023 csv",[Delimiter=",", Columns=19, Encoding=65001, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(#"Imported CSV", [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Diagnosis_Code", type text}, {"Description", type text}, {"Effective_Date", type date}, {"DateTimeModified", type datetime}, {"Termination_Date", type date}, {"Type", Int64.Type}, {"Relation", type text}, {"Action", type text}, {"Entity", type text}, {"Key_1", type text}, {"Key_2", Int64.Type}, {"Key_3", Int64.Type}, {"Key_4", type date}, {"Key_5", Int64.Type}, {"Key_6", Int64.Type}, {"Key_7", Int64.Type}, {"Key_8", Int64.Type}, {"Key_9", Int64.Type}, {"Key_10", Int64.Type}})
in
#"Changed Type"

 

 

To clarify, this code is working, I just need  to modify it to include the two steps: 

  1. get newest file
  2. rename query based on file name
1 ACCEPTED SOLUTION
Anonymous
Not applicable

You can't dynamically rename a query with M code.

 

--Nate

View solution in original post

1 REPLY 1
Anonymous
Not applicable

You can't dynamically rename a query with M code.

 

--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.