Forum Discussion

Dicken's avatar
Dicken
Post Prodigy
2 months ago
Solved

Rolling accumulation

Hi, I have been trying various methods of a rolling accumultion or bucketiing;  so  buckets of 3  =  {1..10} =  {  {1}, {1,2}, { 1,2,3}, { 2,3,4} , {3,4,5}, {4,5,6}, {5,,6.7}, { 6,7,8} , { 7,8,9}, ...
  • pcoley's avatar
    2 months ago

    Dicken please try with: 

    let
        alist = {1 .. 15},
        window = 3,
        pad = List.Repeat({0}, window - 1),           // {0, 0}
        padded = List.Combine({pad, alist}),          // {0,0,1,2,...,15}
        positions = {0 .. List.Count(alist) - 1},     // 0-based indices for final result
        buckets = List.Transform(
            positions,
            each List.Range(padded, _, window)        // always take exactly 'window' items
        )
    in
        buckets
  • pcoley's avatar
    2 months ago

    Dicken Another option could be with List.Accumulate: 

    let
        alist = {1 .. 15},
        window = 3,
        buckets = List.Accumulate(
            {1 .. List.Count(alist)},
            {},
            (state, current) => 
                state & {
                    if current <= window 
                    then List.Range(alist, 0, current)
                    else List.Range(alist, current - window, window)
                }
        )
    in
        buckets