Csv.Document

D

T

The M Code Behind the Power Query M function Csv.Document

In this article, we will dive into the M code behind the Csv.Document function. We will explore how it works, what options it has, and how you can use it to optimize your data transformation workflows.

What is Csv.Document?

Csv.Document is a Power Query M function that reads a CSV file and returns a table. It takes a file path or a binary as input and produces a table as output. The table contains the data in the CSV file, with the first row as the header and the rest of the rows as data.

The Csv.Document function has a number of options that allow you to customize the way it reads the CSV file. These options include:

– Delimiter: Specifies the delimiter used in the CSV file. By default, Csv.Document uses a comma as the delimiter, but you can change it to any character you want.

– Encoding: Specifies the encoding used in the CSV file. By default, Csv.Document uses UTF-8 encoding, but you can change it to any encoding supported by Power Query.

– Header: Specifies whether the CSV file has a header row or not. By default, Csv.Document assumes that the CSV file has a header row.

– QuoteStyle: Specifies the quote style used in the CSV file. By default, Csv.Document assumes that the CSV file uses double quotes to enclose field values.

– Culture: Specifies the culture used to parse numeric and date/time values in the CSV file. By default, Csv.Document uses the current culture.

How does Csv.Document work?

Let’s take a closer look at how Csv.Document works. Here is an example of how to use Csv.Document to read a CSV file:


let

Source = Csv.Document(File.Contents(“C:UsersJohnDoeDocumentsdata.csv”), [Delimiter=”,”, Encoding=1252, QuoteStyle=QuoteStyle.None, Header=1]),

#”Changed Type” = Table.TransformColumnTypes(Source,{{“Column1”, type text}, {“Column2”, Int64.Type}})

in

#”Changed Type”


In this example, Csv.Document reads a CSV file located at "C:UsersJohnDoeDocumentsdata.csv". The function uses a comma as the delimiter, Windows-1252 encoding, no quotes to enclose field values, and assumes that the CSV file has a header row.

The output of Csv.Document is then transformed using Table.TransformColumnTypes. This function converts the "Column2" column to Int64 type.

Tips for optimizing Csv.Document performance

Csv.Document can be slow when reading large CSV files. Here are some tips for optimizing its performance:

- Limit the number of columns: Csv.Document has to read the entire CSV file to determine the number of columns. This can be slow for large files with many columns. If possible, limit the number of columns in your CSV file.

- Specify the data types: Csv.Document has to guess the data types of the columns based on the values in the file. This can be slow for large files with many rows. If possible, specify the data types of the columns in your CSV file.

- Use binary format: Reading a binary file is faster than reading a text file. If possible, convert your CSV file to binary format before reading it with Csv.Document.

Csv.Document is a powerful function in Power Query that allows users to read CSV files and transform them into tables. By understanding the M code behind Csv.Document and its options, you can optimize your data transformation workflows and improve performance.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)