Forum Discussion
Adjusting positions in a list
- 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 rsltI 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 🌀.
- 9 months ago
Don't forget the Fifth Element!
Table.Group: Exploring the 5th element in Power BI and Power Query –
That can provide a much more elegant solution.
obligatory shout-out to ImkeF
re table group, 5th element , not sure how this helps, for example if you do ;
= Table.Group( Source, {"Item"} ,
{{"N", each let t = _ in
Table.InsertRows( t, Table.RowCount( t ) ,
{ [Item = null ] } ) , (x,y)=>
how don't see how the comparer can be used to prevent adding rows to the final tablle , which
is why i tend not to use it. Unless you meant something completely different ?
If you don't like that approach you can go for something simpler
let
Source = {"a", "a", "a", "b", "b", "b", "b", "c", "c", "d", "d", "d", "e","a","a","b"},
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Added Index" = Table.AddIndexColumn(#"Converted to Table", "Index", 0, 1, Int64.Type),
#"Grouped Rows" = Table.Group(#"Added Index", {"Column1"}, {{"Rows", each _, type table [Column1=text, Index=number]}, {"MinIndex", each List.Min([Index]), type number}},GroupKind.Local),
#"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Index"}, {"Index"})
in
#"Expanded Rows"- Dicken9 months ago
Post Prodigy
thanks but i was really interested in a list generate approach.
- lbendlin9 months ago
Super User
let Source = {"a", "a", "a", "b", "b", "b", "b", "c", "c", "d", "d", "d", "e","a","a","b"}, Changes = List.Generate(() => 0, each _ < List.Count(Source), each _ +1 , each if _ = 0 then 0 else if Source{_}=Source{_-1} then 0 else 1) in Changes