Sub topic 2
Importing from an Excel table, a CSV file, and other sources
This chapter assumes you can work with Excel tables and know what text, number, and date types are. You will import data with Get & Transform and read the M code it generates.
How importing works
Every import starts on the Data tab, in the Get & Transform Data group. Excel connects to the source and shows a preview. You then choose Transform Data to open the Power Query Editor, or Load to send the data straight to a worksheet. For anything that needs cleaning, choose Transform Data.
| Source | Where to find it | M function used |
|---|---|---|
| Excel table or range in this workbook | Data > From Table/Range | `Excel.CurrentWorkbook` |
| CSV or text file | Data > From Text/CSV | `Csv.Document` |
| Folder of files | Data > Get Data > From File > From Folder | `Folder.Files` |
| Database or web | Data > Get Data > From Database or From Other Sources | Varies by connector |
Importing from an Excel table
Convert your range to a table first (select a cell and press Ctrl+T), and name it, for example SalesRaw. Then select a cell in the table and choose From Table/Range. A named table keeps working when rows are added, because the query reads the table and not a fixed cell range.
The generated query below reads the table, sets types, and cleans the Region text:
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
Typed = Table.TransformColumnTypes(Source, {{"Date", type date}, {"Amount", type number}}),
Trimmed = Table.TransformColumns(Typed, {{"Region", each Text.Trim(Text.Clean(_)), type text}})
in
TrimmedThe first step selects the table by name. The {[Name="SalesRaw"]} part picks the row for that table, and [Content] returns its data.
Importing from a CSV file
Choose From Text/CSV and select the file. The preview dialog shows three settings you should check before loading:
- File Origin: the encoding. Use 65001: UTF-8 if accented characters look wrong.
- Delimiter: comma, semicolon, tab, or another character.
- Data Type Detection: Excel guesses types from the first rows. It can guess wrong, so check types again in the editor.
Here is the M code for a UTF-8 CSV file:
let
Source = Csv.Document(File.Contents("C:\Data\sales.csv"), [Delimiter=",", Encoding=65001, QuoteStyle=QuoteStyle.Csv]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
Typed = Table.TransformColumnTypes(Promoted, {{"OrderID", Int64.Type}, {"Amount", type number}})
in
TypedFile.Contents reads the raw bytes, and Csv.Document interprets them as a table. Table.PromoteHeaders turns the first row into column names.
Other sources
The same menu offers databases (SQL Server and others), web pages, JSON, XML, and PDF files. The steps are the same: connect, preview, then transform. Some sources ask for credentials the first time. Excel stores them per source, and you can change them under Data > Get Data > Data Source Settings.
Common errors
- "The input couldn't be recognized as a valid Excel table" or a missing table: the table was renamed or deleted. Fix the name in the
Sourcestep. - The file path breaks when you move the workbook to another computer. Keep files in a shared location, or use a parameter for the path (covered in a later chapter).
- Garbled characters in a CSV: the encoding is wrong. Change File Origin and reload.
- Types guessed wrongly, such as an ID with leading zeros turned into a number. Set the type to text, and do it before other steps.
Exercise
A CSV file shows "José" as "José" in the preview. Which setting do you change, and to what value?
Show answer
Change File Origin (the encoding) to 65001: UTF-8. In M, this is the Encoding=65001 option of Csv.Document.
Exercise
Why is it better to import from a named Excel table than from a fixed cell range?
Show answer
A named table grows and shrinks with your data, and the query reads it by name. A fixed range would miss rows added later.