Power Query Value.ViewFunction: How to Use It
In this article
Value.ViewFunction is a Power Query M function that returns a view of a function value, letting you inspect its parameters and invoke it with a record of arguments. You use it when you have a function stored as a value (for example, from a table column, a custom function, or a function returned by another M function) and you want to call it dynamically or examine its signature. It does not create a new function; it wraps the existing one so you can pass arguments as a record instead of positional parameters. This is useful in advanced M code, custom connectors, and scenarios where functions are data. You need Power Query in Excel or Power BI Desktop, and basic familiarity with the M language and the Advanced Editor.
What You'll Need
- Power Query Editor in Excel (Data > Get & Transform Data) or Power BI Desktop (Transform Data).
- Access to the Advanced Editor (Home > Advanced Editor) to write M code.
- A basic understanding of M functions, records, and the difference between a function value and a function invocation.
- A function value to work with — either a custom function you created, a built-in function referenced as a value, or a function stored in a table cell.
Menu labels can differ slightly by version, device, or region. The steps below use the Power Query Editor in Excel and Power BI Desktop; the M code is the same in both.
Step-by-Step
- Open the Advanced Editor. In Power Query Editor, go to Home > Advanced Editor. This is where you will write or paste M code. If you are new to M, start with a simple query that loads a table so you have a base to modify.
- Create or obtain a function value. You can define a custom function in M, for example:
MyFunction = (x as number, y as number) as number => x + yAlternatively, reference a built-in function as a value by using its name without parentheses, such asNumber.Abs. You can also have a function stored in a table column; in that case, the cell value is a function. - Wrap the function with Value.ViewFunction. The syntax is
Value.ViewFunction(function as function, optional options as nullable record) as function. In practice, you call it like this:ViewedFunction = Value.ViewFunction(MyFunction)The result is a new function value that behaves like the original but accepts a single record argument containing the parameter names and values. - Invoke the viewed function with a record. Instead of calling MyFunction(3, 4) , you call the viewed function with a record: The record field names must match the parameter names of the original function. If the original function has optional parameters, you can omit them from the record.
- Inspect the function signature (optional). You can use
Value.ViewFunctiontogether withType.FunctionParametersandType.FunctionReturnto examine the function's type. For example:FunctionType = Value.Type(MyFunction)Parameters = Type.FunctionParameters(FunctionType)This returns a record describing each parameter's name and type. This is helpful when you need to build a generic invocation wrapper. - Use it in a dynamic scenario. A common pattern is to store functions in a table and invoke them by row. For example, if you have a table with a column
Functioncontaining function values and a columnArgumentscontaining records, you can add a custom column with:Value.ViewFunction([Function])([Arguments])This calls each function with its corresponding arguments. Ensure the record field names match the function's parameter names. - Test and handle errors. Because dynamic invocation can fail if the record does not match the function's parameters, wrap the call in try ... otherwise to provide a fallback. For example: This prevents the entire query from failing when one row has mismatched arguments.
- Close and apply. Once your M code is correct, click Done in the Advanced Editor, then Close & Load (Excel) or Close & Apply (Power BI) to load the results.
Common Mistakes
- Passing positional arguments to the viewed function. The viewed function expects a single record, not separate arguments. Calling
ViewedFunction(3, 4)will cause an error. - Mismatched record field names. The record keys must exactly match the parameter names of the original function, including case. If the function parameter is X and you pass , it may fail depending on the function's definition.
- Forgetting that Value.ViewFunction returns a function, not a result. You must invoke the returned function with a record to get a value.
- Using it on something that is not a function. If the value is not of type function, you will get an error. Check with
Value.TypeorValue.Isfirst. - Assuming it changes the function's behavior. It only provides a view; the underlying function still enforces its own parameter types and optionality.
Tips
- Use
Value.ViewFunctionwhen you need a uniform way to call functions with a record, such as in generic wrappers or when functions are stored as data. - Combine it with
Type.FunctionParametersto build a record of default arguments dynamically. - If you only need to call a function normally, you do not need
Value.ViewFunction; use direct invocation for simplicity and better performance. - Remember that optional parameters can be omitted from the record; required parameters must be present.
- Use
tryto handle errors gracefully in dynamic scenarios, especially when processing many rows.
Troubleshooting
If you get an error like "We cannot convert a value of type Function to type Record" or "The field 'x' of the record wasn't found," check that you are passing a record with the correct field names. If the error says "We cannot apply operator & to types," you may be trying to concatenate a function value; ensure you are invoking it first. If the function expects a table or other complex type, make sure the record field values are of the correct type. When using Value.ViewFunction on a built-in function, confirm the function name is correct and that it is a function value, not a function invocation. If the query is slow, consider whether dynamic invocation is necessary; direct calls are faster. If the Advanced Editor shows a syntax error, check for missing commas, parentheses, or brackets. Always test with a small dataset before applying to large queries.
Frequently Asked Questions
What is the difference between Value.ViewFunction and calling a function directly?
Direct calling uses positional arguments, like MyFunction(3, 4) . Value.ViewFunction returns a function that takes a single record argument, like . The latter is useful when you need to pass arguments dynamically as a record.
Can I use Value.ViewFunction with built-in functions?
Yes, as long as you reference the built-in function as a value (without parentheses). For example, Value.ViewFunction(Number.Abs) works, and you can invoke it with if the parameter name is number . Check the function's signature to know the parameter names.
Does Value.ViewFunction work in both Excel and Power BI?
Yes, it is part of the M language and available in Power Query in Excel and Power BI Desktop. The behavior is the same, though the surrounding UI may differ slightly.
What happens if I omit an optional parameter in the record?
The function uses its default value for that parameter, if defined. If the parameter is required and omitted, you will get an error.
Can I use Value.ViewFunction to create a function that accepts a variable number of arguments?
No, Value.ViewFunction does not change the function's arity. It only provides a record-based calling convention. The original function still defines how many parameters it accepts.
Is Value.ViewFunction available in all Power Query versions?
It has been part of the M language for a long time, but very old versions may not include it. If you are using a current version of Excel or Power BI Desktop, it should be available. If not, update your software.