Data Sources

M CSV Source

Loading CSV Data

M CSV source uses Csv.Document for delimited files.

Introduction to M CSV Source

The M CSV source is a powerful tool for handling delimited text files within the Power Query M language. It is primarily used to import and manipulate CSV (Comma-Separated Values) files within data solutions. The core function behind this capability is Csv.Document, which parses the CSV data into a structured table format.

Understanding Csv.Document Function

The Csv.Document function is used to transform a CSV file into a table that can be easily queried and manipulated. This function provides multiple parameters to customize its behavior, allowing you to handle different file structures and delimiters.

  • source: The binary data representing the CSV file content.
  • csvOptions (optional): A record that contains various settings such as delimiter, encoding, and more.

Basic Example: Reading a CSV File

To read a simple CSV file, you can use the Csv.Document function with basic parameters. Here's a straightforward example:

In this example, File.Contents reads the file from the specified path, and Csv.Document converts it into a table using a comma as the delimiter and Windows-1252 as the encoding.

Handling Different Delimiters

CSV files might not always use commas as delimiters. Sometimes, semicolons or tabs are used. The Csv.Document function allows you to specify a different delimiter:

This example demonstrates how to handle a CSV file that uses semicolons as delimiters.

Advanced Options: Csv.Document

Beyond basic delimiter and encoding settings, Csv.Document offers advanced options for more complex scenarios:

  • QuoteStyle: Defines how quotation marks are used in the CSV file.
  • HasHeaders: Indicates whether the first row should be treated as headers.
  • Culture: Specifies the culture to be used for formatting.

This example uses several options to parse a CSV file with headers and specific culture settings, demonstrating the flexibility of the Csv.Document function.