each & underscore_ in Power Query Explained

Goodly
19 Oct 202209:57

Summary

TLDRこのビデオスクリプトは、パワークエリにおける'each'キーワードとアンダースコアの使い方を詳しく説明しています。'each'はコレクション内の各アイテムを意味し、アンダースコアは文脈によって意味が変わります。テーブルやリストの文脈での実践例を通じて、アンダースコアがテーブルでは行(レコード)を、リストではリストの各アイテムを表すようになる理由が説明されています。また、関数を用いたより複雑なデータ操作の例も紹介されており、パワークエリの構文が簡潔で読みやすいように設計されている点も強調されています。

Takeaways

  • 😀 「each」キーワードは、コレクション内の各アイテムを意味しますが、コンテキストによって意味が変わります。
  • 🔍 「_」アンダースコアは、その文脈によって意味が変わる特別なキーワードです。テーブルでは行を、リストではリスト内の各アイテムを表します。
  • 📊 Power Queryで「Table.AddColumns」関数を使用すると、「each」キーワードとアンダースコアを用いて新しい列を追加できます。
  • 💡 アンダースコアを使用すると、テーブルの各行(レコード)を変数として扱うことができます。これにより、行内の特定のフィールド(例えば「amount」)にアクセスできます。
  • 📝 「each」とアンダースコアは、関数を簡単に記述するための糖衣構文であり、Power Queryの構文を読みやすくします。
  • 🔗 リストの文脈で「each」キーワードとアンダースコアを使用すると、リスト内の各アイテムに対して操作を実行できます。
  • 🛠 「each」キーワードとアンダースコアは、関数を定義する際に使用されることができ、これによりテーブルやリストの各アイテムを操作できます。
  • 📚 Power Queryの学習者は、このビデオの説明を通じて、「each」キーワードとアンダースコアの使い方をより深く理解できます。
  • 🎓 ビデオでは、DAXやPower Queryのコースへのリンクが提供されており、これらは初心者から上級者まで幅広くデータ分析のスキルを身につけるのに役立ちます。
  • 🎉 ビデオの最後に、視聴者からのフィードバックや質問を歓迎するメッセージが含まれており、視聴者がより深く学び込む機会を提供しています。

Q & A

  • 「each」キーワードは何を意味するのですか?

    -「each」キーワードは、コレクション内の各アイテムを意味します。

  • アンダースコア(_)は何を意味するのですか?

    -アンダースコアは文脈によって意味が変わります。テーブルの文脈では行(レコード)を指し、リストの文脈ではリストの各アイテムを指します。

  • 「each」キーワードとアンダースコアはどのように使用されますか?

    -「each」キーワードとアンダースコアは、Power Queryの関数内で使用され、特定のコレクションの各アイテムに対して操作を実行するために使われます。

  • テーブルに新しい列を追加するにはどうすればいいですか?

    -テーブルに新しい列を追加するには、「table.addcolumns」関数を使用し、「each」キーワードとアンダースコアを組み合わせて、各行の特定のフィールドを取得または操作します。

  • リストに対して「each」キーワードとアンダースコアを使用すると何が起こりますか?

    -リストに対して「each」キーワードとアンダースコアを使用すると、リストの各アイテムに対して操作を実行することができます。例えば、リストの各アイテムに1を加算するなどです。

  • アンダースコアはどのようにして変数として機能するのですか?

    -アンダースコアは関数内で使用される際に、コレクションの各アイテム(テーブルの行やリストの要素)を参照する変数として機能します。

  • 「each」キーワードとアンダースコアが提供する「糖衣構文」とは何ですか?

    -「糖衣構文」とは、Power Queryの関数をより読みやすく、簡単に記述するための簡略表現です。「each」キーワードとアンダースコアは、関数を定義する際に使用され、その結果を簡単に取得することができます。

  • 関数内で変数を宣言する必要がある理由は何ですか?

    -関数内で変数を宣言することにより、関数の処理中に行ったり来たりするデータや値を参照しやすくなります。これにより、コードの可読性と管理性が向上します。

  • テーブルとリストの文脈で「each」キーワードとアンダースコアを使用する際の違いは何ですか?

    -テーブルの文脈ではアンダースコアは行(レコード)を参照し、リストの文脈ではリストの各アイテムを参照します。これは、データ構造によって変数として保持されるデータの性質が異なるためです。

  • 「each」キーワードとアンダースコアを使用する際に文脈が重要である理由は何ですか?

    -文脈が重要であるのは、アンダースコアが参照するデータの性質が文脈によって変わることによるためです。テーブルとリストなど異なるデータ構造に対して同じキーワードを使用する場合、期待する動作が異なるためです。

