Forum Discussion
List.generate syntax error
- 7 years ago
Hi ctrip77 ,
there's no way to make the necessary adjustment in the UI window alone, that pops up when you add a column.
You have to adjust the code that has been generated by your current step like this
= Table.AddColumn(#"Changed Type", "Custom", (x) => List.Generate( () => [ a = x[Date_Start], b = x[Date_End] ], each [a] <= [b], each [ a = Date.AddMonths( [a], x[Interval] ), b = [b] ], each [a] ))
Reason for this is the syntax sugar generated here: You have nested functions who both produce short code and therefore create ambiguity. Therefore you replace the shortcode of the outer function by a proper function definition: (x) => .... where the x stands for the current record that is passed into the function. Then use the x as the record identifier and you can use the square brackets as lookup operators for the record fields.
Check out this series if you want to learn more about M's evaluation context: https://ssbi-blog.de/technical-topics-english/the-environment-concept-in-m-for-power-query-and-power-bi-desktop-part-1/
- 7 years ago
Oh yes, my bad:
Table.AddColumn(#"Changed Type", "Custom", (x) => if x[interval] = 2 then List.Generate( () => [ a = x[Date_Start], b = x[Date_End] ], each [a] <= [b], each [ a = Date.AddMonths( [a], x[Interval] ), b = [b] ], each [a] ) else List.Generate( () => [ a = x[Date_Start], b = x[Date_End] ], each [a] <= [b], each [ a = Date.AddYears( [a], x[Interval] ), b = [b] ], each [a] ) )
Hi MiraAbel ,
not sure I understand the requirement correctly, but please try replacing the 1 by b like so:
= Table.AddColumn(#"Changed Type", "Cash Flow Years", (x) => List.Generate( () =>
[ a = x[Delivery Date], b=0 ],
each [b] <= x[ Years],
each [b = [b]+1 , a = Date.AddYears( [a], b )], each [a] ))
The solution to my question is as follows:
= Table.AddColumn(#"Changed Types", "Dates", (x) => List.Generate( () =>
[ a = Date.AddYears( x[Delivery Date], 1 ), b=0 ],
each [b] <= x[Years],
each [b = [b]+1 , a = Date.AddYears( [a], 1 )], each [a] ))
It answers a bigger question for me that List.Generate in fact can start from a function (formula!) rather than just a starting value (text, or number etc)