Sub topic 5
Connecting to a CSV file: delimiter, encoding, and headers
This chapter assumes you can open Power BI Desktop, use the Power Query Editor, and read a CSV file in a text editor. You should also know what a column data type is. The goal is to understand what the Text/CSV connector does when it opens a file, so you can fix the file when it parses wrongly.
How the Text/CSV connector works
A CSV file is plain text, so it carries no metadata. It has no column types, no declared encoding, and no guarantee about which character separates fields. The connector has to infer or be told these things.
When you choose Get Data > Text/CSV and pick a file, Power BI shows a preview dialog with three settings:
- File Origin is the text encoding.
- Delimiter is the character that separates fields.
- Data Type Detection chooses how types are guessed: based on the first 200 rows, based on the entire dataset, or not at all.
Click Transform Data to open the Power Query Editor. The dialog's choices are written into M code, the Power Query language. A typical result looks like this:
let
Source = Csv.Document(File.Contents("C:\Data\orders.csv"), [Delimiter = ",", Encoding = 65001, QuoteStyle = QuoteStyle.Csv]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),
Typed = Table.TransformColumnTypes(Promoted, {{"OrderID", Int64.Type}, {"OrderDate", type date}})
in
TypedEach line is one applied step. Csv.Document reads the bytes and splits them into rows and columns. At this point every column is named Column1, Column2, and so on, and every value is text. Table.PromoteHeaders turns the first row into column names. Table.TransformColumnTypes then assigns real types.
Errors usually belong to one stage. Garbled characters or one giant column point to stage one. Wrong column names point to stage two. Error cells or wrong numbers and dates point to stage three. Fix the stage that is actually wrong, not the one where you noticed the symptom.
Delimiter
The comma is the default, but many files use something else. Spreadsheet software in locales where the comma is the decimal separator (much of Europe, for example) often exports with a semicolon. Tab-separated and pipe-separated files are also common.
If the delimiter is wrong, the whole row lands in a single column. In the preview dialog, change Delimiter to the correct choice. In M, you edit the Delimiter option:
- Semicolon:
Delimiter = ";" - Pipe:
Delimiter = "|" - Tab:
Delimiter = "#(tab)"
M uses #(tab) as an escape for the tab character. Typing a literal tab inside the quotes is fragile, because editors may convert it to spaces.
Encoding
An encoding maps bytes to characters. The Encoding option takes a numeric code page. These are the values you will meet most often:
| Code | Encoding | Typical source |
|---|---|---|
| 65001 | UTF-8 | Most modern tools, web exports |
| 1252 | Windows Western European | Older Excel "CSV" exports on Windows |
| 1200 | UTF-16 little-endian | Some database and Windows tool exports |
| 28591 | ISO-8859-1 | Older Unix and legacy systems |
The classic symptom of a wrong encoding is mojibake: "café" appears as "café". That usually means a UTF-8 file was read as 1252. The reverse mistake shows a replacement character, such as "caf�". In the dialog, change File Origin until the preview looks right. Check a row with accented characters, not just the first rows.
Quote handling
QuoteStyle controls how double quotes are treated. With QuoteStyle.Csv, a quoted field may contain the delimiter or a line break, which is how most CSV files escape such values. With QuoteStyle.None, quotes are ordinary characters and a comma inside a quoted field splits it into two columns.
Use QuoteStyle.Csv unless you know the file never quotes anything. If your row count is higher than the number of records in the file, look for fields with embedded line breaks that were read with QuoteStyle.None.
Promoting headers
Right after Csv.Document, the real header row is just the first data row. Table.PromoteHeaders promotes it. The PromoteAllScalars = true option matters when a header looks like a number or date, such as a column named 2026. With the option set to false, only text values are promoted, so a numeric-looking header is left as data and the column gets a generic name.
Two header problems come up often:
- Preamble lines. Some exports put a title or report date above the header. Remove them with
Table.Skipor Remove Rows > Remove Top Rows before promoting headers. - Duplicate header names. Power Query makes names unique by appending
.1,.2, and so on. Rename the columns on purpose so later steps do not depend on the suffixes.
Setting column types and culture
Power Query reads CSV values as text, so the types you set decide how values are interpreted. The interpretation depends on a culture, which defines the decimal separator, the thousands separator, and the date order. By default Power Query uses the culture of your Power BI Desktop installation. A report built on an English (US) machine and refreshed with different settings can parse the same file differently.
Pass the culture to Table.TransformColumnTypes as a third argument to make parsing independent of the machine. This example reads a semicolon-delimited, Windows-1252 file that uses European number and date formats:
let
Source = Csv.Document(File.Contents("C:\Data\orders_eu.csv"), [Delimiter = ";", Encoding = 1252, QuoteStyle = QuoteStyle.Csv]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),
Typed = Table.TransformColumnTypes(
Promoted,
{{"OrderID", Int64.Type}, {"OrderDate", type date}, {"Amount", type number}, {"PostalCode", type text}},
"de-DE"
)
in
TypedWith "de-DE", the text 1.234,56 becomes the number 1234.56 and 27.11.2026 becomes a valid date. The same text parsed with "en-US" would produce an error or a wrong value.
In the editor you can get the same effect through Transform > Data Type > Using Locale, which writes the culture into the step.
Why the detection setting matters
Detection based on the first 200 rows is fast, but a column that holds whole numbers for 200 rows and then a value like N/A will be typed as a whole number and produce errors on refresh. For files with irregular data, choose detection on the entire dataset, or turn it off and set types yourself. Setting types yourself is the more predictable option for a report that will be refreshed on a schedule.
Parameterizing the path
A hard-coded path such as C:\Data\orders.csv breaks when the file moves or when a colleague opens the report. A Power Query parameter named FolderPath can hold the location:
let
Source = Csv.Document(File.Contents(FolderPath & "orders.csv"), [Delimiter = ",", Encoding = 65001]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars = true])
in
PromotedThe parameter value must end with a backslash, for example C:\Data\, because the query simply joins the two strings. Parameters are covered in more detail in their own chapter.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| All values in one column | Wrong delimiter | Set `Delimiter` to the actual separator |
| Characters like `é` | UTF-8 read as 1252 | Set `Encoding = 65001` |
| Extra rows, split text fields | `QuoteStyle.None` on a quoted file | Use `QuoteStyle.Csv` |
| `Column1`, `Column2` as names | Headers not promoted | Add `Table.PromoteHeaders` |
| `Error` in date or number cells | Culture mismatch or stray text | Set the culture, then inspect the bad rows |
| `Expression.Error: The column 'X' of the table wasn't found` | Header renamed in the source file | Update the column names in the type step |
The last error occurs because the type step names columns explicitly. If the file's owner renames a header, the refresh fails. Keep a note of which source columns your steps depend on.
Exercise
A semicolon-delimited export from a colleague loads into Power BI as one column. After you fix that, the name "José" appears as "José", and the amount "1.250,75" becomes an error in a column typed as type number. Which three settings do you change, and to what?
Show answer
Set Delimiter = ";" so the fields split into columns. Set Encoding = 65001 (UTF-8), because "José" is UTF-8 text read as Windows-1252. Pass a culture that uses a comma as the decimal separator, such as "de-DE", as the third argument of Table.TransformColumnTypes, so 1.250,75 parses as 1250.75.
Exercise
A CSV file has two title lines above the header row, and a column of postal codes such as 02134 shows up as 2134 after loading. How do you fix each problem?
Show answer
Remove the top two rows before promoting headers, using Remove Top Rows or Table.Skip(Source, 2), so the real header row becomes the first row. Set the postal code column to type text instead of a number type, so the leading zero is kept.