Outlines

00:00

🔍 各キーワードの理解:Power Queryでの「each」と「_」

このセクションでは、Power Queryにおける「each」と「_」キーワードの使い方を解説しています。まず、「each」はコレクション内の各アイテムを指し、「_」は文脈によって意味が変わると説明しました。次に、テーブルに新しい列を追加する例を通じて、「each」と「_」がどのように機能するかを実演しました。テーブルの文脈では、「_」は行(レコード)を指し、リストの文脈ではリスト内の各アイテムを指すことを示しました。また、「each」と「_」は関数の糖衣構文であり、Power Queryのシンタックスを読みやすくする役割を果たすと述べています。

05:01

🛠 関数による「each」と「_」の代替表現

このセクションでは、「each」と「_」を関数として代替する例を示しています。まず、テーブルに対して変数を宣言し、その変数がテーブルの各行(レコード)を保持することを説明しました。次に、変数を関数として使用し、レコードから特定のフィールドを抽出する方法を実演しました。同様に、リストに対しても関数を宣言し、リスト内の各アイテムに対して操作を行う方法を説明しました。最後に、関数を用いた「each」と「_」の代替表現の利便性を強調し、より複雑なクエリでもこれらの概念がどのように適用されるかについても触れています。

Mindmap

Keywords

💡Power Query

Power Queryは、Microsoft ExcelやPower BIなどのデータ分析ツールに組み込まれているデータ変換およびデータ取得機能です。ビデオでは、Power Queryの機能を使ってデータテーブルを操作し、新しい列を追加する方法が説明されています。

💡each

「each」はPower Queryの構文で使用されるキーワードで、コレクション内の各アイテムに対して操作を実行する際に使われます。ビデオでは、テーブルの各行に対して新しい列を追加する例として説明されています。

💡underscore

アンダースコア(_)は、Power Queryの関数内で使用される変数であり、現在のアイテムを参照します。ビデオでは、テーブルの各行(レコード)やリストの各アイテムを指すために使用されています。

💡table.add columns

「table.add columns」はPower Queryの関数で、テーブルに新しい列を追加するために使用されます。ビデオでは、この関数を使ってテーブルに「new」という新しい列を追加し、その列に特定の値を設定する方法が示されています。

💡record

「record」とは、Power Queryにおいてテーブルの行を指すデータ構造です。ビデオでは、アンダースコアを使用してテーブルの各行(レコード)を参照し、その行から特定のフィールド(例えば「amount」)を取得する方法が説明されています。

💡list.transform

「list.transform」はPower Queryの関数で、リストの各アイテムに対して特定の変換を適用するために使用されます。ビデオでは、この関数を使ってリストの各数字に1を加算し、新しいリストを作成する方法が示されています。

💡function

関数は、Power Queryで定義され、特定のタスクを実行する一連の命令です。ビデオでは、関数を定義してテーブルの各レコードから特定のフィールドを取得したり、リストの各アイテムに特定の計算を適用する方法が説明されています。

💡variable

変数は、プログラム中で値を保持するための名前です。ビデオでは、変数「r」や「x」を使用してテーブルのレコードやリストのアイテムを参照し、それらに対して操作を実行する方法が説明されています。

💡context

コンテキストは、コード内の特定の文脈で変数の意味が変わることを指します。ビデオでは、アンダースコアがテーブルのコンテキストではレコードを、リストのコンテキストでは各アイテムを参照するように意味が変わる例が説明されています。

💡sugar syntax

