Record.Field is a Power Query M function that retrieves the value of the specified field in a record. The function returns the value of the field or throws an exception if the field is not found.
Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365
Syntax
Record.Field(
record as record,
field as text,
) as any
Description
Record.Field retrieves the value associated with a specific field
within a record
. Should the specified field not exist within the record, the function will throw an error.
Examples
When working with records in Power Query, you might need to retrieve the value of a specific field. Let’s say you’re working with the following record:
[ Citrus = "Orange", Berry = "Strawberry", StoneFruit = "Peach" ]
Retrieve a Field using Record.Field
To get the value for the field named “Berry”, you can use the Record.Field
function. Here’s how it works:
// Output: "Strawberry"
let
myRecord = [ Citrus = "Orange", Berry = "Strawberry", StoneFruit = "Peach" ],
getValue = Record.Field( myRecord, "Berry" )
in
getValue
In this example, the Record.Field
function looks for the field called “Berry” in myRecord
and returns its value, which is “Strawberry”. This function is very useful when you need to dynamically reference a field name, especially within custom functions.
Alternative Method: Field Selection
There is a more concise way to achieve the same result using field selection. Here’s how you can do it:
// Output: "Strawberry"
let
myRecord = [ Citrus = "Orange", Berry = "Strawberry", StoneFruit = "Peach" ],
getValue = myRecord[Berry]
in
getValue
In this method, you directly access the value of the “Berry” field using myRecord[Berry]
. This approach makes the code shorter and easier to read.
When to Use Each Method
Both methods will give you the same result, but they are suitable for different situations:
- Field Selection: Use this when you are directly working with records and know the field names in advance. It’s cleaner and more straightforward.
- Record.Field Function: Use this when you need to reference field names dynamically. This is common when writing custom functions where field names might not be known ahead of time.
Related functions
Other functions related to Record.Field are:
2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy