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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
JeffP25
Regular Visitor

Dynamic File Import

I have a model that is built off of four Excel files that are stored in a network folder.  The network folder contains several files that aren't associated with my model, but I need the most recent version of the four files.  My dilemma is that the files have datestamps, and files from previous days need to be kept for auditing purposes.  My steps are as follows:

 

1. = Folder.Files("FolderPath")

2. = Table.SelectRows (Source, each
Text.Contains ([Name], "Text in Filename"))

3. = Table.Sort(Filtered,{{"Date modified", Order.Descending}})

4. = Table.FirstN(#"Sorted Rows",4)

5. = Table.SelectRows(#"Kept First Rows", each Text.Contains([Name], "Text in Filename"))

6. = #"Filtered Rows"{[#"Folder Path"="Filename with Specific Date"]}[Content]

 

How should I modify my steps so they find the most recent file that contains "X" in the name?

1 REPLY 1
Anonymous
Not applicable

Hi, @JeffP25 

Maybe you can try the following expression:

let
    // Step 1: Get all the files in the folder
    Source = Folder.Files("FolderPath"),
  
    // Step 2: Filter for file names that contain "X".
    FilteredByText = Table.SelectRows(Source, each Text.Contains([Name], "X")),
  
    // Step 3: Extract the identifier in the file name (assuming the format is "Identifier_Date.xlsx")
    ExtractIdentifier = Table.AddColumn(FilteredByText, "Identifier", each Text.BeforeDelimiter([Name], "_")),
  
    // Step 4: Group by identifier and take the latest file for each group
    GroupAndSelectLatest = Table.Group(ExtractIdentifier, {"Identifier"}, {
        "LatestFile", each Table.Sort(_, {{"Date modified", Order.Descending}}){0}
    }),
  
    // Step 5: Expand and merge the file contents
    ExpandContent = Table.ExpandRecordColumn(GroupAndSelectLatest, "LatestFile", {"Content"}, {"Content"}),
    ImportExcelData = Table.TransformRows(ExpandContent, (row) => Excel.Workbook(row[Content]){0}[Data])
in
    ImportExcelData

 

How to Get Your Question Answered Quickly

Best Regards

Yongkang Hua

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

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.