rmae19
rmae19

Reputation: 77

Select more than one item by index position in a M / PowerQuery list?

Let's say I have the following list in M language: {"A", "B", "C" } and I want to select more than one item. How can I do it? When I check the M language specs it only shows examples on how to select one item through the following syntax {list}{#index} but that only retrieves a single item. I tried using the : operator as in other languages without success.

Upvotes: 0

Views: 1561

Answers (2)

Power Query doesn't support this with a single function. But you can make your own logic.

The basic pattern is:

List.Transform( 
  { 0, 2 },  
  each {1..100}{ _ } 
)

The starting list is a list from 1 to 100 represented by {1..100}.

We retrieve the first (index=0) and third (index = 2) item with above code.

You can find the above code turned into a function at his URL: https://gorilla.bi/power-query/select-items-by-position-in-list/

There's also 2 alternative methods in case you want to look at another.

Cheers, Rick

https://powerquery.how/

Upvotes: 1

horseyride
horseyride

Reputation: 21318

You could pull specific items in the list like this, position 2,1,4

= List.Combine ({ {{list}{2}}, {{list}{1}}, {{list}{4}} })

Or you could pick a range with List.Range see List.Range

Or pick out one of the other List.xxx functions in list functions

Upvotes: 2

Related Questions