シュガー構文は、プログラムの書きやすさや読みやすさを向上させるために使用される簡略化された構文です。ビデオでは、「each」やアンダースコアがPower Queryの関数をより読みやすくするためのシュガー構文であると説明されています。

Highlights

Explains the 'each' keyword in Power Query

Describes the underscore's context-dependent meaning

Shows how to add a column using 'table.addcolumns'

Clarifies what 'each' means in the context of a table

Demonstrates capturing the entire row with the underscore

Illustrates extracting a specific field from a record

Discusses the difference between 'each' and underscore in lists

Explains how underscore captures every single item in a list

Shows how to transform a list using 'list.transform'

Details the importance of context with 'each' and underscore

Provides an alternative syntax to 'each' and underscore

Demonstrates declaring a variable to replace 'each' and underscore

Shows how to create a function to transform table records

Explains extracting a field from a record using a function

Discusses creating functions for lists similar to 'each' and underscore

Illustrates incrementing list values using a function

Encourages viewers to ask questions for further clarification

Promotes the presenter's DAX and Power Query courses

Transcripts

play00:00

this has been the most requested video

play00:02

so far for each and every one of you I'm

play00:05

going to talk about the each keyword in

play00:07

power query words would not do any

play00:09

justice let's just go deep dive and take

play00:12

a look at what the hell is each in

play00:14

powerquery let's go when you're trying

play00:17

to understand the each and the

play00:18

underscore the each is the easy one

play00:20

because it means the same like every

play00:22

single item in a collection but what

play00:25

does the underscore mean the underscore

play00:27

on the other hand changes the meaning

play00:29

depending upon on the context in which

play00:31

you're using it let's just take a look

play00:33

at a practical example now I have a

play00:35

table three columns right here and I

play00:37

would like to add a column to this table

play00:40

for that I'm going to use a function

play00:41

called table. add columns let's just

play00:43

start with that so table. add column and

play00:47

I'm going to maybe start to write the

play00:50

name of the column so let's just say new

play00:51

the first part of the formula is source

play00:53

source happens to be the name of the

play00:55

entire table which is nothing but the

play00:57

previous step new happens to be the name

play00:59

of the column that I'm creating and

play01:01

after that I'm going to say that I would

play01:02

like to write the each which means every

play01:05

single item but what does the item mean

play01:07

right here I'm going to write the

play01:09

underscore right here and close the

play01:10

bracket now as soon as I happen to use

play01:12

the underscore in the context of the

play01:15

table it simply means every single item

play01:18

of the table and the item here is

play01:20

nothing but the row so this is the first

play01:22

record the second record record means

play01:24

row and the third record and so on and

play01:26

so forth now as soon as I commit to this

play01:28

particular function it's actually going

play01:29

to create create a new column in here

play01:31

which is where it'll actually capture

play01:33

the record so if you peek into this

play01:35

record right here you're going to see

play01:37

that the entire data of the first row

play01:39

has been captured right here now since I

play01:41

happen to use the underscore in the

play01:43

context of the table the underscore

play01:46

meant that you're trying to catch hold

play01:49

of the record of this particular table

play01:51

now once we have been able to extract

play01:53

the record which is right here I could

play01:55

do anything with that record which is

play01:57

now stored in this little variable right

play01:59

here which which is nothing but the

play02:00

underscore let's just say that I'm

play02:02

trying to maybe capture like a column of

play02:04

this particular uh record right here so

play02:07

the record has got three columns date

play02:08

sales Dev and amount maybe I'm just

play02:10

trying to capture the amount right here

play02:12

so how do I do that there can be another

play02:14

function which is nothing but let's say

play02:16

record. field and I'm going to say that

play02:19

the underscore gives me the record and

play02:21

I'm going to say that my field is

play02:23

nothing but the amount and I'm going to

play02:25

close the bracket and press enter now

play02:27

what this is going to do is it's going

play02:28

to go inside of that record which is

play02:30

stored in the variable underscore and

play02:32

just pull up the amount and give you the

play02:33

answer which is nothing but the amount

play02:36

right here now like I said that the

play02:37

underscore changes the meaning depending

play02:39

upon the context let's just take a look

play02:41

at the each and the underscore in the

play02:43

context of a

play02:45

