Record.FromList

Updated on

Record.FromList is a Power Query M function that creates a record from a list of field values and a set of fields, either by a list of text values or a record type. The function returns a new record with the specified fields and values, or throws an error if the fields are not unique.

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

Syntax

Record.FromList(
   list as list,
   fields as any,
) as record

Description

The Record.FromList function allows you to create a record from a list of field values and a corresponding set of fields. These fields can be specified either by a list of text values or a record type. However, it’s important to note that it will throw an error if the fields are not unique. This uniqueness is determined by an ordinal comparison, meaning it is case-sensitive.

Examples

Let’s explore some practical examples to better understand the Record.FromList function:

Creating a Record from Field Values and Field Names

Consider a scenario where you have a list of field values and a corresponding list of field names. You can use the Record.FromList function to create a record:

// Output: [ CustomerID = 55545, Name = "Rick", Phone = "06-446699" ]

Record.FromList( 
  { 55545, "Rick", "06-446699" }, 
  { "CustomerID", "Name", "Phone" } 
)

This operation creates a record with the specified fields and values.

Creating a Record from Field Values and a Record Type

The Record.FromList function also allows you to specify the data type along with the column names of the different values. This can be done using a record type:

// Output: [ CustomerID = 55545, Name = "Rick", Phone = "06-446699" ]

Record.FromList( 
  { 55545, "Rick", "06-446699" }, 
  type [ CustomerID = number, Name = text, Phone = number ] 
)

This operation creates a record with the specified fields, values, and data types.

Case-Sensitive Field Names

Power Query uses an ordinal comparison (case-sensitive) to check the uniqueness of field names. This means that ‘Phone’ and ‘phone’ are considered as two different fields:

// Output: [ CustomerID = 55545, Name = "Rick", Phone = "06-446699" , phone = "123-456"]

Record.FromList( 
  { 55545, "Rick", "06-446699", "123-456" }, 
  type [ CustomerID = number, Name = text, Phone = number , phone = number ] 
)

However, if you try to use the same case for a field name more than once, Power Query will throw an error:

// Output: Expression.Error: The field 'Phone' already exists in the record.
Record.FromList( 
  { 55545, "Rick", "06-446699", "123-456" }, 
  type [ CustomerID = number, Name = text, Phone = number , Phone = number ] 
)

This returns the expression error: The field ‘Phone’ already exists in the record. This example illustrates the importance of case sensitivity in field names when using the Record.FromList function.

Other functions related to Record.FromList are:

BI Gorilla Blog

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