Table.CombineColumnsToRecord

D

T

The M Code Behind the Power Query M function Table.CombineColumnsToRecord

Understanding the Table.CombineColumnsToRecord Function

The Table.CombineColumnsToRecord function is used to combine multiple columns in a table into a single record. The resulting record is created by combining the values of the selected columns, with the column names used as the record field names. Here’s the basic syntax of the function:


Table.CombineColumnsToRecord(table as table, columnNames as list, newColumnName as text)


The function takes three arguments:

– table: The table containing the columns to be combined.

– columnNames: A list of column names to combine.

– newColumnName: The name of the new column to create.

Here’s an example of how to use the function:


let

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcslNzEtVslIwMjIwVdQyKMlMzXWz0vz8xNzyzLBwA=", BinaryEncoding.Base64)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t])),

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}}),

#"Combined Columns" = Table.CombineColumnsToRecord(#"Changed Type",{"Column1", "Column2", "Column3"}, "Combined")

in

#"Combined Columns"


In this example, we start with a table containing three columns: Column1, Column2, and Column3. We use the Table.CombineColumnsToRecord function to combine these columns into a new column called Combined.

Examples of Using the Table.CombineColumnsToRecord Function

Example 1: Combining Columns with a Delimiter

Let’s say we have a table containing first names and last names, and we want to combine these columns into a single column with a delimiter between them. We can use the Table.CombineColumnsToRecord function with a custom delimiter to achieve this task. Here’s how:


let

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcslNzEtVslIwMjIwVdQyKMlMzXWz0vz8xNzyzLBwA=", BinaryEncoding.Base64)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t])),

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

#"Combined Columns" = Table.CombineColumnsToRecord(#"Changed Type",{"Column1", "Column2"}, "Name"),

#"Added Custom Delimiter" = Table.AddColumn(#"Combined Columns", "Full Name", each [Name.Column1] & ", " & [Name.Column2])

in

#"Added Custom Delimiter"


In this example, we start with a table containing two columns: Column1 (first names) and Column2 (last names). We use the Table.CombineColumnsToRecord function to combine these columns into a new column called Name. We then add a custom delimiter (“, “) between the first and last names using the Table.AddColumn function.

Example 2: Combining Columns with a Space

Let’s say we have a table containing a person’s first name, middle initial, and last name, and we want to combine these columns into a single column with a space between them. We can use the Table.CombineColumnsToRecord function with a space delimiter to achieve this task. Here’s how:


let

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcslNzEtVslIwMjIwVdQyKMlMzXWz0vz8xNzyzLBwA=", BinaryEncoding.Base64)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t])),

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}}),

#"Combined Columns" = Table.CombineColumnsToRecord(#"Changed Type",{"Column1", "Column2", "Column3"}, "Name"),

#"Added Spaces" = Table.AddColumn(#"Combined Columns", "Full Name", each [Name.Column1] & " " & [Name.Column2] & " " & [Name.Column3])

in

#"Added Spaces"


In this example, we start with a table containing three columns: Column1 (first names), Column2 (middle initials), and Column3 (last names). We use the Table.CombineColumnsToRecord function to combine these columns into a new column called Name. We then add spaces between the first name, middle initial, and last name using the Table.AddColumn function.

The Table.CombineColumnsToRecord function is a powerful tool for combining multiple columns into a single record in Power Query. By understanding the M code behind this function and its syntax, you can easily create new columns that consolidate data and simplify your workflow. With examples of how to use this function in different scenarios, you can start using it right away to streamline your data analysis.

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)