2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

小ネタ:複数の位置番号を指定して、リストの中身を取る|Power Query

Last updated at Posted at 2021-01-23

ふと気づいたら、そのような記法や既定の関数がないようなので、一つの解法を残しておきます。

結論

let Source ={73,82,77,56,21,56,91,44,98,27,29,47,63,47,98,3,86,100,68,31}
in  List.Accumulate(
       {4,5,10,13}, //位置を指定するリスト。0始まりで、4,5,10,13番目を取りたい。
       {}, //初項
       (x,y)=>x & { Source{y} } //1周目は初項と4、2周目は{4}と5・・・というように実行される。
    ) 

無題2.png

解説

あるlistについて、特定の位置の値だけ欲しいというとき、1個なら、こういう記法で取れます。0始まりで数えます。

普通に位置一個を指定する書き方
let Source ={73,82,77,56,21,56,91,44,98,27,29,47,63,47,98,3,86,100,68,31}
in  Source{2}  //0始まりで2番目を指定。77が返る。

ただ、それが例えば、4,5,10,13番目だけが欲しいというような場合。こんな記法は許されません。

エラーになる例
let Source ={73,82,77,56,21,56,91,44,98,27,29,47,63,47,98,3,86,100,68,31}
in  Source{4,5,10,13}  //エラーになる。

なので、結論に書いたようなコードで取りたい番号のリストを作り、取ってはくっつけてやることになります。
List.Accumulate大事ですね。

もちろん、地道に書いてもよいのですが、量や用途によっては取れない方法です。

個人的にはやりたくない
let Source ={73,82,77,56,21,56,91,44,98,27,29,47,63,47,98,3,86,100,68,31}
in  Source{4} & Source{5} & Source{10} & Source{13}

参考

list自体についての基礎
https://docs.microsoft.com/ja-jp/powerquery-m/m-spec-values#list
List.Accumulate関数
https://docs.microsoft.com/ja-jp/powerquery-m/list-accumulate

2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?