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.
I am happy that my code is useful to you. But a word of caution - The powerquery that I have given is not designed to take a parameter. So it scans the folder and fetches all the excel files in that folder and processes them. If by mistake, there is an excel file in that folder that is not supposed to be there and by chance that file has a sheet that has the word "Data" in its name, then that scenario will mess up your entire table by loading its contents, probably in a haphazard manner. So for this code to work properly always, you have to ensure that only relevant files are stored in that SharePoint directory and any other files should be stored in some other directory so that this program does not fetch those unnecessary excel files and disrupts your data.
Anonymous I did notice that. Staff are under strict instructions to put nothing in the folder that shouldn't be there.
They won't listen, but the instructions are there.