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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Extraction of Excel File name automatically in Power BI dash board

Dear all,

I want my  BI dashboard to extract excel file name automatically for that may be I need to create a column and use a code to get the name. so please any one can guide me or tell me which code i need to use. So, every time when I change my source data file the dash board must extract the file name and disply it.

 

Thank you

10 REPLIES 10
mussaenda
Super User
Super User

Hi @Anonymous ,

 

Filename is automatically extracted if you are using the folder as your source.

It is usually called Source.Name or Name. 

 

Is it not showing on your side? Don't you want to use the folder as soure instead of the excel so you can achieve your requirement?

mussaenda_0-1693208120674.png

 

Anonymous
Not applicable

@foodd I am facing one little problem if you can help then please help.

 

After doing 2nd last step as you mentioned above (Rename the first column containing the Excel Workbook name to [File Name])  when I choose a different file from name column the format will disturbe again like for new file headers will appear again in first row while the old headers are still there.

If you have any solution please let me know. Thank you

Anonymous
Not applicable

Hi, Thanks for the help. I just copy this code and change the source folder link and created same excel file using same name (LOG_2023081616900_NYK01_CBS350) with same table inside. but I got this errorScreenshot 2023-08-18 124805.png

Anonymous
Not applicable

I dont know you get it right or not.  but  my simple requirment is I want my excel file name in to an column by M code or any other mean. It is happens automatically when we choose folder as source data (it creates automatically a clumn with "Name" and show all excel file names inside that folder) but when we choose excel file as source data it never happens. so I want a cloumn in my excel file who have the name of that excel file.

Note:  Assumes that you are importing log data saved in Excel format from a local folder. 

This is one way, and there are other variants.

 

Source Path is a local folder = C:\Users\user\Desktop\SwitchLogs

Excel File represents a log file with four columns :  

EventID, Severity, Date, Duration

 

foodd_1-1692317955116.png

 

 

  • The first two lines of the m-code will be the folder source using
    = Folder.Contents("C:\Users\user\Desktop\SwitchLogs")
  • Add a custom column that retrieves the [Content] column from each Excel file.
    = Table.AddColumn(Source, "Custom", each Excel.Workbook([Content]))  
  • Remove Other Columns leaving only [Name] and [Custom] columns
    = Table.SelectColumns(#"Added Custom",{"Name", "Custom"})
  • Expand [Custom] column, and choose 'Data' from the list

    = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Data"}, {"Data"})

  • Expand [Data] column
    = Table.ExpandTableColumn(#"Expanded Custom", "Data", {"Column1", "Column2", "Column3", "Column4"}, {"Column1", "Column2", "Column3", "Column4"})
  • Promote Headers using the first row as the header row
  • Rename the first column containing the Excel Workbook name to [File Name]
  • Close and Load

Results:

foodd_0-1692317573389.png

 

Query:

 

 

 

 

let
    Source = Folder.Contents("C:\Users\user\Desktop\SwitchLogs"),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Excel.Workbook([Content])),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Name", "Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Data"}, {"Data"}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Expanded Custom", "Data", {"Column1", "Column2", "Column3", "Column4"}, {"Column1", "Column2", "Column3", "Column4"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Data", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"LOG_2023081616900_NYK01_CBS350.xlsx", type text}, {"EventID", Int64.Type}, {"Severity", Int64.Type}, {"Date", type date}, {"Duration", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"LOG_2023081616900_NYK01_CBS350.xlsx", "File Name"}})
in
    #"Renamed Columns"

 

 

 

 

Anonymous
Not applicable

No, I want this column of file name in excel sheet where I have other data. like here I want a column of file name please see attached fileScreenshot 2023-08-17 231339.jpg

If I understand, you would like to understand how to add the file
name to the Query named 'SMB', and have it appear in position 1
to the left of [EventID] like it appears in Query 'experiment'?

[Excel workbook Filename], [EventID], [Severity] ...

foodd
Super User
Super User

What is the source of (where are they stored) the Excel Files?

 

Would this be SharePoint Online, OneDrive, Network Share, local folder, etc?

 

The powerquery-m is straightforward, and the process is maintainable.   Do you have a working Powerbi report 

currently, and would you share it?  Also, if you would share what the desired results 

for the report would look like? 

Anonymous
Not applicable

1. I am taking the file from local folder.

2. In the screen shot I have marked that when we choose source data folder it taked file names automatically by adding new column and what I want is same column with file name in my excel data sheet.Screenshot 2023-08-17 164658.png

So, you have the File Name already.   Good.

What you need is the worksheet name or names from the workbook.  

Are the Excel Workbooks the same structure (count of sheets) from day to day?

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

Top Solution Authors