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 and how to set column types. Here you will read the same kinds of files from a SharePoint Online document library.
Why SharePoint files differ from local files
A local connector such as File.Contents points at one path on your disk. A SharePoint connector first lists every file on the site. You then pick the file you want from that list. The file's content is returned as binary data, which you pass to Excel.Workbook or Csv.Document.
The same loaded data can refresh in the Power BI service without a gateway. A local path cannot do that, because the service cannot reach your disk.
The two ways to connect
- SharePoint Folder (
SharePoint.Files): returns one row per file on the whole site, including all subfolders. You filter the rows to find yours. - Excel Workbook or Text/CSV with a web URL: you paste the full file URL. This works, but the URL is long and breaks if the file moves.
This chapter uses SharePoint.Files, because it also supports combining files later in the tutorial.
Read an Excel workbook from SharePoint
In Power BI Desktop, choose Get data, then SharePoint Folder. Enter the site URL, not the folder URL. Use the form https://contoso.sharepoint.com/sites/Sales. Sign in with an Organizational account when prompted.
The query below picks one workbook by name and folder path, then reads the table named Sales.
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
SalesTableApiVersion = 15selects the SharePoint API version that current SharePoint Online sites use.- The record
[Name = ..., #"Folder Path" = ...]selects a single row. It needs both fields, because two folders can hold files with the same name. The folder path must end with a slash. Excel.Workbook(FileContent, true, true)reads the binary content. The second argumenttrueuses the first row as headers. The third argumenttruelets Power Query detect column types.Kind = "Table"selects an Excel table. Use"Sheet"for a worksheet, or"DefinedName"for a named range.
Read a CSV file from SharePoint
The first steps are the same. Only the parser changes. Replace Excel.Workbook with Csv.Document, and set the delimiter and encoding yourself.
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/"]}[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
TypedEncoding 65001 is UTF-8. A CSV does not carry types, so you must always set them with Table.TransformColumnTypes.
Common errors
| Symptom | Likely cause |
|---|---|
| "The key didn't match any rows in the table" | The file name or folder path in the record is wrong. Check capitalization and the trailing slash. |
| Credentials prompt keeps returning | The site URL was entered with a subfolder. Use the site root. |
| Query is slow | `SharePoint.Files` lists every file on the site. Filter early on **Folder Path**. |
Exercise
You write Source{[Name = "Sales.xlsx"]}[Content] and get an error that more than one row matched. What is the cause, and how do you fix it?
Show answer
Two folders on the site contain a file named Sales.xlsx, so the name alone does not identify one row. Add the folder path to the record, as in Source{[Name = "Sales.xlsx", #"Folder Path" = "https://contoso.sharepoint.com/sites/Sales/Shared Documents/"]}[Content].
Exercise
You change the file from Sales.xlsx to orders.csv. Which function must replace Excel.Workbook, and which two options should you set explicitly?
Show answer
Use Csv.Document. Set Delimiter and Encoding, for example a comma and 65001 for UTF-8. Then promote the headers and set the column types yourself.