Skip to content

Instantly share code, notes, and snippets.

@ImkeF
Last active April 16, 2021 12:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ImkeF/01da3ce14b5d3e23715ccae36b57f820 to your computer and use it in GitHub Desktop.
Save ImkeF/01da3ce14b5d3e23715ccae36b57f820 to your computer and use it in GitHub Desktop.
let func =
(SelectionList as list, ListOfPositions as list, optional StartWithOne as number, optional FillWithNulls as number) =>
let
selectionList = List.Buffer(SelectionList),
listOfPositions = ListOfPositions,
StartPosition = if StartWithOne = 1 then -1 else 0,
Result = {List.Transform(listOfPositions, each selectionList{_ + StartPosition}), // null
List.Transform(listOfPositions, each selectionList{_+StartPosition}?), // 0
Table.RemoveRowsWithErrors(Table.FromColumns({List.Transform(listOfPositions, each selectionList{_+StartPosition})})) //1
}{List.PositionOf({null,0,1}, FillWithNulls)}
in
Result
, documentation = [
Documentation.Name = " List.SelectPositions
", Documentation.Description = " Selects all items from <code>SelectionList</code> that are on positions in <code>ListOfPositions</code>.
" , Documentation.LongDescription = " Selects all items from <code>SelectionList</code> that are on positions in <code>ListOfPositions</code>. For the behaviour of the optional parameters see: https://wp.me/p6lgsG-N8 .
", Documentation.Category = " List
", Documentation.Source = " https://wp.me/p6lgsG-N8 .
", Documentation.Author = " Imke Feldmann: www.TheBIccountant.com .
", Documentation.Examples = {[Description = " For details see: https://wp.me/p6lgsG-N8 .
" , Code = " List.SelectPositions({1..5}, {0, 3, 4})
", Result = " {1,4,5}
"]}]
in
Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation))
@Hugoberry
Copy link

Hey Imke, Great packaging of the function!
btw what do you think if you use
"each SelectionList{_+StartPosition}?"
It will result in "cleaner" way of try X otherwise null

@ImkeF
Copy link
Author

ImkeF commented Nov 18, 2019

Hey Igor @Hugoberry, absolutely! thanks for the hint :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment