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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

adding the file name to a column in power query

Hello Community  -  I have a file that is in a corporate one drive folder.   Below. 

 

The file is called December 2020.   

 

My understanding is that you can get Power Query to automatically add the file name to a column in the table.  In this case, I would want to add a column that contains "December 2020".   

 

I actually have a year's worth of files in this folder, and I know I can also use the invoke function command to bring them in all at once.   But just not specifically sure how to extract the file name, and add it to the column.    Each of the files is named similarly:  January 2021   February 2021  etc  so they should all be able to be changed to a date data type. 

 

https://xxxxxxx-my.sharepoint.com/personal/xxxxxxxxxx/Documents/Historical%20CRM%20Locked%20Forecast...

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @Anonymous 

 

You could use Sharepoint folder connector to connect to your OneDrive for business site and use combine files feature after getting a file list in the folder you want to connect to. But you need to edit or add some steps during the process. I will show you how to do that. 

 

Steps:

 

1. Go to Get Data > File > SharePoint folder > Connect. Enter https://xxxxx-my.sharepoint.com/personal/xxxxxxxxcom into Site URL textbox.

 

Note: Only enter the part before _layouts in the whole URL (https://xxxxx-my.sharepoint.com/personal/xxxxxxxxxxxxx_com/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fxxxxxxxxxxxxxxx%2FDocuments%2FTestFolder%2FData).

 

2. Once it connects and shows a full file list, click on Transform Data button. Then you will go to Source step in Power Query editor. 

 

3. Filter rows on Folder Path column and only select the folder path you want to import data from. This will keep only files from that folder. 

 

4. Remove other columns except for Content column and Name column. Combine files in Content column.

21101301.jpg 

 

5. After a while, you will get a combined table called Query1. However this table doesn't have file name in it. In this query, you may see below steps. Remove several bottom steps untill you see Name column.

21101302.jpg

 

6. Remove Content column. Expand Transform File column. Then you will have data from all files into a table. The Name column also exists. 

21101303.jpg

 

Result

21101304.jpg

 

Hope this helps.

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

9 REPLIES 9
comish4lif2
Advocate II
Advocate II

This is great if your files are in SharePoint, or if you are doing a folder import. 
But what if you are importing a single txt file and want to grab the file name for the purpose of extracting the date?

Hi @comish4lif2 

 

For a single file, you can also use the SharePoint folder or Folder connector, filter by [Name] column to leave only that file row in the query then continue from Step 4 of the same solution. 

 

Best regards,

Jing

So the only way is thru a folder import?

There's no easy way to add a new column in power query and populate the column with the name of the file?

rahuldas_vgm
Helper I
Helper I

I had a similar problem n it worked for me. Thank You.

v-jingzhang
Community Support
Community Support

Hi @Anonymous 

 

Have you solved this problem? Kindly accept a proper reply as solution, or share your own solution if you have got some better ones. This would sure help more people who may have similar requirements. Thanks. 

 

Best Regards,
Community Support Team _ Jing

v-jingzhang
Community Support
Community Support

Hi @Anonymous 

 

You could use Sharepoint folder connector to connect to your OneDrive for business site and use combine files feature after getting a file list in the folder you want to connect to. But you need to edit or add some steps during the process. I will show you how to do that. 

 

Steps:

 

1. Go to Get Data > File > SharePoint folder > Connect. Enter https://xxxxx-my.sharepoint.com/personal/xxxxxxxxcom into Site URL textbox.

 

Note: Only enter the part before _layouts in the whole URL (https://xxxxx-my.sharepoint.com/personal/xxxxxxxxxxxxx_com/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fxxxxxxxxxxxxxxx%2FDocuments%2FTestFolder%2FData).

 

2. Once it connects and shows a full file list, click on Transform Data button. Then you will go to Source step in Power Query editor. 

 

3. Filter rows on Folder Path column and only select the folder path you want to import data from. This will keep only files from that folder. 

 

4. Remove other columns except for Content column and Name column. Combine files in Content column.

21101301.jpg 

 

5. After a while, you will get a combined table called Query1. However this table doesn't have file name in it. In this query, you may see below steps. Remove several bottom steps untill you see Name column.

21101302.jpg

 

6. Remove Content column. Expand Transform File column. Then you will have data from all files into a table. The Name column also exists. 

21101303.jpg

 

Result

21101304.jpg

 

Hope this helps.

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

parry2k
Super User
Super User

@Anonymous connect using one drive for business using SharePoint files or contents, it will list all the files and there you will get file name column and then you can call a function or combine files together, and keep the file name as the part of the data.

 

Read more here 

 

Follow us on LinkedIn

 

Learn about conditional formatting at Microsoft Reactor

My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Vera_33
Resident Rockstar
Resident Rockstar

Hi @Anonymous 

 

You need to provide some sample data, not sure your files are exactly named like January 2021   February 2021, or contains? If they are like Jan 2021, Feb 2021, then you can add a custom column

Date.From( "1 " & Text.BeforeDelimiter([Name],"."))

Vera_33_1-1633825251474.png

 

 

Anonymous
Not applicable

@Vera_33    My files are named per what I mentioned in my post.   January 2021   February 2021 etc  (and as per the example path given.   

 

My question is how to create the column, from the file name, upon import of the file.  

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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