Power Query Csv.Document: Load CSV Files Step by Step
In this article
Csv.Document is the M function Power Query uses to turn raw CSV text or a file reference into a table. You rarely type it by hand, but you see it every time you load a CSV: the query editor writes a line like = Csv.Document(File.Contents("C:\Data\sales.csv"), [Delimiter=",", Columns=6, Encoding=65001]) and then wraps it in Table.PromoteHeaders and change-type steps. Understanding that line is what lets you fix broken imports, handle odd delimiters, and build reusable queries that do not depend on a file dialog. This guide walks through loading a CSV properly, editing the generated function call, and solving the problems that come up. Menu labels in Power Query vary slightly between Excel and Power BI, and between versions and regions, so treat the exact wording as a guide.
What You'll Need
- A Windows or Mac desktop version of Excel with Power Query (it is built in from Excel 2016 onward), or Power BI Desktop, or another tool that hosts Power Query.
- A CSV file saved somewhere you can reach: a local folder, a network share, or OneDrive or SharePoint if you plan to refresh from the cloud.
- A rough idea of the file's structure: how many columns, what the delimiter is, and whether it uses commas or semicolons.
- For Power BI cloud refreshes or Excel workbooks shared online, the file must live in a data source the service can reach, such as OneDrive or SharePoint. A local drive path will not refresh on the service.
Step-by-Step
- Start with the data import. In Excel, open a blank workbook and go to Data > Get Data > From File > From Text/CSV. In Power BI Desktop, use Home > Get data > Text/CSV. Pick your file. This opens a preview dialog rather than dropping the table straight in.
- Check the preview and the key settings before loading. The dialog shows a File origin and Delimiter guess. If the columns look crammed into one, the delimiter is wrong: change it to the correct character or choose your own. For files with accented characters or symbols, set File origin to the UTF-8 option so text does not turn into question marks. Then choose Load To or Transform Data. Pick Transform Data so you land in the query editor.
- Open the Advanced Editor to see the actual function. In the query editor, go to Home > Advanced Editor. You will find
Csv.Documentnear the top, typically inside a step called something likeSource. The structure isCsv.Document(source, [options]), where source can be binary file content fromFile.ContentsorFolder.Files, or pasted text. - Learn the four options that matter.
Delimitersets the separator, for example",",";","|"or"#(tab)"for a tab.Columnstells Power Query how many columns to expect, which stops it guessing wrong on the first rows.Encodingis a code page number such as 65001 for UTF-8 or 1252 for Western European.QuoteStylecontrols how quoted fields are treated, which matters when your data contains the delimiter inside text. - Edit the call deliberately. If the first row was swallowed as data, the import likely used
Columnswith no header promotion. Keep theCsv.Documentstep as-is and add or adjust the following step, usually Home > Use First Row as Headers, or writeTable.PromoteHeadersin the formula bar. If your file has no headers at all, setTable.PromoteHeadersaside and rename columns manually instead. - Set data types explicitly. After headers are promoted, the query editor adds a "Changed Type" step based on a sample of the first rows. This is a hazard: if row 3 is a text note and the rest are numbers, the column can come in as text. Click a column's type icon near the header and pick the correct type yourself for date, number and currency columns, especially if you refresh the file regularly.
- Remove the hardcoded path if the query moves between devices. A
File.Contents("C:\Users\...")path breaks when the file is on a shared drive or another computer. For repeated imports of similarly shaped files, use Data > Get Data > From File > From Folder instead; Power Query generatesCsv.Documentinside a per-file function and lets the folder path be edited in one place. - Close and load. Choose Home > Close & Load, or Close & Load To to put the table on a worksheet or add it to the data model. The query stays linked to the file: Refresh re-reads it.
Common Mistakes
The most common mistake is leaving encoding on the wrong setting. A UTF-8 file read as Windows-1252 shows garbled characters in names and currency, and re-saving the CSV does not fix it. Set the correct origin in the import dialog or change the encoding number in the function call.
Second is trusting the delimiter guess. Exporters from European locales often use semicolons and decimal commas, so a comma-delimited import produces one wide column. Third is relying on the automatic type-change step: it samples rows and can generate a query that breaks on refresh when a later row has a blank or unexpected value. Delete it and set types per column. Fourth is hardcoding a local path shared with other people; the refresh fails for everyone but you.
A subtler one is files where a text field contains the delimiter, such as an address with a comma. Without proper quoting, the row splits into extra columns. The QuoteStyle option and the source file's quoting determine this, so test the preview carefully when columns shift.
Tips
Name your query steps meaningfully. Renaming optional steps in the Applied Steps pane makes a long query readable for anyone who opens it later.
Use the formula bar rather than only the interface for tricky options. Clicking in the formula bar lets you type Encoding=65001 directly, which is faster than hunting through a dialog.
When several CSVs share a layout, build one query and use it as a template. A folder query backed by Csv.Document combines all files and only needs the folder location updated.
Keep a known-good sample row with a blank cell, a text field containing the delimiter and a long number. Consistent refresh problems usually show up in exactly those rows.
Troubleshooting
Only one column appears. The delimiter is wrong. Open the source step and change Delimiter; semicolons and tabs are the usual culprits. Verify against the raw file in a text editor.
All values look like text. The type-change step did not run or the file has mixed values. Set the type on each column and check whether stray characters, such as spaces or thousands separators, are being read as text.
Characters show as question marks or symbols. Adjust Encoding. UTF-8 is 65001. If you are unsure of the file's encoding, open it in a text editor that reports it.
Refresh fails with a file-not-found error. The path changed or the drive is disconnected. Update the source step or switch to a folder-based import. For files intended for shared or cloud use, store them in OneDrive or SharePoint and point the query at that location.
Dates come in as numbers. The source uses a numeric date format or a different date order than your locale. Change the column type and, if needed, split or transform the text before typing.
Header row is treated as data. The Columns value may be off, or header promotion was skipped. Add Home > Use First Row as Headers after the source step. If that leaves a bogus column name, remove the extra row and fix the count.
If that did not work, rebuild the import from Data > Get Data with a fresh preview, and compare the generated formula against your previous one. If the file is genuinely irregular, import it as plain text and split columns with a delimiter step, which is easier to debug.
Frequently Asked Questions
Do I need to write M code to use Csv.Document?
No. The import dialog creates it. Editing it by hand only becomes useful for encoding, delimiter and column-count problems.
What is the difference between Csv.Document and Lines.FromBinary?
Lines.FromBinary returns each line as raw text with no columns. Csv.Document splits lines into columns using the delimiter and options you specify.
Can I load CSV data from a web address?
Yes, if the address returns a CSV file directly. Use Data > Get Data > From Web and the connector handles the binary content, which can then feed Csv.Document.
Why does refresh break when the file changes?
Usually the automatic type step or a column count. Explicit types and a fixed Columns value make refreshes stable when new rows carry blank or mixed values.
Does Csv.Document work in Power BI and Excel the same way?
The function itself is the same. What differs is where the file can live: the Power BI service and cloud-shared workbooks need a source they can reach, such as SharePoint or OneDrive.
Can I import a file with no header row?
Yes. Skip header promotion and rename the generated Column1, Column2 names yourself. Set Table.PromoteHeaders aside for that query.