list okay I'm playing with the simple

play02:47

list right here which has got just five

play02:49

numbers and I will try to write the each

play02:51

and the underscore in the context of the

play02:53

list now let's just create a new step in

play02:56

here and I will use a list function this

play02:58

time since I'm working with a list so

play03:00

there happens to be a function called

play03:01

list. transform the first part of the

play03:04

list. transform function is to provide a

play03:06

list and Source was the step just

play03:08

previous which contained the five

play03:10

numbers and that's my list and now let's

play03:12

just happen to write the each keyword so

play03:13

let's just say each underscore and press

play03:15

enter now the underscore here means

play03:18

every single item of the list which is

play03:21

not a table so it doesn't mean a record

play03:23

anymore since the context has changed

play03:25

this time the underscore is going to

play03:27

capture every single item of this list

play03:30

so the first time it's going to capture

play03:31

one the second time it's going to

play03:33

capture two the third time it's going to

play03:34

capture three so on and so forth and as

play03:36

soon as I press enter nothing changes

play03:37

it's going to give you the same value

play03:39

because you just captured that value and

play03:41

put it right here you did nothing with

play03:43

that let's just say that for every

play03:44

single item of the list I would want to

play03:46

add a one so I could probably say

play03:49

something like now that the underscore

play03:51

has captured one maybe I'd like to add

play03:53

one to that so I'm going to say

play03:54

something like + one and as soon as I do

play03:56

that you now have counting from 2 to 6

play04:00

so the point that I was trying to make

play04:01

which is very very important is that the

play04:03

each keyword is not very hard to

play04:05

understand it means every single item

play04:07

but the underscore part which refers to

play04:09

the item is going to change the meaning

play04:12

depending upon the context in which it

play04:14

has been used if you're using it in in

play04:16

the context of a table it means a record

play04:19

if you're using this in the context of a

play04:20

list it means every single item of the

play04:22

list an item of the list is every single

play04:24

value I hope you understood that we not

play04:27

done yet the each and the underscore

play04:30

happens to just be a sugar syntax or

play04:32

maybe an easy way of writing functions

play04:36

um that make the syntax of power query a

play04:38

bit easy to read now let's just take a

play04:40

look at the same query that we had been

play04:41

working with in the past which is where

play04:43

we had the table and we use the table.

play04:45

add columns function to add a column and

play04:47

each underscore is the keyword that we

play04:49

wrote Let's just delete that for just a

play04:51

bit and to start to read that what the

play04:53

formula is asking for so if I just put

play04:55

in comma here you can see that the

play04:57

table. add columns function is asking me

play05:00

to give me a column generator as a

play05:03

function that give me a function that

play05:05

could generate a column it's not asking

play05:07

you for the each and underscore here it

play05:09

is asking you for a function and as a

play05:12

simple syntax of creating a function we

play05:15

happened to write the each and

play05:17

underscore now here let's just start to

play05:19

add a function let's just see that is

play05:21

that similar to writing an each and

play05:23

underscore or not so I'm going to maybe

play05:25

go in here and first start to declare a

play05:27

variable and let's just open the bracket

play05:29

and declare any variable let's just call

play05:30

the variable R you can call it anything

play05:32

but for the moment I'm going to call

play05:34

this as r as soon as you declare the

play05:36

variable r r is going to hold up every

play05:40

single item of the table and like I said

play05:43

every single item of the table is

play05:44

nothing but the record of the table or

play05:46

the row of the table so R here means the

play05:49

row of the table now how do I convert

play05:51

the R into a function so I'm just going

play05:53

to maybe start to write an equals to

play05:54

sign and a greater than symbol this

play05:57

rocket sign right here means function

play05:59

anything that you write after that

play06:01

happens to be a function and that's what

play06:03

the formula is asking me for so I'm

play06:05

going to say that hey I don't really

play06:07

want to do anything just give me the r

play06:09

back which is nothing but my record back

play06:11

I'm going to close in the bracket and

play06:13

press enter and what do I get I get the

play06:15

same output like I was able to get with

play06:18

the each and the underscore and the each

play06:20

and the underscore happens to be an easy

play06:22

