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] ) )
thanks for your reply ImkeF !
After change the IF to lowercase, Now instead having the text "list" on the column I see "function" and if I click on the cell, in the bottom pane (where before appear the expanded list) now is showing "function (x as any) as any
I think Im missing something but cant find it!
thanks in advance!
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] )
)