Sub topic 8
Reading Excel and CSV files from a SharePoint site
This chapter assumes you can already connect to a local Excel workbook and a local CSV file. It also assumes you know what applied steps are in Power Query Editor. You should be comfortable with Excel.Workbook and Csv.Document, which earlier chapters covered. Here you will apply the same skills to files stored in a SharePoint Online document library.
What changes when the file lives in SharePoint
For a local file, File.Contents reads bytes from a disk path. For a file in SharePoint, the connector first asks the site for a catalog of files, and you then pick one row from that catalog. The row's Content column holds the binary data. You pass that binary to the same functions you used before, Excel.Workbook or Csv.Document.
The pattern has three stages:
- List files with
SharePoint.Files. - Select one row by
NameandFolder Path. - Parse the
Contentbinary.
Using the SharePoint Folder connector
In Power BI Desktop, choose Get data, then SharePoint Folder. Enter the site URL, then sign in when prompted.
The site URL is the address of the site, such as https://contoso.sharepoint.com/sites/Sales. It is not the address of a library, a folder, or a file. If you paste a longer address, the connector may fail or return an unexpected set of files. Strip everything after the site name.
The preview lists every file in every document library on that site. Each row has these columns:
| Column | Contents |
|---|---|
| Content | The file as a binary value |
| Name | The file name, such as `Sales.xlsx` |
| Extension | The extension including the dot, such as `.xlsx` |
| Date accessed, Date modified, Date created | Timestamps |
| Attributes | A record with details such as size and hidden flags |
| Folder Path | The full URL of the containing folder, ending in `/` |
Choose Transform Data rather than Combine. Combine is for many files with the same layout, which a later chapter covers. For one file, you want to select a single row yourself.
Reading one Excel workbook
Filter the list down to one file, then parse its content. This is the query from the topic's example:
let
Source = SharePoint.Files("https://contoso.sharepoint.com/sites/Sales", [ApiVersion = 15]),
FileContent = Source{[Name = "Sales.xlsx", #"Folder Path" = "https://contoso.sharepoint.com/sites/Sales/Shared Documents/"]}[Content],
Workbook = Excel.Workbook(FileContent, true, true),
SalesTable = Workbook{[Item = "Sales", Kind = "Table"]}[Data]
in
SalesTableEach step does one job:
SourcecallsSharePoint.Files. The optionApiVersion = 15selects the SharePoint 2013-and-later API, which is what SharePoint Online expects.FileContentuses record lookup,Source{[...]}, to return the single row where both fields match, then takes itsContentfield. If no row or more than one row matches, the step errors.Excel.Workbook(FileContent, true, true)lists the workbook's objects. The second argument,true, uses the first row as headers. The third,true, delays type detection so Power Query does not guess types on large sheets.SalesTablepicks the object whoseItemisSalesand whoseKindisTable. UseKind = "Sheet"for a worksheet, orKind = "DefinedName"for a named range.
As with local files, prefer an Excel table over a sheet. A table has a fixed header row and an exact range, so blank rows and stray notes below the data do not leak into your result.
After selecting the table, set column types explicitly with Table.TransformColumnTypes, as you did for local workbooks. Type detection is delayed here, so untyped columns arrive as any.
Reading one CSV file
The CSV query differs only in the parsing step. Everything up to Content is the same:
let
Source = SharePoint.Files("https://contoso.sharepoint.com/sites/Sales", [ApiVersion = 15]),
FileContent = Source{[Name = "orders.csv", #"Folder Path" = "https://contoso.sharepoint.com/sites/Sales/Shared Documents/Exports/"]}[Content],
Parsed = Csv.Document(FileContent, [Delimiter = ",", Encoding = 65001, QuoteStyle = QuoteStyle.Csv]),
Promoted = Table.PromoteHeaders(Parsed, [PromoteAllScalars = true]),
Typed = Table.TransformColumnTypes(Promoted, {{"OrderID", Int64.Type}, {"OrderDate", type date}})
in
TypedNotice that Csv.Document receives the binary value FileContent instead of File.Contents(path). Delimiter, encoding, and header handling work exactly as in the local CSV chapter. Encoding = 65001 is UTF-8. Set the delimiter to ";" for files exported from locales that use the semicolon.
Why the row lookup is fragile
The lookup Source{[Name = ..., #"Folder Path" = ...]} depends on an exact text match. Three details cause most failures:
- Trailing slash.
Folder Pathends with/. If you type the path without it, no row matches. - Encoded spaces and case. Use the path exactly as the preview shows it. Do not copy a browser address that contains
%20. - Renamed or moved files. Renaming the file or moving it to another folder breaks the lookup at the next refresh.
A more tolerant alternative is to filter instead of looking up a single row. Filtering returns an empty table, not an error, when nothing matches. You can then decide how to handle the case:
let
Source = SharePoint.Files("https://contoso.sharepoint.com/sites/Sales", [ApiVersion = 15]),
InFolder = Table.SelectRows(
Source,
each [Folder Path] = "https://contoso.sharepoint.com/sites/Sales/Shared Documents/Exports/" and [Extension] = ".csv"
),
Names = Table.SelectColumns(InFolder, {"Name", "Date modified"})
in
NamesThis returns a list of the CSV files in one folder, a useful way to check what the connector actually sees before you write a lookup. It is also the starting point for combining files.
Performance on large sites
SharePoint.Files enumerates every file on the site, including files in other libraries and in nested folders. On a site with tens of thousands of files, the preview and each refresh can be slow. Reduce the cost:
- Filter on
Folder PathandExtensionas early as possible, ideally as the first step afterSource. - Point at a dedicated site for data files when you can, rather than a large team site.
- Avoid steps that force a full evaluation of the catalog before filtering, such as sorting or expanding the
Attributesrecord.
An alternative: the Web connector with a file URL
You can also read a single file by its direct URL with the Web connector:
let
Source = Excel.Workbook(Web.Contents("https://contoso.sharepoint.com/sites/Sales/Shared Documents/Sales.xlsx"), true, true),
SalesTable = Source{[Item = "Sales", Kind = "Table"]}[Data]
in
SalesTableThis avoids listing the whole site and is quick for one file. The URL must be the plain file address. Use the file's Copy link or the address of the file in the library, and remove query strings such as ?web=1. The trade-off is that the file path is a single hard-coded string with no catalog to browse, and it cannot combine a folder of files. For one stable workbook, it is fine. For anything that grows or moves, use SharePoint.Files.
Authentication and privacy
The first time you connect, Power BI asks how to sign in. For SharePoint Online, choose Microsoft account (organizational account) and sign in with your work identity. Anonymous access works only when the site is public, which is rare. Credentials are stored per site URL, so each distinct site prompts once. You can review and clear them under File, Options and settings, Data source settings.
If your query combines SharePoint data with another source, such as a local CSV, Power Query checks privacy levels before it lets data from one source influence another. Set the SharePoint source to Organizational. A later chapter explains privacy levels in full.
Refresh behaviour
After you publish, a SharePoint Online file in a document library is reachable from the Power BI service over the internet. The service signs in with the credentials stored on the dataset, so refresh normally works without an on-premises data gateway. A local path such as C:\Data\Sales.xlsx does need a gateway, because the service cannot reach your disk. This difference is the main reason teams move files to SharePoint.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Key didn't match any rows in the table` | Name or `Folder Path` in the lookup does not match exactly | Check the trailing slash, spelling, and case against the preview |
| Preview is empty after filtering | Wrong folder URL, or `Extension` written without the leading dot | Use `".csv"` and copy `Folder Path` from the data |
| Sign-in loop or `Access denied` | Wrong account, or no permission on the site | Clear permissions in Data source settings and sign in again |
| Garbled characters in CSV | Encoding does not match the file | Set `Encoding = 65001` for UTF-8 or the file's real code page |
| Slow refresh | Whole site enumerated | Filter by `Folder Path` first, or use a smaller site |
Exercise
A query uses Source{[Name = "Sales.xlsx", #"Folder Path" = "https://contoso.sharepoint.com/sites/Sales/Shared Documents"]}[Content] and fails with "Key didn't match any rows". The file exists in that folder. What is the most likely cause, and how can you confirm it?
Show answer
The Folder Path value is missing its trailing slash. SharePoint.Files returns Folder Path ending in "/", so the text does not match any row. Confirm by previewing the Source step and comparing the Folder Path value, or by filtering with Table.SelectRows on Name and inspecting the Folder Path column. Add the trailing slash to fix the lookup.
Exercise
You need to read "orders.csv" from SharePoint Online and parse it as UTF-8 with a comma delimiter. Which two functions do you use after selecting the Content binary, and what does Table.PromoteHeaders do in this query?
Show answer
Use Csv.Document with [Delimiter = ",", Encoding = 65001, QuoteStyle = QuoteStyle.Csv] to parse the binary, then Table.PromoteHeaders to turn the first data row into column names. Csv.Document returns generic columns (Column1, Column2, and so on), so without promoting headers the real field names stay in row one.