Skip to content

Loading the latest version of a file

Sometimes only the latest version of a file matters. One example is a transaction file that grows each time it is sent, and another is a lookup table, such as a customer list, that changes slowly over time.

This page shows how to load just the latest version, while the older ones are kept.

The trick is to connect to the folder that holds the file, rather than to the file itself:

  1. Give each version a name of its own, such as myFile-YYYYMMDD.xlsx.
  2. Save every version in the same folder.
  3. In Power Query, sort the folder’s files by creation date, newest first.
  4. Take the first file in the sorted list.

The query then loads only the newest version, and the folder keeps a history of all the earlier ones.

A folder holding three versions of the same file, named datafile 18-10-2020, 19-10-2020 and 20-10-2020

In this example, the file grows each day. The version from 18 October holds one day of data:

The 18 October file, with rows of Date, Product and Quantity all dated 18/10/2020

The version from 19 October holds two days of data:

The 19 October file, with the 18 October rows followed by rows dated 19/10/2020

  • On Excel’s Data tab, choose Get Data ➔ From File ➔ From Folder, and browse to the folder.

Excel’s Get Data menu open at From File, with From Folder highlighted

Power Query lists the files in the folder, with their names, extensions and dates:

The folder’s file list, with Content, Name, Extension, Date accessed, Date modified, Date created and Attributes columns for the three files

  • Click Transform Data.
  • Sort the Date created column in descending order, so that the newest file comes first.

The Date accessed, Date modified and Date created columns, with Date created sorted newest first

  • Click the fx button beside the formula bar, and enter this formula:
= Excel.Workbook(#"Sorted Rows"{0}[Content])

#"Sorted Rows"{0} picks the first row of the sorted list, [Content] takes that file’s contents, and Excel.Workbook opens them as a workbook, which lists the sheets inside it.

  • Rename the step SelectFirstRow.

The SelectFirstRow step, listing one sheet, Sheet1, with Applied Steps of Source, Sorted Rows and SelectFirstRow

  • Click Table in the Data column to open the sheet.
  • On the Transform tab, click Use First Row as Headers.

The query now returns the contents of the newest file:

The loaded data, with Date, Product and Quantity columns and rows from 18/10/2020 and later