Forum Discussion
List Positions
- 1 year ago
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.
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- MasonMA1 year agoSuper User
I think your try/otherwise approach is perfectly valid and likely the most maintainable option:)
- p45cal1 year agoSolution Supplier
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.
- Dicken1 year agoPost Prodigy
Thank you both.