way to read power query syntax but this

play06:25

happens to be the same and we can do

play06:27

something with that r r happens to hold

play06:29

the record value for every single row

play06:31

and we could do something with it so

play06:33

let's just say that we would want to

play06:35

again extract the amount from here let's

play06:38

just extract it in a slightly different

play06:40

way so this time I'm going to say that

play06:42

hey R happens to hold the record the

play06:44

record has got three columns date sales

play06:46

D and amount and I would want to extract

play06:48

the amount so I'm going to say that hey

play06:50

square bracket and I'm going to say

play06:52

amount and this time as soon as I press

play06:54

enter I'm going to get the amount of

play06:56

that particular value right here awesome

play06:58

now let's just start to create functions

play07:01

in a list and let's just see if it also

play07:03

means the same over there all right I'm

play07:05

back at my list which is where just a

play07:07

while ago we had five numbers one

play07:09

through five and to every single number

play07:11

we added one using the each and the

play07:13

underscore keyword let's just happen to

play07:15

delete the each and the underscore

play07:16

keyword and let's just start to create a

play07:18

function let's just quickly verify that

play07:20

as well so if I just happen to write a

play07:21

comma again you can see that this is

play07:23

asking you that hey please give me the

play07:25

transformation that you would like to

play07:27

apply as a function so let's just to

play07:29

start to create a function this time I'm

play07:31

going to maybe declare the function

play07:32

variable as X and I'm going to maybe

play07:35

start to write the function keyword so

play07:38

which is nothing but the equals to and

play07:40

the greater than sign this converts this

play07:42

into a function what has X done X has

play07:45

captured the value of every single item

play07:48

of the list whatever the underscore was

play07:50

doing it earlier the x is doing it now

play07:53

the only difference is that X is now

play07:55

used in a function so I'm going to say x

play07:58

happens to hold every single value I'm

play08:00

just going to maybe want to have the X

play08:02

again if I do nothing with it with X I'm

play08:04

just going to get the values back 1 2 3

play08:06

4 and 5 but if I happen to let's say add

play08:09

+ one right here I'm going to get let's

play08:11

say 2 3 4 5 and six which were just one

play08:14

incremented from one now what you could

play08:16

do is that using a function you could

play08:19

also happen to declare the underscore as

play08:21

a variable so the variable like I said

play08:23

could be anything it could be x y z

play08:25

anything but whatever you happen to

play08:27

write here is going to hold up the value

play08:29

of every every single item of that

play08:31

particular structure that you're working

play08:33

with table record list just one single

play08:35

item so this time I'm going to say that

play08:37

hey xcore +1 which is not going to give

play08:39

you the same output right here so just

play08:42

another way of writing it each in

play08:44

underscore or you could write a function

play08:46

and you now know both of them all right

play08:48

that's been it the each and the

play08:50

underscore in power query let me know

play08:52

how did you find this one and in case

play08:54

you have any questions around this

play08:55

please feel free to drop in a comment

play08:57

and I will be glad to reply obviously I

play08:59

trying to use some very simple examples

play09:01

to demonstrate how the each and the

play09:03

underscore works but if you happen to

play09:05

use a very complicated query and you

play09:07

maneuvering between the table and a list

play09:09

and other items of power query

play09:11

structures then each an underscore would

play09:14

mean different items of that particular

play09:17

object structure that you're trying to

play09:18

work with I hope you understood that

play09:21

part really really well let me know if

play09:22

you have any questions around this and

play09:24

I'll be glad to reply in the end a big

play09:25

shout about my Dax and my power query

play09:27

courses in case you're starting out with

play09:29

RBI please feel free to check out my

play09:31

courses they're going to take you from a

play09:33

beginner level to a more advanced level

play09:35

and help you solve more sophisticated

play09:37

and more advanced problem even of your

play09:38

own data thanks so much for watching

play09:40

this and I'll catch you guys in the next

play09:41

one cheers bye

play09:44

[Music]

Rate This

5.0 / 5 (0 votes)

相关标签
Power Queryデータ分析チュートリアル各要素アンダースコアテーブルリスト変数関数
您是否需要英文摘要?