Forum Discussion

Dicken's avatar
Dicken
Icon for Post Prodigy rankPost Prodigy
9 months ago
Solved

Adjusting positions in a list

Hi, can anyone come up with a way to adjust posiotns of change in a list, so they can be used with  List.accumulate to insert rows,     i have use List.TransformMany,  ; so to insert at each chaan...
  • Zanqueta's avatar
    9 months ago

    Hi Dicken,

     

    Your logic is correct: to use List.Accumulate for inserting rows, you need to calculate the adjusted positions, taking into account the shifts caused by previous insertions. In your current approach, List.PositionOf returns the original positions without considering these cumulative shifts.

    Please try stwitch you code for these:

    let
        alist = {"a", "a", "a", "b", "b", "b", "b", "c", "c", "d", "d", "d", "e"},
        insertN = 1,
        result = List.Generate(
            () => [i = 0, prev = alist{0}, acc = {}],
            each [i] < List.Count(alist),
            each
                if [i] = 0 then
                    [i = [i] + 1, prev = alist{0}, acc = {[prev]}]
                else
                    if alist{[i]} <> [prev] then
                        [i = [i] + 1, prev = alist{[i]}, acc = List.Combine({[acc], List.Repeat({null}, insertN), {alist{[i]}}})]
                    else
                        [i = [i] + 1, prev = [prev], acc = [acc] & {alist{[i]}}],
            each [acc]
        ),
        rslt = List.Last(result)
    in
        rslt

     

    I testet, let me know if thats it are you expectig for:

     

     

    If this response was helpful in any way, I’d gladly accept a 👍much like the joy of seeing a DAX measure work first time without needing another FILTER.

    Please mark it as the correct solution. It helps other community members find their way faster (and saves them from another endless loop 🌀.