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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
negbc
Helper I
Helper I

Setting Up Dynamic Source File Name

I'm looking to create a source file path to which the file name can change dynamically by date. I will have a daily extract to a folder, with the same naming convention "Name.YYMMDD" with that date being the extract date. In power query I want to connect, for example, to the file the is -7 days from today, or -14 days from today. Is there a way to format the file path so it changes everyday when the report freshes to file name that has the date corresponding to the date -7 days from the current day?

5 REPLIES 5
dufoq3
Super User
Super User

Hi @negbc,

 

dufoq3_0-1715015658627.png

= "Name." & Date.ToText(Date.AddDays(Date.From(DateTime.FixedLocalNow()), -7), [Format="yyyyMMdd"])

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Would this replace the entire source code? or would I still need some of the elements that as shown below and replace the file path with the code you provided?

 

negbc_0-1715171924527.png

 

Hello,

Were you able to have a solution on your question?
I am looking for the same code, and I can't find how to include the code mentionned in the one I am using.
I need to include : 

= "Name." & Date.ToText(Date.AddDays(Date.From(DateTime.FixedLocalNow()), -7), [Format="yyyyMMdd"])


in 

= Csv.Document(File.Contents("C:\Users\NAME\Desktop\DATABASE\DATA123\123OTB.110924.txt"),[Delimiter=" ", Columns=19, Encoding=1200, QuoteStyle=QuoteStyle.None])

The  code above is the source code I have when I go manually looking for the file, so I guesses I just needed to replace the name of the file by the code suggested? But so far it didn't work I have the below error.

Expression.SyntaxError : Jeton Comma

Any idea what is wrong?

Thanks a lot !

There wasn't an exact solution as the dynamic source code doesn't work with automatic refreshes, so wouldn't work for my solution. However, you could still try with a single source.

 

Create a variable first for the date, last week for example:

LW = Date.ToText(Date.AddDays(Date.From(DateTime.LocalNow()),-7),"MMddyy")

 

Then in your source code, enter the variable, concatenated to the file name.

= Csv.Document(File.Contents("C:\Users\NAME\Desktop\DATABASE\DATA123\"123OTB."& LW &".txt"),[Delimiter=" ", Columns=19, Encoding=1200, QuoteStyle=QuoteStyle.None])

 

I ended up connecting an entire folder and then selected the files I needed used this dynamic code, that way the refresh is the entire folder instead of a single file.

Hi @abc44 & @negbc. It is possible to do it the way above, but I would use Folder.Files and filter newest file.

 

1.) Use Folder as source

dufoq3_0-1726123765246.png

 

2.) Click Transform Data

 

3.) Sort by Date Crated (descending order) if necessary

 

4.) Click at 1st Binary in [Content] column

dufoq3_4-1726124259823.png

 

5.) Edit Navigation step and replace selected with 0 so the code for this step will be:

 

= Source{0}[Content]

 

dufoq3_3-1726124222924.png

This will choose 1st file dynamicaly.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors