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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Dicken
Responsive Resident
Responsive Resident

List Positions

  Can some one explain what is happenign here so, straightforward  position;

 

[ alist = {1,3,2,"a",2,2,2,2,"a",2,2,2,2,"a",2,2} ,
result = List.PositionOf( alist, "a",Occurrence.All) ]

 but here I have a list of true / false   , the problem is Occurrence.All, if that is omitted then the first 'false is returned

otherwise an error. 

[ alist = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,6,6,7},
tflist = List.Transform( List.Positions( alist),(x)=> alist{x} = alist {x+1} ) ,
result = List.PositionOf( tflist, false ,Occurrence.All ) ]


Richard

1 ACCEPTED SOLUTION

There's guaranteed to be an error in the last member of the list (you're referring to x+1 which doesn't exist).

Using an otherwise = true will mask that error which is fine, although as a coder you'll realise this masks any other error that may appear in the list and you wouldn't be alerted to it. My approach would be to remove the known error at the end of the list with a List.RemoveLastN(tflist,1) (or do it in the same line) so that you're only removing the expected error, then do your ListPositionOf with Occurence.All.

View solution in original post

5 REPLIES 5
slorin
Super User
Super User

Hi @Dicken 

Another solution with List.IsDistinct

List.PositionOf(
List.Transform(
List.Zip({List.RemoveFirstN(alist,1), List.RemoveLastN(alist,1)}),
List.IsDistinct),
true,
Occurrence.All)

 Stéphane

Dicken
Responsive Resident
Responsive Resident

to answer my own question, the last value throws and error and this does not seem to liked by 
positons/ select etc; so I added a try otherwise and this solves the problem, there may be more elegant way ? 

let
    Source = [ alist = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,6,6,7},
tflist = List.Transform( List.Positions( alist),(x)=>  
try alist{x} = alist {x+1} otherwise   true ) ],
    Custom1 = List.PositionOf(   Source [tflist], 
false , Occurrence.All)
in
    Custom1

There's guaranteed to be an error in the last member of the list (you're referring to x+1 which doesn't exist).

Using an otherwise = true will mask that error which is fine, although as a coder you'll realise this masks any other error that may appear in the list and you wouldn't be alerted to it. My approach would be to remove the known error at the end of the list with a List.RemoveLastN(tflist,1) (or do it in the same line) so that you're only removing the expected error, then do your ListPositionOf with Occurence.All.

Dicken
Responsive Resident
Responsive Resident

Thank you both. 

MasonMA
Resident Rockstar
Resident Rockstar

I think your try/otherwise approach is perfectly valid and likely the most maintainable option:)

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors