Forum Discussion
The 'right' way to bring a custom function requiring arguments 'inline'
- 1 year ago
p45cal , regarding "in-line" function definition and call: consider Function.Invoke as an alternative. Example:
( ) => 5 // this is our function ( ( ) => 5 )( ) // this is how you call it now Function.Invoke( ( ) => 5, { } ) // alternative way to call our functionI am not aware of any other alternatives. And there is nothing wrong with your current approach. But the fact that you apply it to solve that particular problem (the one with different "_" conflicting each other) does not look like a ... reasonable one in that particular case. The problem is solved by different identifiers, not by "in-line function call".
p45cal the problem with your 1st code is with a "context" you are referring to in List.Generate. Try to replace "each" with (argument_name) => like I did it here:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("JcfBDQAgCASwXe5NwglidBbC/msosb9mgkO51GgOgb9EZ6IkMbby/AroSusEqi4=", BinaryEncoding.Base64), Compression.Deflate)),{"Start Date","End Date"}),
ChangedType = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}},"en-GB"),
Custom1 = Table.AddColumn(
ChangedType,
"ListMonths",
(row) => List.Generate(
() => row[Start Date],
(x) => x <= row[End Date],
(x) => Date.AddMonths(x, 1)
)
)
in
Custom1
Now it's clear that Start Date and End Date belong to ChangeType table, not List.Generate function. At the same time I replaced "each" in List.Generate with (x) =>. This function works now because List.Generate knows that "row" belongs to the current row (record) of ChangedType table. I would recommend you to forget about "each" for some time. Use (x) => , (y) => , (w) => - whatever you like to use but not each (aka (_) =>)
p.s. read this article written by Ben Gribaudo about identifier scope