Csv.Document

Updated on

Csv.Document is a function in the Power Query M language that returns the contents of a CSV document as a table. The function returns a table with the specified number of columns, column names, or options, and handles various delimiters, extraValues, and encodings.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Csv.Document(
   source as any,
   optional columns as any,
   optional delimiter as any,
   optional extraValues as nullable number,
   optional encoding as nullable number,
) as table
ArgumentAttributeDescription
Sourcecontents of a CSV document
ColumnsOptionalcan be null, the number of columns, a list of column names, a table type, or an options record. When specifying a record (and omitting other optional arguments) you can provide the following fields: Delimiter, Columns, Encoding, CsvStyle and Quotestyle.
DelimiterOptionalcan be a single character, a list of characters, or the value "", which indicates rows should be split by consecutive whitespace characters. Default: ",".
ExtraValuesOptionalRefers to the ExtraValues.Type to specify the expected action for extra values in a row that has less columns than expected. You can choose from ExtraValues.Error, ExtraValues.Ignore or ExtraValues.List.
EncodingOptionalSpecifies the TextEncoding.Type. Default: 65001 (UTF-8).
You can choose from: TextEncoding.Unicode, TextEncoding.Utf16, TextEncoding.BigEndianUnicode, TextEncoding.Windows, TextEncoding.Ascii and TextEncoding.Utf8.

Description

Returns the contents of the CSV document as a table. If a record is specified for columns (and delimiterextraValues, and encoding are null), the following record fields may be provided:

  • Delimiter: The column delimiter. Default: ",".
  • Columns: Can be null, the number of columns, a list of column names, or a table type. If the number of columns is lower than the number found in the input, the additional columns will be ignored. If the number of columns is higher than the number found in the input, the additional columns will be null. When not specified, the number of columns will be determined by what is found in the input.
  • Encoding: The text encoding of the file. Default: 65001 (UTF-8).
  • CsvStyle: Specifies how quotes are handled.
  • QuoteStyle: Specifies how quoted line breaks are handled.
    • QuoteStyle.Csv (default): Quoted line breaks are treated as part of the data, not as the end of the current row.
    • QuoteStyle.None: All line breaks are treated as the end of the current row, even when they occur inside a quoted value.

Examples

Process CSV text with column headers

let
    csv = Text.Combine( {"OrderID,Item", "1,Fishing rod", "2,1 lb. worms"}, "#( cr )#( lf )" )
in
    Table.PromoteHeaders( Csv.Document( csv ) )

 /* Output: 
Table.FromRecords( {
    [OrderID = "1", Item = "Fishing rod"],
    [OrderID = "2", Item = "1 lb. worms"]
} )
 */ 

Process CSV text with multiple delimiter characters. In this example, the third parameter specifies the delimiter pattern #|# to use instead of the default.

let
    csv = Text.Combine({"OrderID#|#Color", "1#|#Red", "2#|#Blue"}, "#(cr)#(lf)")
in
    Table.PromoteHeaders(Csv.Document(csv, null, "#|#"))

/* Output:
Table.FromRecords({
    [OrderID = "1", Color = "Red"],
    [OrderID = "2", Color = "Blue"]
} ) 
*/

Learn more about Csv.Document in the following articles:

Other functions related to Csv.Document are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/csv-document