Record.Combine

Updated on

Record.Combine is a Power Query M function that combines the records in the given records. The function returns a single record that contains all the fields from the input records, or an error if any input value is not a record.

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

Syntax

Record.Combine( records as list ) as record

Description

Record.Combine merges multiple records from a list into a single record. It throws an error when you include non-record items within the list, as the function expects a consistent record format for the operation.

Examples

Combining records in Power Query M can be done in different ways. Below are some examples demonstrating how to use the Record.Combine function and the & operator to achieve this.

Using Record.Combine to Create a Combined Record

The Record.Combine function is useful for merging multiple records into a single record.

// Output: [ Name = "Rick", Nationality = "Dutch",  Height = 1.91 ]
let
  Record1 = [ Name = "Rick", Nationality = "Dutch" ],
  Record2 = [ Height = 1.91 ],
  CombinedRecord = Record.Combine( { Record1, Record2 } )
in
  CombinedRecord

Manually Combining Records with the & Operator

You can also combine records manually using the & operator, which can be more concise for simple merges.

// Output: [ Name = "Rick", Nationality = "Dutch",  Height = 1.91 ]
let
  Record1 = [ Name = "Rick", Nationality = "Dutch" ],
  Record2 = [ Height = 1.91 ],
  CombinedRecord = Record1 & Record2
in
  CombinedRecord

This combines the records in the same way as the previous example but with more concise code.

Handling Duplicate Field Names

When combining two records, if one of the field names is identical, the field from the last record overwrites the field value from the first record.

// Output: [ Name = "Danny", Nationality = "Dutch",  Height = 1.91 ]
let
  Record1 = [ Name = "Rick", Nationality = "Dutch" ],
  Record2 = [ Name = "Danny" ],
  CombinedRecord = Record.Combine( { Record1, Record2 } )
in
  CombinedRecord

In this example, the name “Rick” is replaced with “Danny”.

Ensuring Valid Records in Record.Combine

When using Record.Combine with a list of values, ensure each value is a valid record. Providing any other type will result in an error.

// Output: "[Expression.Error] We cannot convert the value 20 to type Record."
let
  Record1 = [ Name = "Rick" ],
  Record2 = [ Name = "Danny" ],
  CombinedRecord = Record.Combine( { Record1, Record2, null } )
in
  CombinedRecord

Providing a non-record value, such as null, will cause an error in Record.Combine. Make sure all elements in the list are valid records to avoid this issue.

Other functions related to Record.Combine are:

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

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy