Forum Discussion
Second Parameter/Loop
- Anonymous5 years ago
Hi,
This can be done in PowerQuery/M. Given below is a step-by-step demo of the outputs at each stage. After that, I have given the PowerQuery/M code which you need to modify - mainly the first line. I have used "Folder.Files" function. You have to change the line to your Sharepoint source. Other than that, this should work without needing any major changes.
Please note that it is possible to achieve the same results using various other functions and a little bit of more complex code to optimize the process, but I have given it step-by-step based on our natural thought process for your understanding so that you would be able to modify it if required according to your needs.
Step 1: Fetch the contents of the Folder.
Step 2: Count the files.
Step 3:
Generate a list of file indices from 0 to (n-1) where n is the number of files in the folder.
Step 4:
Covert the list we got in Step 3 to a table.
Step 5:
Add the contents of the spreadsheets as binary using the file indices (0 to n-1) rather than predefined names.
Step 6: Add the file names.
Step 7:
Add the field with the count of sheets in each of those files. In this example, all the 3 files have 3 sheets in them.
Step 8:
Add the list of sheet indices. (Just like we did for the files)
Step 9:
Expand the SheetIndices list.
Step 10:
Add the sheet names of each file.
Step 11:
Add the sheet contents
Step 12:
Filter the sheet names with the word "Data" in them.
Step 13:
Remove unnecessary columns.
That's it. Now the contents of each file and each sheet is added to this table as a "Table". If you expand the last column, you will have your results.
Given below is the PowerQuery/M Code for the same.
let Source = Folder.Files("C:\Users\Sreenath\Documents\PowerBIDataSource"), FileCount = Table.RowCount(Source), FileIndexList = List.Generate(()=>0,each _ < FileCount, each _ +1), FileIndexTable = Table.FromList(FileIndexList, Splitter.SplitByNothing(), null, null, ExtraValues.Ignore), FileAdditions = Table.AddColumn(FileIndexTable,"ContentBinary", each Source{Record.Field(_,"Column1")}[Content]), FileAdditions2 = Table.AddColumn(FileAdditions,"FileName", each Source{Record.Field(_,"Column1")}[Name]), AddSheetCount = Table.AddColumn( FileAdditions2, "SheetCount", each Table.RowCount(Excel.Workbook(Record.Field(_,"ContentBinary"),null,true)) ), #"Changed Type" = Table.TransformColumnTypes(AddSheetCount,{{"SheetCount", Int64.Type}}), AddSheetIndices = Table.AddColumn( #"Changed Type", "SheetIndices", each let RC = Record.Field(_,"SheetCount") in List.Generate(()=>0,each _ < RC,each _ +1) ), ExpandIndicesColumn = Table.ExpandListColumn(AddSheetIndices, "SheetIndices"), RenameFileIndices = Table.RenameColumns(ExpandIndicesColumn,{{"Column1","FileIndex"}}), AddSheetNames = Table.AddColumn( RenameFileIndices, "Sheet Name", each let F = Excel.Workbook(Record.Field(_,"ContentBinary"),null,true), S=Record.Field(_,"SheetIndices") in F{S}[Name] ), AddSheetContents = Table.AddColumn( AddSheetNames, "Sheet Contents", each let S2 = let F = Excel.Workbook(Record.Field(_,"ContentBinary"),null,true), S=Record.Field(_,"SheetIndices") in F{S}[Data] in Table.PromoteHeaders(S2, [PromoteAllScalars=true]) ), FilterSheetsWithWord_Data_inIt = Table.SelectRows(AddSheetContents, each Text.Contains([Sheet Name], "Data")), RemoveUnnecessaryColuumns = Table.RemoveColumns(FilterSheetsWithWord_Data_inIt,{"ContentBinary","SheetCount"}) in RemoveUnnecessaryColuumnsPlease remember to change this line
Source = Folder.Files("C:\Users\Sreenath\Documents\PowerBIDataSource"),to
Source = SharePoint.Files("https://SHAREPOINTFOLDER", [ApiVersion = 15]),or whatever is your path to the directory/folder.
Hi Mat42 ,
Sounds like nested functions in power query, not certain but you could refer:
- Nested Functions in PowerQuery
- Custom function with rest API - Expand nested tables in columns
- Nested let in power query and variable scope- (Power Query Training) 11
Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
v-yingjl Thanks for your reply.
I've checked out the links you provided and can basically see how it works, but I don't know enough about M to put it in to practice.
I know that I need to adjust this section of code:
Date.ToText( Date.AddMonths(Date.From(DateTime.LocalNow()),-1),"MMMM yyyy") & " data",Kind="Sheet"]}[Data]
At the minute it is looking specifically for a sheet named for the month previous to the current month with the word 'data' at the end (so, in this case, it is looking for a sheet named 'March 2021 data'). I know I need to adjust it to only look for sheets containing the word 'data', which I can do, but I can't work out how to implement nested functions to loop the code to look at all sheets containing the word 'data' in the name and pull data from them.