Smart Systems for Home

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

Power Query Value.Traits Function: Usage and Guide

· 5 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.Traits function in Power Query (M) is used to retrieve a record of metadata and intrinsic properties—known as traits—associated with a specific value or data type. It allows developers to programmatically determine how a value behaves, its type, and whether it supports specific operations, which is essential for building dynamic functions that must handle varying data types without crashing.

What You'll Need

  • Microsoft Excel (with Power Query installed) or Power BI Desktop.
  • Access to the Advanced Editor to write M code, as Value.Traits cannot be invoked via the standard ribbon interface.
  • A basic understanding of M records and types.

Step-by-Step

  1. Open the Power Query Editor: 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.
  2. Create a Blank Query: Right-click in the Queries pane on the left and select New Query > Other Sources > Blank Query. This provides a clean slate for writing M code.
  3. Open the Advanced Editor: On the Home tab, click the Advanced Editor button. This opens the window where you can manually enter the M formula.
  4. Define the Input Value: Start by defining a value you want to analyze. For example, create a variable for a number or a text string.
    let MyValue = 123,
  5. Apply the Value.Traits Function: Use the Value.Traits function, passing your variable as the argument. This function returns a record containing the traits of that value.
    Traits = Value.Traits(MyValue)
  6. Access Specific Traits: Because Value.Traits returns a record, you must use the square bracket notation [ ] to access specific properties. Common traits include Type or IsNumeric (depending on the value's nature).
    Result = Traits[Type] in Result
  7. Test with Different Types: To see the function's utility, change MyValue = 123 to MyValue = "Hello" or MyValue = {1, 2, 3}. You will notice that the record returned by Value.Traits changes based on the data type of the input.
  8. Implement in a Custom Function: To make this practical, wrap the logic in a function. This allows you to pass any column value into the trait checker to determine how to process it.
    (inputValue) => Value.Traits(inputValue)[Type]

Common Mistakes

One of the most frequent errors is attempting to use Value.Traits directly in the formula bar without understanding that it returns a record, not a single value. If you simply enter = Value.Traits(10), Power Query will display a record object. If you then try to use that record in a calculation or a text concatenation, you will receive a type mismatch error. You must always specify which trait you are looking for using the [TraitName] syntax.

Another common mistake is confusing Value.Traits with Value.Type. While Value.Type tells you the data type (e.g., type number), Value.Traits provides a broader set of properties that describe the capabilities of that value. Using Value.Type when you actually need to know if a value is a list or a record can lead to overly complex conditional logic that Value.Traits could have solved more elegantly.

Users also frequently forget that Value.Traits is case-sensitive. M is a case-sensitive language; therefore, attempting to access Traits[type] instead of Traits[Type] will result in an error stating that the field was not found in the record.

Tips

To explore all available traits for a specific data type, use the Record.FieldNames function on the result of Value.Traits. For example, Record.FieldNames(Value.Traits(123)) will return a list of every trait available for numeric values. This is the fastest way to discover hidden properties without relying on incomplete documentation.

When building complex data pipelines, combine Value.Traits with try...otherwise blocks. Since not all values have the same traits (a text value won't have the same traits as a date value), attempting to access a trait that doesn't exist for that specific type will throw an error. Using try Value.Traits(x)[SomeTrait] otherwise null ensures your query continues to run even when encountering unexpected data types.

Use Value.Traits to build "Type-Agnostic" transformations. If you are creating a function that needs to trim whitespace from a value, but the value might be a number or a null, you can use Value.Traits to verify if the value is actually text before applying the Text.Trim function, preventing the query from failing on non-text rows.

Troubleshooting

If you encounter a "Field not found" error, the first step is to verify the data type of the value you are passing into the function. Because Value.Traits returns different records for different types, you cannot assume a trait exists across all inputs. Use a Value.Type check first, or use the Record.HasFields function to check if the trait exists before attempting to access it.

If the function returns an empty record or unexpected results, check if the value is wrapped in a list or a table. Value.Traits operates on a single value. If you pass it a Table object, it will return the traits of the Table itself, not the traits of the data inside the table. To analyze the data within a table, you must use Table.AddColumn to apply Value.Traits to each individual cell in a row.

If you are experiencing performance slowdowns, avoid calling Value.Traits inside a loop or a highly repetitive custom function across millions of rows. While powerful, retrieving metadata for every single cell can add significant overhead to the query evaluation. Instead, try to determine the trait for a sample of the data or use it only on columns where the data type is known to be inconsistent.

Frequently Asked Questions

How is Value.Traits different from Value.Type?

Value.Type returns the specific type object (like type text), whereas Value.Traits returns a record containing various properties and characteristics of that value, which may include its type among other metadata.

Can I use Value.Traits in the standard UI?

No, there is no button or menu option for Value.Traits. It must be written manually in the formula bar or the Advanced Editor.

Does Value.Traits work on null values?

Yes, but the traits returned for a null value will differ significantly from those of a defined type, as null has its own intrinsic properties in M.

Can I use this to find the length of a string?

No. Value.Traits provides metadata about the type, not the content of the value. To find the length of a string, use Text.Length.

Is Value.Traits available in all versions of Power BI?

Yes, it is a core part of the Power Query M language and is available in all modern versions of Power BI Desktop and Excel Power Query.

What happens if I pass a record into Value.Traits?

The function will return the traits associated with the Record type, allowing you to programmatically determine how to interact with that record.