Forum Discussion
LIst Generate ; to insert at given position
H,
is there a way to use List.Generate to insert a null , at a given position,
so here I have to ways to calculate the positons, one gen one accer ;
Inserting 2 places;
let
alist = {"A".."Z"} ,
itemlist = {"D", "K", "N"} ,
pos = List.PositionOfAny(alist,itemlist,Occurrence.All),
newpos = List.Accumulate( pos, {}, (s,c)=> s & { ( List.Count(s) * 2 ) + c + 1 } )
in
newpos
let
alist = {"A".."Z"} ,
itemlist = {"D", "K", "N"} ,
pos = List.PositionOfAny(alist,itemlist,Occurrence.All),
newpos = List.Generate( ()=> [ x = 0 , y = pos{0}, z = y + 1] ,
each [x] < 3 ,
each [ x = [x] + 1, y = pos{x} , z = (x * 2) + y +1] ,
each [z] )
in newpos
Then to insert I have been using accumuate;
result = List.Accumulate( newpos, alist, (s,c)=> List.InsertRange( s, c, {null,null} ) )
I have been trying to replicate the above using List.Generate, but end up with nested lists one for each positon,
not what I wnated. Any suggestions ?
Richard.
- Anonymous1 year ago
Hi Dicken ,
Just wanted to check and make sure everything is working good with the solution you shared. It looks like a solid approach, and I am really glad you found a way to use List.Generate to solve your issue.
Is it meeting your needs so far? If there is anything else you would like to adjust or if you run into any other challenges, feel free to reach out .We happy to help further.
Thank you.
8 Replies
- lbendlinSuper User
Remember you can use a shortcut to append lists
List.First(list,2) & {null} & List.Skip(list,2)
- wdx223_DanielCommunity Champion
let
alist = {"A".."Z"} ,
itemlist = {"D", "K", "N"} ,
newpos = List.TransformMany(alist,each if List.Contains(itemlist,_) then {_,null,null} else {_},(x,y)=>y)
in
newpos - DickenPost Prodigy
How are these list.generate, which is what was asked. i do like the
List.Transform / many , but I wanted to use generate if possible, if it's not possibel that's fine.- lbendlinSuper User
let alist = {"A".."Z"} , itemlist = {"D", "K", "N"}, blist = List.Combine(List.Generate(()=>0,each _ < List.Count(alist),each _+1,each if List.Contains(itemlist,alist{_}) then {null,alist{_}} else {alist{_}})) in blist
- DickenPost Prodigy
To offer a sort of answer to my own question, I had want to use actrual number positons with generate ,
rather than a true false approach , but this does work, I have used null but ideally would create blank record,
[ reclist = Table.ToRecords(Source) ,
poslist = {"F", "K", "N"} ,
gen = List.Generate( ()=> [
x = 0, y = reclist{0} , z = {y} ] ,
each [x] < List.Count( reclist) ,
each [ x = [x] + 1, y = reclist{x} , z =
if List.Contains( poslist, Record.Field( y, "Item"))
then { y,null} else {y} ] ,
each [z] ) ,
result = List.Combine( gen ) ] [result]
Richard.- AnonymousNot applicable
Hi Dicken ,
Just wanted to check and make sure everything is working good with the solution you shared. It looks like a solid approach, and I am really glad you found a way to use List.Generate to solve your issue.
Is it meeting your needs so far? If there is anything else you would like to adjust or if you run into any other challenges, feel free to reach out .We happy to help further.
Thank you.- DickenPost Prodigy
Yes, one thing, if posting code, I feel i should use tags as it would make easier for people to read,
would you recommend ?