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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
avi081265
Helper III
Helper III

How to combine SharePoint List from multiple SharePoint sites

Hello 

 

I want to combine SharePoint List from different SharePoint sites. All the list having same kind of structure. My Idea that I will create one SharePoint LIst which we will have four columns i.e. Tenant Name,SIte Name,List Name and Site code. Then I want to read the data from this list and append or merge all the list which are exist in above list.

 

Please let me know how can I achieve this. If any article or Video please let me know.

 

Regards

Avian

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I cannot share the pbix sample but I can provide the explanation of the code:

The code is designed to fetch data from multiple SharePoint lists based on the URLs and list names provided in an Excel file.

The first part of the code reads an Excel file. This Excel file contains a list of SharePoint site URLs and corresponding list names. The Excel.Workbook function is used to load the Excel file, and the Table.RenameColumns function is used to rename the columns to "Site URL" and "List Name".

Next, a custom function named FetchList is defined. This function takes a SharePoint site URL as an argument and returns a table of all lists in the SharePoint site. The SharePoint.Tables function is used to fetch the lists from the SharePoint site.

Finally, a new column named "Custom" is added to the table. For each row in the table, the FetchList function is called with the site URL from that row. The result is a table with an additional column that contains the SharePoint lists for each site URL.

This code is a common pattern in Power Query M language for fetching and combining data from multiple sources based on a list of parameters.

View solution in original post

27 REPLIES 27
avi081265
Helper III
Helper III

I also tried following, but it is showing Token Eof

avi081265_0-1689759780387.png

Not sure what is the issue?

 

However original question is still open

@avi081265 

 

Where is the in? did you use in? 

 

like

 

in

#"Filtered Rows"

 

 

also, modify the code this way,

let Combined = Table.Combine({SharePoint.Tables("https://main.sharepoint.com/site1", [ApiVersion = 15]){[Name="ProjectList"]}[Content], SharePoint.Tables("https://main.sharepoint.com/site2", [ApiVersion = 15]){[Name="ProjectList"]}[Content]}) in Combined

 

rubayatyasmin_0-1689517080227.png


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


rubayatyasmin
Super User
Super User

Hi, @avi081265 

 

To combine SharePoint lists from different SharePoint sites, you can use Power Query in Power BI. Power Query allows you to connect to various data sources, including SharePoint lists, and perform data transformations.

Here is a step-by-step guide on how to achieve this:

  1. Open Power BI Desktop and click on "Get Data" from the Home tab.

  2. In the "Get Data" window, search for and select "SharePoint Online List" as the data source.

  3. Enter the URL of one of the SharePoint sites containing the lists you want to combine. Click OK.

  4. In the Navigator window, select the SharePoint list you want to import and click Edit. This will open the Power Query Editor.

  5. In the Power Query Editor, you can perform any necessary data transformations, such as removing unnecessary columns or filtering rows. Make sure the column names and data types match across all the lists you want to combine.

  6. Once you've finished transforming the data, click Close & Apply to load the data into Power BI.

  7. Repeat steps 3 to 6 for each SharePoint site and list you want to include in the combined list.

  8. After importing data from all the SharePoint lists, you will have multiple queries in the Power Query Editor. To merge or append the data, you can use the "Append Queries" or "Merge Queries" options from the Home tab.

    • Append Queries: Use this option to stack the rows of multiple tables vertically. The column names and data types must match for successful appending.

    • Merge Queries: Use this option when you have a common key column in all the queries and want to combine the columns horizontally based on that key.

  9. Once you have appended or merged the queries, you can further refine the combined list if needed.

  10. Finally, click Close & Apply to load the combined data into Power BI. You can now use this data to create visualizations and reports.

rubayatyasmin_0-1689517080227.png


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


Hello Rubayart,

 

Thanks for quick response. I already followed these steps. As i mentioned earlier that I am maintain a list with site url and list name. I want that in what ever siteurl and list available in this list, should populate a common dataset instead of adding of each site and append.

 

Logic behind that, today I have 4 sites, in fuure I have more sites, so I have to add/update the url in particular list.  so I need to update report every time.

 

Can you please guid here that how can I read the site url from particular site url list and combine or append all the site url in one dataset.

 

Thanks again

Avian 

try this one. 

 

 

