Power BI: dashboard stops to work after the data source name has been changed

When my data source has a name with a date or date/time (for instance, an excel file with name: test data 17.01.2026.xlsx or test data 17.01.2026 15_47_59.xlsx) and every month, in the sharepoint folder, either I have to replace it with the last month or I will upload the last month file, in both situations, my dashboard will stop to work and to resolve it, I will have to update it manually. In this article, I will explain different ways to do it automatically and I will have to edit the code in “home -> advanced editor” in Power BI Query:

power bi

Before to start, in the original code, copy what I highlight in green:

power bi

IMPORTANT: make sure that the file starts with an unique name because if in the folder, there are other files and subfolders which start with the same name, it will not work properly. For instance, in the same folder:

  • I have a subfolder with name “test” (conflict)
  • I have a file with name “test for unix.xlsx” (conflict)
  • I have another file with name “check test ticket.xlsx” (all good)

In this case, in the code, I will reference my file as “test data“ (unique) and not “test” (conflict)

NOTE: after changing the code, do a refresh by clicking on “home -> refresh preview” if not, the updated new data will not display

power bi

Option 1: only 1 single file

I have one unique file meaning that every month, I will replace the old one.


let
    // change www by yours
    FolderSource = SharePoint.Files("https://www",[ApiVersion=15]),
    // change xxx (no need to put the full file name) and yyy by yours
    FileSource = Table.SelectRows(FolderSource,each Text.StartsWith([Name], "xxx") and Text.Contains([Folder Path], "/Shared Documents/yyy/")),
    Source = Excel.Workbook(FileSource{0}[Content], null, true),
    // change zzz by yours
    DataSource = Source{zzz}[Data]
in
    DataSource              
              
power bi
power bi

Option 2: fetch the last one (current month)

I have multiple files so it will update the last one which is the current month. For instance, the current month is August.


let
    // change xxx (put the file name until the date) by yours
    FileName = "xxx" & Date.ToText(DateTime.Date(DateTime.LocalNow()), "dd.MM.yyyy") & ".xlsx",
    // change www by yours
    FolderSource = SharePoint.Files("https://www",[ApiVersion = 15]),
    // change yyy by yours
    FileSource = FolderSource{[Name = FileName, Folder Path = "https://yyy"]}[Content],
    Source = Excel.Workbook(FileSource, null, true),
    // change zzz by yours
    DataSource = Source{zzz}[Data]
in
    DataSource             
              
power bi
power bi

Option 3: fetch the last updated one based on the sharepoint modified date

I have multiple files so it will update the last modified one in the list. For instance the last one is March and the current month (August) is not in the sharepoint but I updated data of February.


let
    // change www by yours
    FolderSource = SharePoint.Files("https://www",[ApiVersion=15]),
    // change xxx (no need to put the full file name) and yyy by yours
    FileSource = Table.SelectRows(FolderSource,each Text.Contains([Folder Path],"/Shared Documents/yyy/") and Text.StartsWith([Name], "xxx")),
    SortedFiles = Table.Sort(FileSource,{{"Date modified", Order.Descending}}),
    Source = Excel.Workbook(SortedFiles{0}[Content], null, true),
    // change zzz by yours
    DataSource = Source{zzz}[Data],
    AddFileName = Table.AddColumn(DataSource, "FileName", each SortedFiles{0}[Name], type text),
    AddFileModifiedDateTime = Table.AddColumn(AddFileName, "FileModifiedDateTime", each SortedFiles{0}[Date modified], type datetime)
in
    AddFileModifiedDateTime             
              
power bi
power bi

Option 4: fetch the last one in the list

I have multiple files so it will update the last one in the list and not the current month. For instance the last one is March and currently, the month is August and the current month file is not in the sharepoint.


let
    // change www by yours
    FolderSource = SharePoint.Files("https://www",[ApiVersion = 15]),
    // change yyy by yours
    FileSource = Table.SelectRows(FolderSource, each [Folder Path] = "https://yyy"),
    // change xxx1 (no need to put the full file name) by yours
    FilterFiles = Table.SelectRows(FileSource, each Text.StartsWith([Name], "xxx1") and Text.EndsWith([Name], ".xlsx")),
    // change xxx2 (put some characters of the file name before the date) by yours
    AddFileDate = Table.AddColumn(FilterFiles,"FileDate", each Date.FromText(Text.BetweenDelimiters([Name], "xxx2", ".xlsx"),[Format = "dd.MM.yyyy"]), type date),
    SortedFiles = Table.Sort(AddFileDate, {{"FileDate", Order.Descending}}){0},
    Source = Excel.Workbook(SortedFiles[Content], null, true),
    // change zzz by yours
    DataSource = Source{zzz}[Data],
    AddFileName = Table.AddColumn(DataSource, "FileName", each SortedFiles[Name], type text)
in
    AddFileName             
               
power bi
power bi

If my file name includes the time (test data 17.01.2026 15_47_54.xlsx), use this code:


let
    // change www by yours
    FolderSource = SharePoint.Files("https://www",[ApiVersion = 15]),
    // change yyy by yours
    FileSource = Table.SelectRows(FolderSource, each [Folder Path] = "https://yyy"),
    // change xxx1 (no need to put the full file name) by yours
    FilterFiles = Table.SelectRows(FileSource, each Text.StartsWith([Name], "xxx1") and Text.EndsWith([Name], ".xlsx")),
    // change xxx2 (put some characters of the file name before the date) and xxx3 by yours
    AddFileDate = Table.AddColumn(FilterFiles,"FileDateTime", each DateTime.FromText(Text.Replace(Text.BetweenDelimiters([Name], "xxx2", ".xlsx"),"xxx3",":"),[Format = "dd.MM.yyyy HH:mm:ss"]), type datetime),
    LatestFile = Table.Sort(AddFileDate, {{"FileDateTime", Order.Descending}}){0},
    Source = Excel.Workbook(LatestFile[Content], null, true),
    // change zzz by yours
    DataSource = Source{zzz}[Data],
    AddFileName = Table.AddColumn(DataSource, "FileName", each LatestFile[Name], type text),
    AddFileDateTime = Table.AddColumn(AddFileName, "FileDateTime", each LatestFile[FileDateTime], type datetime)
in
    AddFileDateTime               
               
power bi

The time format is in 24 hours, if you want it in 12 hours, change it from HH:mm:ss to hh:mm:ss. To include AM/PM:

  • 03:47:54 PM, "dd-MM-yyyy hh:mm:ss tt"
  • 03:47:54PM, "dd-MM-yyyy hh:mm:ss" & "tt"
power bi

Now if the file is in the csv format, the code should be modified so first, as for the xlsx format, I have to copy some lines from the original code:

power bi

NOTE: do not copy the “column” part, if not, new columns will not be included

For my example, I will use the code with date only (the process is the same for date/time), in the picture, follow the instructions:

power bi

Interesting Topics