TOCSV Function in DAX

The TOCSV() function in DAX (Data Analysis Expressions) is used to convert a table or table expression into a single string of comma-separated values (CSV). It's useful when you want to display or export table data as a string, often for tooltips, labels, or exports.

DAX Syntax

TOCSV(Table, [MaxRows], [Delimiter], [IncludeHeaders])

The function has the following parameters:

Return value

A string with CSV representation of the table.

Example: Lets we the following table named ‘Student List’.

TOCSV DAX Function in Power BI

Let’s create a measure with name “TOCSV measure”.

DAX

TOCSV Measure = TOCSV('Student List')    

Let’s use this measure in the card visual as shown in the image below:

TOCSV DAX Function in Power BI

DAX

TOCSV Measure = TOCSV(
  SELECTCOLUMNS(
      'Student List', 
      "Student", 'Student List'[StudentName], 
      "Teacher", 'Student List'[AssociatedTeacher]
  )
)        

The output of the above code is shown below:

TOCSV DAX Function in Power BI

Example: With Custom Delimiter and No Headers.

DAX

TOCSV Measure = TOCSV(
  SELECTCOLUMNS(
      'Student List', 
      "Student", 'Student List'[StudentName], 
      "Teacher", 'Student List'[AssociatedTeacher]
  ), 20, "|", FALSE()
)        

The output of the above code is shown below:

TOCSV DAX Function in Power BI

Example:

DAX

TOCSV Measure = TOCSV(
  TOPN(3, 'Student List', 'Student List'[ID], DESC),
  3, ", ",
  TRUE
)        

The output of the above code is shown below:

TOCSV DAX Function in Power BI

We can also export the data from the visual, by clicking on three dots, and then click on Export data.

TOCSV DAX Function in Power BI

The file is downloaded as a csv file.

TOCSV DAX Function in Power BI

Note: As it is a measure the table used in the function is also filtered by the filter context.

Use Cases: