Forum Discussion

ctrip77's avatar
ctrip77
Frequent Visitor
7 years ago
Solved

List.generate syntax error

Hi, I have a table with 4 columns Date_start, Date_End, Interval. I add a custom column with the following code: List.Generate( () => [ a = [Date_Start], b = [Date_End] ], each [a] <= [b], each [...
  • ImkeF's avatar
    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/

     

     

     

     

  • ImkeF's avatar
    ImkeF
    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] )
    
    )