none
List.Generate function or {[A] .. [A] + 4 } RRS feed

  • Question


  • I want to create sequentially increasing list of 5 values for each record in my table.

    Example of desired results

    Value    List of 5 Values

    1          1,2,3,4,5

    2          2,3,4,5,6

    3          3,4,5,6,7

    etc


    I am successfully using {[A] .. [A] + 4 } to generate desired list of numbers.

    But so far haven't been able to do same thing with List.Generate function  The two examples given in documentation are either too 'easy' or too 'complex' to give more specific guidance.

    https://msdn.microsoft.com/en-us/library/mt260751.aspx?f=255&MSPPError=-2147217396

    List.Generate(()=>0, each _ < 10, each _ + 1) equals { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
    List.Generate(()=>[A=1,B=1], each [A] < 5, each [A=[A]+1, B=[B]+1], each [A]+[B]) equals { 2, 4, 5, 8 }

    How can I do equivalent of  {[A] .. [A] + 4 }  with List.Generate?







    Friday, June 24, 2016 6:37 PM

Answers

  • I'm not sure why you'd want to use List.Generate, but it could look like this:

    List.Generate(() => [Value=4, Counter=0], (state) => state[Counter] < 5, (state) => [Value=state[Value]+1, Counter=state[Counter]+1], (state) => state[Value])

    You can also use List.Numbers to generate a sequence of numbers.

    • Proposed as answer by Curt Hagenlocher Friday, June 24, 2016 7:35 PM
    • Marked as answer by sitrucp Friday, June 24, 2016 8:28 PM
    Friday, June 24, 2016 7:35 PM

All replies

  • I'm not sure why you'd want to use List.Generate, but it could look like this:

    List.Generate(() => [Value=4, Counter=0], (state) => state[Counter] < 5, (state) => [Value=state[Value]+1, Counter=state[Counter]+1], (state) => state[Value])

    You can also use List.Numbers to generate a sequence of numbers.

    • Proposed as answer by Curt Hagenlocher Friday, June 24, 2016 7:35 PM
    • Marked as answer by sitrucp Friday, June 24, 2016 8:28 PM
    Friday, June 24, 2016 7:35 PM
  • That is pretty complex with List.Generate  Now I am not sure why I would want to use it either : )

    I realized List.Numbers between posting this and now, it is much better suited to task:

    = Table.AddColumn(Source, "day_list", each List.Numbers([DayOfYear], 5))


    Friday, June 24, 2016 8:28 PM
  • can this code work?

    = List.Generate(()=>[A=1,B=2,C=3,D=4], each [A] < 5, each [A=[A]+1, B=[B]+1,C=[C]+1,D=[D]+1])

    you can get the list below

    A B C D

    1 2 3 4

    2 3 4 5

    3 4 5 6

    ....



    • Edited by 张文洲 Tuesday, June 28, 2016 8:35 AM
    Tuesday, June 28, 2016 8:34 AM