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
twister8889
Helper V
Helper V

Excel filename as column

Hi Guys,

 

How can do I get the filename, split, and use it into a new column? I would like to have a column with 1 first string: Custom with content  =ETL and Custom2 = excel

 

Excel Filename.png

 

1 ACCEPTED SOLUTION
5 REPLIES 5
v-yingjl
Community Support
Community Support

Hi @twister8889 ,

If you connect to the file directly, I'm afraid you cannot get the file name directly in power query.

You can try to connect to folder and try this query:

let
    folder = "C:\Users\xxx\xxx\excel folder",
    Source = Folder.Files(folder),
    RemoveUnused = Table.SelectColumns(Source,{"Content","Name"}),
    Addtables = Table.AddColumn(
        RemoveUnused,
        "Custom",
        each Table.PromoteHeaders(Excel.Workbook([Content]){[Item = "Sheet1",Kind = "Sheet"]}[Data])
    ),
    Addtables2 = Table.AddColumn(
        Addtables,
        "Custom2",
        each Table.PromoteHeaders(Excel.Workbook([Content]){[Item = "Sheet2",Kind = "Sheet"]}[Data])
),
    #"Removed Columns" = Table.RemoveColumns(Addtables2,{"Content"})
in
    #"Removed Columns"

You will get a File name column and two table columns under this query.

file name.png

Now you can split the Name column twice one by space and another by . , remove unnecessary columns and rename the new column:

spilit column.png

 

Best Regards,
Yingjie Li

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

First of all, thank you for your answers and I'm sorry for the delay.

 

When the scenario is to get one file and sheets with the same structure, these options to solve the problem. I'm new in power bi and in my project, so I noticed that I need to get data of multiples files (excel) with different sheets structures. However, I will be open another thread to this scenario, for this I will mark the first answer as correct, but all solved my problem.

Thank you so much, guys.

 

Greg_Deckler
Super User
Super User

@twister8889 - Had another thought on this. Make the file name/path a query parameter. Then you can set it and use it to create a new column in your query.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Super User
Super User

@twister8889 - Maybe check out a Folder query


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.