Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreShape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.
Hi, I'm trying to combine data from different Excel files and sheets from a SharePoint folder.
I'm attempting to accomplish this using a single query, but I'm encountering difficulties.
Here's an example of what I'm trying to achieve:
Here are the initial steps that are working:
Before proceeding with these steps, I need to implement the conditions I've described. I've tried various approaches, but I haven't been successful.
I would appreciate any assistance with this matter.
Solved! Go to Solution.
I think I have it!
For other people, this code:
Takes a sharepoint folder
Here is the code:
let
FolderUrl = "sharepoint folder",
//Get the list of files from SharePoint
Source = SharePoint.Files("sharepoint", [ApiVersion = 15]),
//Filter the files to select only those starting with "A" or "B"
FilteredFiles = Table.SelectRows(Source, each Text.StartsWith([Name], "A") or Text.StartsWith([Name], "B")),
//Define a function to load data from each Excel file
LoadData = (file as text) =>
let
//Fetch the binary content of the Excel file
ExcelContent = Web.Contents(FolderUrl & file),
//Load the Excel workbook
ExcelWorkbook = Excel.Workbook(ExcelContent),
//Determine the sheet name based on the file name
DataSheetName =
if Text.StartsWith(file, "A") then "C"
else if Text.StartsWith(file, "B") then "D"
else null,
//Extract data from the specified sheet
DataSheet = if DataSheetName <> null then ExcelWorkbook{[Item=DataSheetName, Kind="Sheet"]}[Data] else null,
//Skip the first row if data exists
SkipFirstRow = if DataSheet <> null then Table.Skip(DataSheet, 1) else null,
//Add a column to identify the source file
AddSourceColumn = Table.AddColumn(SkipFirstRow, "FileSource", each file, type text)
in
//Return the resulting table
AddSourceColumn,
//Apply the LoadData function to each file and combine the results
CombinedData = Table.Combine(List.Transform(FilteredFiles[Name], each LoadData(_)))
in
CombinedData
Hi @BMM27 ,
Below is my table:
The following M code will help you:
let
Source = Excel.Workbook(File.Contents("C:\Users\Administrator\Desktop\Project Files\" & [Project]&".xlsx"), null, true),
#"SheetData" = if Text.StartsWith(Source, "A") then
FileContent{[Item="B",Kind="Sheet"]}[Data]
else if Text.StartsWith(Source, "D") then
FileContent{[Item="C",Kind="Sheet"]}[Data]
else
null,
#"AdjustedData" = if Source = List.First(FilteredFiles[Name]) then
Table.PromoteHeaders(SheetData, [PromoteAllScalars=true])
else
Table.Skip(SheetData,1)
in
#"AdjustedData"
Best Regards,
Xianda Tang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi, thanks for the response. Could you clarify how [project] will be changed, or does this code imply that it will select all projects? Also, I'm a bit confused about how FilteredFiles is used later in the code.
The following code snippet may provide context. It should be placed below the FilteredFiles step, but it's currently not functioning as expected:
LoadData = (file) =>
let
ExcelContent = SharePoint.Files(FolderUrl & file){0}[Content],
ExcelWorkbook = Excel.Workbook(ExcelContent),
DataSheet =
if Text.StartsWith(file, "A") then
ExcelWorkbook{[Item="B", Kind="Sheet"]}[Data]
else if Text.StartsWith(file, "D") then
ExcelWorkbook{[Item="C", Kind="Sheet"]}[Data]
else
null,
SkipFirstRow = if DataSheet <> null then Table.Skip(DataSheet, 1) else null,
AddSourceColumn = Table.AddColumn(SkipFirstRow, "FileSource", each file, type text)
in
AddSourceColumn,
CombinedData = Table.FromRecords(List.Transform(FilteredFiles[Name], each LoadData(_)))
in
CombinedData
Thanks!
I think I have it!
For other people, this code:
Takes a sharepoint folder
Here is the code:
let
FolderUrl = "sharepoint folder",
//Get the list of files from SharePoint
Source = SharePoint.Files("sharepoint", [ApiVersion = 15]),
//Filter the files to select only those starting with "A" or "B"
FilteredFiles = Table.SelectRows(Source, each Text.StartsWith([Name], "A") or Text.StartsWith([Name], "B")),
//Define a function to load data from each Excel file
LoadData = (file as text) =>
let
//Fetch the binary content of the Excel file
ExcelContent = Web.Contents(FolderUrl & file),
//Load the Excel workbook
ExcelWorkbook = Excel.Workbook(ExcelContent),
//Determine the sheet name based on the file name
DataSheetName =
if Text.StartsWith(file, "A") then "C"
else if Text.StartsWith(file, "B") then "D"
else null,
//Extract data from the specified sheet
DataSheet = if DataSheetName <> null then ExcelWorkbook{[Item=DataSheetName, Kind="Sheet"]}[Data] else null,
//Skip the first row if data exists
SkipFirstRow = if DataSheet <> null then Table.Skip(DataSheet, 1) else null,
//Add a column to identify the source file
AddSourceColumn = Table.AddColumn(SkipFirstRow, "FileSource", each file, type text)
in
//Return the resulting table
AddSourceColumn,
//Apply the LoadData function to each file and combine the results
CombinedData = Table.Combine(List.Transform(FilteredFiles[Name], each LoadData(_)))
in
CombinedData
User | Count |
---|---|
92 | |
90 | |
88 | |
82 | |
49 |
User | Count |
---|---|
156 | |
145 | |
104 | |
72 | |
55 |