Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Value.ReplaceType Function Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Value.ReplaceType function in Power Query (M language) is used to change the type of a value without altering the underlying data itself. This is primarily achieved by replacing the type of an existing value with a new type, which is essential when you need to cast a value to a specific record or table type for the purpose of schema enforcement or when utilizing custom types in advanced function development. To use it, you simply provide the value you wish to modify and the new type you want to assign to it.

What You'll Need

  • Microsoft Excel or Power BI Desktop with Power Query enabled.
  • Access to the Advanced Editor (found under the View tab or Home tab) to write M code.
  • A basic understanding of the M language, specifically how to define types using the type keyword.
  • An existing dataset or a manually created value (such as a record or a list) to test the transformation.

Step-by-Step

  1. Open the Power Query Editor: Launch Power BI Desktop or Excel, go to the Data tab, and select Get Data > Launch Power Query Editor.
  2. Enter the Advanced Editor: On the Home tab, click the Advanced Editor button. This allows you to write the M code directly, as Value.ReplaceType is not available as a button in the ribbon interface.
  3. Define Your Target Type: Before applying the function, you must define what the new type should be. For example, if you want a record to be treated as a specific type with defined fields, create a type definition. Example: MyCustomType = type [ID = Int64.Type, Name = Text.Type].
  4. Identify the Value to Change: Locate the variable or value in your script that currently has a generic type (such as any or a general record) but contains the data structure required by your target type.
  5. Apply the Value.ReplaceType Function: Use the syntax Value.ReplaceType(value, type). For instance, if your record is named SourceRecord and your type is MyCustomType, you would write: ChangedValue = Value.ReplaceType(SourceRecord, MyCustomType).
  6. Verify the Result: Click Done in the Advanced Editor. Look at the formula bar or the preview window. While the data values remain identical, the metadata associated with the object has been updated to reflect the new type.
  7. Integrate into a Table: If you are applying this to a column of records within a table, you will likely need to wrap this function inside a Table.TransformColumns function to apply the type replacement to every row in that specific column.

Common Mistakes

One of the most frequent errors is confusing Value.ReplaceType with Value.Type. While Value.Type is used to inspect the type of a value, Value.ReplaceType is used to change it. Users often try to use the former when they intend to perform the latter, resulting in a script that merely reports the type without modifying it.

Another common mistake is attempting to use this function to convert a data type that is fundamentally incompatible with the target type. For example, trying to replace the type of a simple integer with a complex record type will not magically turn that integer into a record; it simply tells Power Query to treat that value as if it were that type. This can lead to runtime errors when you try to access fields that do not actually exist in the underlying data.

Finally, many users forget that M is case-sensitive. Writing value.replacetype instead of Value.ReplaceType will result in a syntax error, as the engine will not recognize the function call.

Tips

To make your code more maintainable, define your custom types at the top of your M script in a separate let block or as a global variable. This prevents you from having to redefine the type every time you call the function, making the script cleaner and easier to audit.

Use Value.ReplaceType when you are creating custom functions that return records. By explicitly typing the return value, you provide better intellisense and documentation for anyone else using your function, as the expected structure of the output is clearly defined in the metadata.

If you are working with large tables, be mindful of where you place the type replacement in your step sequence. Performing type replacements early in the query can sometimes help the engine optimize subsequent steps, but doing it too often within a loop (like Table.AddColumn) can occasionally impact performance on massive datasets.

Troubleshooting

If the function does not seem to be doing anything, check the preview window. Because Value.ReplaceType changes the metadata and not the value, you won't see the numbers or text change. To verify it worked, you can use the Value.Type function on the result to see if the output matches your target type.

If you encounter an "Expression.Error: We cannot convert the value to type..." message, it usually means you have a mismatch between the actual data structure and the type you are trying to impose. Ensure that if you are replacing a value with a record type, the original value is actually a record. If the original value is null, the function may behave unexpectedly depending on how the target type is defined.

If your query is running slowly after adding this function, try moving the type replacement to the very end of your transformation process. In some cases, forcing a type too early can prevent the Power Query engine from performing "query folding," which is the process of pushing the work back to the original data source (like a SQL server).

Frequently Asked Questions

Does Value.ReplaceType change the actual data?

No, it does not change the data values. It only changes the type information associated with the value. It is a metadata operation, not a data transformation operation.

Is this different from using the Change Type button in the UI?

Yes. The "Change Type" button in the UI typically uses Table.TransformColumnTypes, which converts the actual data (e.g., turning a string "123" into a number 123). Value.ReplaceType is a lower-level M function used for assigning types to values, often used with records and lists.

Can I use this function to convert a List to a Table?

No. To convert a list to a table, you should use Table.FromList. Value.ReplaceType is for changing the type definition, not for restructuring the data object itself.

When should I use Value.ReplaceType instead of Table.TransformColumnTypes?

Use Value.ReplaceType when you are dealing with non-tabular values (like single records or lists) or when you need to enforce a specific custom type schema that the standard UI tools cannot handle.

Does this function work with null values?

Yes, but the result will still be null. The function will assign the new type to the null value, but since null is a special type in M, it may not exhibit the properties of the target type until a non-null value is present.

Can I define a type on the fly inside the function?

Yes, you can pass a type definition directly as the second argument, such as , rather than referencing a previously defined type variable.