let
    // Get the list of SharePoint sites and corresponding list names
    Source = SharePoint.Tables("https://main.sharepoint.com", [ApiVersion = 15]),
    SitesList = Source{[Name="SitesList"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(SitesList,{{"Site URL", type text}, {"List Name", type text}, {"Tenant Name", type text}, {"Site Code", type text}}),

    // Add a custom function to fetch data from the corresponding SharePoint list for each SharePoint site
    FetchList = (site as text, list as text) => 
        let
            Source = SharePoint.Tables(site, [ApiVersion = 15]),
            List = Source{[Name=list]}[Content]
        in
            List,

    // Add a new column "Custom" to call the custom function for each row
    AppendLists = Table.AddColumn(#"Changed Type", "Custom", each FetchList([Site URL], [List Name])),

    // Expand the tables from the "Custom" column into rows
    #"Expanded Custom" = Table.ExpandTableColumn(AppendLists, "Custom", {"Employee ID", "Name", "Position"}, {"Employee ID", "Name", "Position"}),

    // Select only the necessary columns
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded Custom",{"Tenant Name", "Site Code", "Employee ID", "Name", "Position"})
in
    #"Removed Other Columns"

 

This script will:

  1. Fetch the list of SharePoint sites and their corresponding list names from your main SharePoint site.
  2. For each SharePoint site, it fetches the corresponding SharePoint list.
  3. It then expands each fetched SharePoint list into rows and appends these rows into the main table.
  4. Finally, it selects only the necessary columns.

Please replace "Employee ID", "Name", "Position" with your actual column names in the SharePoint Lists. If your lists have different structures, you'll need to modify the script accordingly to accommodate those differences.

Also, please replace "https://main.sharepoint.com" with your main SharePoint site URL and "SitesList" with your actual list name that contains the SharePoint site URLs and corresponding list names.

 

this is an experimental solution. you need to adjust it. 

 

 

rubayatyasmin_0-1689517080227.png


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


@rubayatyasmin Is it possible to just have it show all columns of the sharepoint list, if so what would the code be.

 

I tried your code but I was just getting an error

 

Expression.Error: The key didn't match any rows in the table.
Details:
Key=
Name=Site Sign In
Table=[Table]

 

J

Hi @jbrines thanks for reaching out. Can you tell me in which step you are getting the error? And yes it's possible to read the columns from SP list. Also, make sure you have replaced the necessary changes. 


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


Hi @rubayatyasmin see screenshot below.

 

Lists error.jpg

 

If I can get this working then I won't need the code for all columns

 

J

Can you share the m code?


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


@rubayatyasmin sure see below.

 

let
    // Get the list of SharePoint sites and corresponding list names
    Source = SharePoint.Tables("https://ourdomain.sharepoint.com", [ApiVersion = 15]),
    SitesList = Source{[Name="Site Sign In"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(SitesList,{{"Site URL", type text}, {"List Name", type text}, {"Tenant Name", type text}, {"Site Code", type text}}),

    // Add a custom function to fetch data from the corresponding SharePoint list for each SharePoint site
    FetchList = (site as text, list as text) => 
        let
            Source = SharePoint.Tables(site, [ApiVersion = 15]),
            List = Source{[Name=list]}[Content]
        in
            List,

    // Add a new column "Custom" to call the custom function for each row
    AppendLists = Table.AddColumn(#"Changed Type", "Custom", each FetchList([Site URL], [List Name])),

    // Expand the tables from the "Custom" column into rows
    #"Expanded Custom" = Table.ExpandTableColumn(AppendLists, "Custom", {"Title", "Profession"}, {"Title", "Profession"}),

    // Select only the necessary columns
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded Custom",{"Tenant Name", "Site Code", "Title", "Profession"})
in
    #"Removed Other Columns"

Hi @rubayatyasmin any ideas on why it isn't working?

The second line after source { "site sign ..... } You should be able to see the list of contents of your SP site. In your first step can you see the initial view at least? Also, make sure you are using the correct list name. 


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


Hi @rubayatyasmin ,

 

I think the issue is that I am not understanding and I am new to Power BI

 

With just having the first two lines this is what I am seeing, I have tried several names for the SitesLists but no luck.

 

sp lists.jpg

also in 
in Source should be SitesList because that is the last step. 


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


Hey, you want to read some specific files right? that are in a folder inside SharePoint? Use SharePoint folder connector. 
Power Query SharePoint folder connector - Power Query | Microsoft Learn


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


Hi @rubayatyasmin ,

 

No I am looking to read a specific list on each Sharepoint site.

 

We have a "Site Sign In" list on each site that has the same colmuns and I want to be able to read specific colums or all columns so I then can generate a table to show them from all site together.

 

John

Hi @jbrines 

 

Then use SharePoint list connector. Here is the document that can help you. 

 

https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-sharepoint-online-list

 


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


Hi @rubayatyasmin it appears to be failing on the last line of the code

 

In
  #"Removed Other Columns"

 

It doesn't give an error when I use

 

in
    Source

 

Anonymous
Not applicable

After editing your code with my own data, I was able to import multiple lists into Power BI. Howerver, I am stuck with refreshing the Dataset in Power BI Service because it does not support dynamic data source refreshing. I am aware of the solution for the Web.Contents function using RelativePath and Query headers but I am getting the authorization error when trying to use Web.Contents to fetch SharePoint lists even though I am admin of the SharePoint. Any suggestions on how to refresh dynamic data sources in Power BI Service when using SharePoint related function in Power Query?

Hello Samper,

 

Can you please share your code, I am not able to fetch the lists.

 

Avian

Helpful resources

Announcements
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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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