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
AqiffM
New Member

Load data files from folder and Power Query to load each excel sheet from each excel file

Hi All,

 

I have data source which is from folder, I choose the directory and I combine and transform data (at this stage, i can only choose 1 sheet)

 

Different excel sometimes have 2 excel sheets and 3 excel sheets. but the sheet names are consistent

1 ACCEPTED SOLUTION
Omid_Motamedise
Super User
Super User

you can load all the file by the below code (with comments for each step)

 

let
    // Set the folder path
    FolderPath = "C:\YourFolderPath\",

    // Load all files from the folder
    Source = Folder.Files(FolderPath),

    // Filter to only include Excel files
    ExcelFiles = Table.SelectRows(Source, each Text.EndsWith([Name], ".xlsx") or Text.EndsWith([Name], ".xls")),

    // Add a column to extract data from each file
    AddCustom = Table.AddColumn(ExcelFiles, "Data", each Excel.Workbook([Content], true)),

    // Expand the Data column to get each sheet from each file
    ExpandSheets = Table.ExpandTableColumn(AddCustom, "Data", {"Name", "Data"}, {"SheetName", "Data"}),

    // If you want to filter by specific sheet names, uncomment and modify this line:
    // FilterSheets = Table.SelectRows(ExpandSheets, each [SheetName] = "Sheet1" or [SheetName] = "Sheet2"),

    // Expand the data in each sheet
    ExpandData = Table.ExpandTableColumn(ExpandSheets, "Data", Table.ColumnNames(ExpandSheets[Data]{0})),

    // Optional: Add a column to identify the source file name
    AddSourceInfo = Table.AddColumn(ExpandData, "SourceFile", each [Name])
in
    AddSourceInfo

 

 

If this answer helped resolve your issue, please consider marking it as the accepted answer. And if you found my response helpful, I'd appreciate it if you could give me kudos. Thank you!

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

View solution in original post

4 REPLIES 4
Omid_Motamedise
Super User
Super User

you can load all the file by the below code (with comments for each step)

 

let
    // Set the folder path
    FolderPath = "C:\YourFolderPath\",

    // Load all files from the folder
    Source = Folder.Files(FolderPath),

    // Filter to only include Excel files
    ExcelFiles = Table.SelectRows(Source, each Text.EndsWith([Name], ".xlsx") or Text.EndsWith([Name], ".xls")),

    // Add a column to extract data from each file
    AddCustom = Table.AddColumn(ExcelFiles, "Data", each Excel.Workbook([Content], true)),

    // Expand the Data column to get each sheet from each file
    ExpandSheets = Table.ExpandTableColumn(AddCustom, "Data", {"Name", "Data"}, {"SheetName", "Data"}),

    // If you want to filter by specific sheet names, uncomment and modify this line:
    // FilterSheets = Table.SelectRows(ExpandSheets, each [SheetName] = "Sheet1" or [SheetName] = "Sheet2"),

    // Expand the data in each sheet
    ExpandData = Table.ExpandTableColumn(ExpandSheets, "Data", Table.ColumnNames(ExpandSheets[Data]{0})),

    // Optional: Add a column to identify the source file name
    AddSourceInfo = Table.AddColumn(ExpandData, "SourceFile", each [Name])
in
    AddSourceInfo

 

 

If this answer helped resolve your issue, please consider marking it as the accepted answer. And if you found my response helpful, I'd appreciate it if you could give me kudos. Thank you!

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

where do i put this code? i put in power query it gives me as below ...

 

Error_PowerBI.png

Go to the power query, then in the Home tab, pick Advance EDitor, in the oppened window, remove all the command and past the code I have provided.

 

in the code I have mention 

C:\YourFolderPath\

as folder path, replace it by your own folder path

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

this helps a lot! any recommendation where i can learn more on advance editor?

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.