Record.AddField(), Functions And The Delayed Option In M

Today I was looking at the Record.AddField() M function and saw it had a mysterious – and badly documented – optional fourth argument called delayed. Of course I had to work out what it did, and having done so I thought I’d blog about it in case someone found it useful (and however obscure the topic, there’s always someone out there who finds this stuff useful).

Imagine you have an M function called ReturnANumber that has no arguments at all and returns the number 999:

[sourcecode language=”text” padlinenumbers=”true”]
() as number => 999
[/sourcecode]

image

Now imagine that you want to return this function in a record (which is exactly what #shared does). The following expression:

[sourcecode language=”text”]
[MyFunction = ReturnANumber]
[/sourcecode]

…returns a record with one field whose value is of type function:

image

But what if you wanted the field to contain the number that the function returns, not a reference to the function itself? The delayed option of Record.AddField() allows you to do this: if you set it to true, you get the value the function returns.

[sourcecode language=”text”]
Record.AddField([], "MyFunction", ReturnANumber, true)
[/sourcecode]

image

Setting delayed to false does the same as the first example above:

[sourcecode language=”text”]
Record.AddField([], "MyFunction", ReturnANumber, false)
[/sourcecode]

image

Another way to get the same result as setting delayed to true is to use Function.Invoke():

[sourcecode language=”text”]
[MyFunction = Function.Invoke(ReturnANumber,{})]
[/sourcecode]

image

Now I need to think of a real-world use for this…

2 thoughts on “Record.AddField(), Functions And The Delayed Option In M

Leave a Reply to Power BI Desktop and Mobile updates, embedding and some Power Query | Guy in a CubeCancel reply