Power Query Value.ReplaceMetadata Function Guide
In this article
The Value.ReplaceMetadata function in Power Query (M language) is used to change the metadata of a value—such as a table, list, or record—without modifying the underlying data itself. This is primarily used to change column names, update table descriptions, or adjust data types in a way that preserves the original value's structure while updating its descriptive properties. To use it, you must be comfortable working within the Advanced Editor or the Formula Bar, as this function is not available as a button in the standard Power Query ribbon interface.
What You'll Need
- Microsoft Excel or Power BI Desktop with Power Query installed.
- An existing data source (Table, CSV, or Database) loaded into the Power Query Editor.
- Access to the Advanced Editor or the Formula Bar to enter M code manually.
- A basic understanding of M language syntax, specifically how records are structured using square brackets [ ].
Step-by-Step
- Open Power Query Editor: Launch Power BI Desktop or Excel. In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
- Identify Your Target Value: Load your data. For this guide, assume you have a step named
"Source"that loads a table. You want to change the metadata of this table (for example, changing the name of a column or the table's internal description) without using the standard "Rename Columns" step, which creates a new step in the Applied Steps list. - Access the Formula Bar: Ensure the Formula Bar is visible (View tab > Formula Bar). If you prefer a full-screen view, go to the Home tab and click Advanced Editor.
- Construct the Function Syntax: The basic syntax for the function is
Value.ReplaceMetadata(value as any, replacementMetadata as record). Thevalueis the table or object you are modifying. ThereplacementMetadatais a record containing the new properties you wish to apply. - Apply a Simple Metadata Change: To change the description of a table, enter the following code in a new custom step:
= Value.ReplaceMetadata(Source, [ Description = "This table contains cleaned sales data for 2023" ])In this example,Sourceis the previous step, and the record[ Description = ... ]updates the metadata. - Modify Column Metadata: Changing column-level metadata is more complex because column information is stored within the table's metadata. To change a column name via metadata, you must target the Columns property. Example: Note: When you replace the Columns metadata, you must provide the full list of columns you want to keep, otherwise, you risk stripping the metadata from other columns.
- Verify the Result: Click the checkmark in the formula bar or hit Enter. You will not see a visual change in the data grid for descriptions, but if you right-click the table or view the properties in a tool that reads M metadata, the change is applied. For column name changes, the header in the preview window will update immediately.
- Chain the Step: If you are using the Advanced Editor, ensure the subsequent step references the name of the step where you applied
Value.ReplaceMetadatato maintain the data flow.
Common Mistakes
One of the most frequent errors is attempting to use Value.ReplaceMetadata to change the actual values inside the cells. This function is strictly for metadata (data about the data). If you need to change a "1" to a "2" in a row, you must use Table.ReplaceValue. Using the metadata function for data manipulation will result in no change to your rows.
Another common mistake is providing an incomplete record when replacing column metadata. Because Value.ReplaceMetadata replaces the metadata record rather than merging it, if you define the Columns property with only one column name, Power Query may lose the metadata (like data types) for all other columns in that table. Always ensure your replacement record accounts for the entire structure of the object you are modifying.
Users also frequently confuse this with Table.RenameColumns. While both can change a column header, Table.RenameColumns is a transformation that creates a new table state, whereas Value.ReplaceMetadata alters the property of the existing value. Using the wrong one can lead to performance differences in very large datasets, though for most users, the difference is negligible.
Tips
To avoid the risk of wiping out other metadata when updating columns, first use Value.Metadata(Source) in a separate step to see exactly what the current metadata record looks like. You can copy this record, modify the specific field you want to change, and then paste that entire record into the Value.ReplaceMetadata function. This ensures that only the target property changes while all others remain intact.
Use this function when building custom M functions. If you are creating a reusable function that processes various tables, Value.ReplaceMetadata allows you to standardize the output metadata (like adding a standard description or a specific internal tag) regardless of the input source's original properties.
Combine this with Value.Metadata to create dynamic metadata. For example, you can create a variable that captures the current date and time, and then use Value.ReplaceMetadata to stamp the table's description with the exact time the data was last refreshed.
Troubleshooting
If you receive a syntax error, the first thing to check is the square brackets. In M, records must be enclosed in [ ]. Ensure you haven't used parentheses ( ) or curly braces { } for the metadata record itself. Curly braces are for lists, and parentheses are for functions.
If the column names do not change as expected, check the case sensitivity. M is case-sensitive; "Columns" is not the same as "columns". If you use the wrong casing for the metadata property name, Power Query will simply add a new, unrecognized metadata field to the object instead of updating the existing column definitions.
If the table suddenly appears empty or throws a "Value.Error," you may have accidentally replaced the value itself rather than its metadata. Ensure the first argument of the function is the reference to the previous step (e.g., Source) and the second argument is the record containing the changes. If you accidentally put the data in the second argument, the function will fail because it expects a record.
If you are unable to see the changes in the Power Query preview window, remember that some metadata (like the Description property) is not visible in the data grid. To verify these changes, you can create a new step using = Value.Metadata(PreviousStepName), which will transform the table into a record showing all current metadata values.
Frequently Asked Questions
Does Value.ReplaceMetadata change the actual data in the rows?
No. It only changes the properties of the object, such as the table name, column names, or descriptions. The actual values within the cells remain untouched.
Is this function better than Table.RenameColumns?
It is not necessarily "better," but it is different. Table.RenameColumns is the standard way to change headers. Value.ReplaceMetadata is used for deeper architectural changes to the object's properties and is more common in advanced M function development.
Can I use this to change the data type of a column?
Yes, by modifying the Type property within the Columns metadata record. However, using Table.TransformColumnTypes is the recommended and more intuitive method for most users.
Why is my metadata not appearing in the Excel spreadsheet?
Most metadata, such as the Description field, is internal to the Power Query engine. It is used for documentation and by other M functions; it does not automatically export as a header or a note into the final Excel worksheet cells.
Can I replace metadata for a List or a Record?
Yes. Value.ReplaceMetadata works on any value type in Power Query, including lists, records, and tables, as long as you provide a valid record for the replacement metadata.