Record.RenameFields

D

T

The M Code Behind the Power Query M function Record.RenameFields

What is a Record in Power Query?

In Power Query, a record is a data type that contains one or more fields. Each field in a record has a name and a value. For example, consider the following record:


[CustomerID = 1, FirstName = “John”, LastName = “Doe”, Age = 30]


This record contains four fields: CustomerID, FirstName, LastName, and Age. The value of the CustomerID field is 1, the value of the FirstName field is "John", the value of the LastName field is "Doe", and the value of the Age field is 30.

What is the Record.RenameFields Function?

The Record.RenameFields function is used to rename one or more fields in a record. The syntax of the function is as follows:


Record.RenameFields(record as record, renames as list, optional missingField as nullable any) as record


The function takes three arguments:

- record: The record that contains the fields to be renamed.

- renames: A list of pairs that specify the old and new names of the fields to be renamed.

- missingField: (Optional) The value to return if a renamed field is missing from the input record. By default, the function returns an error if a renamed field is missing.

How to Use the Record.RenameFields Function

To use the Record.RenameFields function, you first need to create a record that contains the fields to be renamed. You can create a record using the Record.FromList or Record.FromFields function. For example, consider the following record:


[CustomerID = 1, FirstName = “John”, LastName = “Doe”, Age = 30]


Suppose you want to rename the FirstName and LastName fields to First and Last respectively. You can use the Record.RenameFields function as follows:


Record.RenameFields([CustomerID = 1, FirstName = “John”, LastName = “Doe”, Age = 30], {[FirstName = “First”, LastName = “Last”]})


The output of this function will be the following record:


[CustomerID = 1, First = “John”, Last = “Doe”, Age = 30]


The FirstName and LastName fields have been renamed to First and Last respectively.

You can also use the Record.RenameFields function to rename multiple fields at once. For example, suppose you want to rename the FirstName, LastName, and Age fields to First, Last, and Years respectively. You can use the function as follows:


Record.RenameFields(

[CustomerID = 1, FirstName = “John”, LastName = “Doe”, Age = 30],

{[FirstName = “First”, LastName = “Last”, Age = “Years”]}

)


The output of this function will be the following record:


[CustomerID = 1, First = “John”, Last = “Doe”, Years = 30]


The FirstName, LastName, and Age fields have been renamed to First, Last, and Years respectively.

The Record.RenameFields function is a powerful tool that allows users to rename one or more fields in a record in Power Query. It is a simple function that takes a record and a list of pairs that specify the old and new names of the fields to be renamed. By using this function, users can easily transform and shape their data in different ways to meet their specific needs.

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)