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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
TSlowik
New Member

PowerQuery - load first 10 rows of every excel sheet from multiple files

Hi,

 

I have a SharePoint with multiple excel files and in every file there are multiple sheets. What I want to do, is to load first 10 rows from every sheet from every excel file at the same time. Number of sheets in these excels range from 5 to 10.

 

Based on other answers I tried to create a function which will load first 10 rows on the sample file, and use it on all the files. It only works partially, as it does not include all the sheets.

 

Is it a way to do it somehow? Thank you!

2 ACCEPTED SOLUTIONS
lbendlin
Super User
Super User

yes, in addition to enumerating the files you also need enumerate all the sheets in each file.  First create a list of all files and their sheets and then use a custom column to select the top 10 rows from each of the elements in the list. Then expand that custom column.

View solution in original post

vojtechsima
Resident Rockstar
Resident Rockstar

Hello, @TSlowik ,
here's a complete solution. 

vojtechsima_0-1730636062858.png

Just fill the parameters and then in query "Files" change first step to Sharepoint.Files, or keep it and test it for local files.

vojtechsima_1-1730636127777.png


Here's link for my file:

first10RowsForEachSheetEachFile.pbix

 

quick steps:

  1. create sample file operation
    1. get file
    2. select first 10 rows
    3. do that for each sheet
  2. get all files
  3. call function for all files
  4. expand it

View solution in original post

3 REPLIES 3
Ahmedx
Super User
Super User

pls try this

let
    // Step 1: Load all files from the specified folder.
    from = Folder.Files("C:\Users\User\Desktop\ggg"),
    
    // Step 2: Filter to include only ".xlsx" files that are not hidden.
    filtr = Table.SelectRows(from, each [Extension] = ".xlsx" and [Attributes][Hidden] = false),
    
    // Step 3: Select only the "Name" and "Folder Path" columns from the filtered table.
    tbl = Table.SelectColumns(filtr, {"Name", "Folder Path"}),
    
    // Step 4: Define a function (func) that will:
    //    a. Load each Excel workbook's content as a table using the full path.
    //    b. Access each sheet in the workbook and take only the first 10 rows.
    //    c. Combine these 10 rows from each sheet into one single table.
    func = each [
        a = Excel.Workbook(File.Contents([Folder Path] & [Name]), null, true)[Data],
        b = Table.Combine(List.Transform(a, (x) => Table.FirstN(x, 10)))
    ][b],
    
    // Step 5: Apply the function (func) to each file in the table and combine all results into a final table.
    final = Table.Combine(Table.AddColumn(tbl, "tmp", func)[tmp])
in
    final
vojtechsima
Resident Rockstar
Resident Rockstar

Hello, @TSlowik ,
here's a complete solution. 

vojtechsima_0-1730636062858.png

Just fill the parameters and then in query "Files" change first step to Sharepoint.Files, or keep it and test it for local files.

vojtechsima_1-1730636127777.png


Here's link for my file:

first10RowsForEachSheetEachFile.pbix

 

quick steps:

  1. create sample file operation
    1. get file
    2. select first 10 rows
    3. do that for each sheet
  2. get all files
  3. call function for all files
  4. expand it
lbendlin
Super User
Super User

yes, in addition to enumerating the files you also need enumerate all the sheets in each file.  First create a list of all files and their sheets and then use a custom column to select the top 10 rows from each of the elements in the list. Then expand that custom column.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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