Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Dicken
Post Patron
Post Patron

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. 

1 ACCEPTED SOLUTION

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.

View solution in original post

8 REPLIES 8
Dicken
Post Patron
Post Patron

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. 

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.

Yes, one thing, if posting code, I feel i should use tags as it would make easier for people to read, 
would you recommend ?  

Dicken
Post Patron
Post Patron

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. 

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
wdx223_Daniel
Super User
Super User

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 

slorin
Super User
Super User

Hi @Dicken 

= List.Combine(
List.Transform(
alist,
each if List.Contains(itemlist, _) then {_, null, null} else {_}
)
)

Stéphane

lbendlin
Super User
Super User

Remember you can use a shortcut to append lists

 

List.First(list,2) & {null} & List.Skip(list,2)

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors