Power Query Running Count Based on Row Condition – Excel Magic Trick 1588

excelisfun
13 Aug 201907:25

Summary

TLDRこのビデオでは、Excelのワークシートで行ったように、Power Queryでランニングカウントを実装する方法が説明されています。Excelではセル参照を使用して簡単に行うことができるが、Power Queryではテーブル参照とカスタム関数を用いて行う必要があります。ビデオでは、データがExcelのテーブルにある場合、まずデータを並べ替え、テーブルをバッファリングし、カスタム列を追加してカウントを実装する方法をステップバイステップで説明しています。最後に、データ型を整数に変更して、Power Queryの計算結果をExcelシートにロードする方法も紹介されています。

Takeaways

  • 📊 エクセルのワークシートでは、特定のアイテムの逐次カウントを簡単に行うことができますが、Power Queryでは異なるアプローチが必要です。
  • 🔢 逐次カウントを実行するには、列内のアイテムが一定の順序で並んでいる必要があります。
  • 💾 Power Queryを使用するには、データがExcelのテーブル内にあらかじめ存在している必要があります。
  • 🔧 Power Queryでは、セル参照を使用して列全体またはその一部、または個々の行を参照することはできません。
  • 📈 Power Queryで逐次カウントを実行するには、テーブルをソートし、その順序でテーブルを選択する必要があります。
  • 🔗 Power Queryでテーブルを参照する際には、テーブルをバッファリングして、各行で参照する必要があります。
  • 🛠️ Mコードを使用してカスタム列を追加し、逐次カウントのロジックを構築します。
  • 🔍 内部テーブルから外部テーブルの行と比較する条件を設定する必要があります。
  • 📋 Power Queryでは、テーブルから行を数えるためには、table.rowcount()関数を使用します。
  • 🎓 新しいユーザーは、MSP TDAクラスのYouTubeビデオを通じてPower QueryとMコードの基礎を学ぶことができます。

Q & A

  • Excelのエクセルで行うランニングカウントはどのようにPower Queryで実現できますか?

    -Power Queryでは、Excelのセル参照のように列全体または個々の行を参照する機能がないため、テーブルの選択とフィルタリングを使用してランニングカウントを計算します。

  • Power Queryでランニングカウントを計算する前に何が必要ですか?

    -ランニングカウントを計算する前に、列が一定の順序で並んでいる必要があります。また、データがExcelのテーブルに既に存在している必要があります。

  • Power Queryでテーブルをソートするにはどうすればいいですか?

    -Power Queryでテーブルをソートするには、列のドロップダウンから「昇順」「降順」のいずれかを選択します。

  • 「バッファリング」とはPower Queryで何を意味しますか?

    -「バッファリング」とは、テーブルを1回だけ読み込んで、各行の計算でその読み込まれたテーブルを使用するプロセスです。

  • Power Queryでカスタム列を追加する際に「each」キーワードは何を意味しますか?

    -「each」キーワードは、Mコードのカスタム関数で使用され、各行に対して関数を実行する際の省略形です。

  • Power Queryで行をフィルタリングする際に使用する関数は何ですか?

    -行をフィルタリングする際には、`Table.SelectRows` 関数を使用します。

  • Power Queryで行の数を数えるにはどのような関数を使用しますか?

    -行の数を数えるには、`Table.RowCount` 関数を使用します。

  • Power Queryでデータ型を変更するにはどうすればいいですか?

    -データ型を変更するには、列を右クリックして「データの変更」を選択し、希望のデータ型に変更します。

  • Power Queryで計算結果をExcelの既存のシートにロードするにはどうすればいいですか?

    -計算結果をExcelの既存のシートにロードするには、「閉じる&ロード」オプションを使用し、必要に応じて既存のシートを選択します。

  • Power QueryやMコードの基礎を学ぶためにおすすめのリソースは何ですか?

    -Power QueryやMコードの基礎を学ぶためには、YouTubeのMSP TDAクラスの無料ビデオ(ビデオ3から12)がおすすめです。

Outlines

00:00

📊 ExcelからPower Queryへの移行とランニングカウントの作成

この段落では、Excelのワークシートで使用していたフォーミュラをPower Queryに移行する方法について説明しています。Excelでは、セル参照を使用して列ごとにアイテムのランニングカウントを簡単に作成できたが、Power Queryでは異なるアプローチが必要です。データがExcelのテーブルにある場合、countifs関数を使用するのが簡単ですが、Power PivotやPower BIソリューションを構築したり、複雑なデータをインポートする場合はPower Queryが適しています。ランニングカウントを作成するには、まず列内のアイテムが一定の順序で並んでいる必要があります。ここでは、取引番号や日付、時間などに基づいて並べ替え、Power Queryウィンドウを開いてクエリを作成し、カスタム列とカスタム関数を用いて処理する方法が説明されています。

05:00

🔍 Power Queryでのランニングカウントの詳細処理

この段落では、Power Queryでランニングカウントを作成する具体的な手順について詳しく説明しています。まず、列をソートし、テーブルをバッファリングすることで、各行で参照するテーブルを一度ロードしてから使用するようにしています。カスタム列を追加し、Mコードを使用してテーブルから行と列をフィルタリングし、条件を満たす行の数をカウントする方法が示されています。このプロセスでは、内部テーブルと外部テーブルの関係を理解し、条件を適切に設定することが重要です。最後に、データ型を整数に設定し、Excelの既存のシートにロードする方法についても触れています。このセクションは、Power QueryやMコードの初心者向けに、YouTubeの無料ビデオコースを 홍보しています。

Mindmap

Keywords

💡Power Query

Power Queryは、ExcelやPower BIで使用されるデータ変換およびデータ取得の機能です。ビデオでは、Excelのセル参照を使った簡単な操作とは異なり、Power Queryではデータセット全体を扱い、複雑なデータインポートや分析を行う際に役立ちます。ビデオでは、Power Queryを使用して行うデータのランニングカウントの方法が説明されています。

💡Running Count

ランニングカウントとは、データセット内の特定のアイテムごとに逐次増加する数値を意味します。ビデオでは、ExcelのカウントIF機能とは異なり、Power Query内でランニングカウントを実装する方法が解説されています。これは、データが時間順やトランザクション番号順に整然と並んでいる場合にのみ意味を持ちます。

💡Table.Buffer

Table.BufferはPower QueryのMコードの機能で、テーブルをバッファリングしてメモリにロードし、各行に対して再利用できるようにします。ビデオでは、テーブルをバッファリングすることで、行ごとにテーブル全体を参照する必要性をなくし、パフォーマンスを向上させる方法が示されています。

💡Table.SelectRows

Table.SelectRowsは、特定の条件に基づいてテーブルから行を選択するPower QueryのMコードの関数です。ビデオでは、この関数を使用して、ランニングカウントを計算するために、テーブル内の特定の条件に一致する行をフィルタリングする方法が説明されています。

💡Each

EachはMコードのキーワードで、テーブルの各行に対して関数を適用する方法を定義します。ビデオでは、Eachを使用して、テーブルの各行に対してカスタム関数を実行し、ランニングカウントを計算する例が示されています。

💡M Code

MコードはPower Queryで使用されるプログラミング言語で、データ変換ロジックを記述するために使用されます。ビデオでは、Mコードを使用してPower Queryの機能を活用し、Excelの機能を超えたデータ操作を行う方法が解説されています。

💡Custom Column

カスタム列とは、Power Queryでテーブルに新しい列を追加し、その列に計算結果やデータ変換の出力を格納する方法です。ビデオでは、カスタム列を通じてランニングカウントの計算結果をテーブルに追加する方法が説明されています。

💡Row Count

行数のカウントは、テーブル内の行の数を数えるプロセスです。ビデオでは、Table.RowCount関数を使用して、特定の条件に一致する行の数をカウントし、ランニングカウントを計算する方法が示されています。

💡Data Type

データタイプは、データの性質や形式を定義するものです。ビデオでは、計算結果のデータタイプを整数に設定することで、正確なランニングカウントを確保する方法が説明されています。これは、データの整合性と正確性を確保するために重要です。

💡Power Pivot

Power Pivotは、ExcelやPower BIで使用されるデータモデリング機能です。ビデオでは、Power QueryとPower Pivotを組み合わせて複雑なデータセットを操作し、分析情報を提供する例が示されています。

Highlights

在Excel工作表中复制公式以实现每行特定项目的运行计数是容易的,但在Power Query中如何实现呢?

如果数据已经在Excel工作表中,使用COUNTIFS比使用Power Query更容易。

构建Power Pivot或Power BI解决方案,或者导入复杂数据时,Power Query是正确的工具。

运行计数需要列中的项目有一定的顺序。

数据集已经在Excel表中,通过“从表获取和转换”打开Power Query窗口。

创建自定义列并构建自定义Power Query函数。

Power Query中没有允许查看整个列或部分列或单个行的单元格引用。

在Excel中,我们可以使用单元格引用创建可扩展范围,但在Power Query中需要使用Table.SelectRows。

首先需要对列进行排序,以便在刷新时保持正确的顺序。

使用Table.Buffer将整个表加载一次,然后每行都使用该加载的表。

在自定义列中,使用Table.AddColumn函数填充列。

使用Table.SelectRows函数过滤表,需要从内部表获取列并与外部表进行比较。

定义变量并使用Table.SelectRows函数进行过滤,确保每行都能看到实际的IT数字。

使用Table.SelectRows和逻辑测试运算符进一步过滤,以匹配特定条件。

使用Table.RowCount计算行数,创建可扩展范围的运行计数列。

最后,将数据类型设置为整数。

加载数据到现有工作表,虽然比Excel中的单元格引用和COUNTIFS更复杂,但有时需要使用M代码在Power Query中进行相同的计算。

对于Power Query或M代码的新手,可以查看YouTube上的MSTDA课程视频,学习Power Query和M代码的所有知识。

Transcripts

play00:00

a formula like this in an Excel

play00:02

worksheet when we copy it down so that

play00:05

we can have a running count for

play00:07

particular items for each row easy to do

play00:10

in an Excel worksheet but how do we do

play00:13

it in power query now if your data is

play00:23

already in an excel worksheet it's much

play00:25

easier to use countifs than it is to use

play00:28

power query but if you're building a

play00:30

power pivot power bi solution or you're

play00:33

importing a lot of complicated data then

play00:36

power queries the right tool so let's

play00:39

see how to do this now the first thing

play00:41

about a running count is there has to be

play00:44

some order to the items in the column

play00:46

for it to make any sense now we have a

play00:49

transaction number but it could be a

play00:51

date or a time or something like that

play00:52

now this data set is already in an Excel

play00:55

table so we'll go up to data get and

play00:57

transform from table that opens up the

play01:01

power query window over here we want to

play01:03

name our query something like product

play01:06

running count and enter we have two

play01:08

steps source and change and we're gonna

play01:10

create a custom column and we're gonna

play01:13

build some custom power query functions

play01:15

now here's the thing about power query

play01:18

we don't have cell references that

play01:20

allows us to look at the whole column or

play01:23

part of the column or an individual row

play01:26

if we think about how we did it in Excel

play01:28

we can just use cell references that's

play01:31

an expandable range that as I copy down

play01:34

gets bigger and bigger well in power

play01:37

query to create an expandable range

play01:39

we're gonna have to use table dot select

play01:41

rows that means for every single row

play01:45

where we make a calculation we're gonna

play01:47

need the full table and actually filter

play01:50

it then when we try and go get a

play01:53

conditioner criteria from the row we're

play01:56

gonna have to do something different

play01:58

than look at the inside table we're

play02:00

gonna have to look back to the outside

play02:03

table now these concepts are foreign to

play02:05

us excel people because it's so easy to

play02:08

simply use certain types of cell

play02:11

references step one is we have to

play02:13

sort this column that way when we

play02:15

refresh whatever the sort is in the

play02:18

Excel worksheet it'll have the correct

play02:20

sort here so I click the drop-down sort

play02:23

descending I can see my new step the

play02:25

next thing is this is the outside table

play02:27

but we're gonna have to repeat this

play02:28

entire table for every row now if we

play02:31

just put the name of the table and

play02:33

copied it down every row would have to

play02:35

look back to the outside back to the

play02:38

outside so we want to add a step and

play02:40

buffer the table and all that means is

play02:43

it will load it once and then use that

play02:45

loaded table for every row now I'm going

play02:48

to use the f of X button to add a new

play02:49

step down here there's the name of the

play02:51

previous step which of course is a full

play02:53

table table dot and I can see from my

play02:57

drop-down table dot buffer open

play03:00

parentheses close parentheses and enter

play03:03

click over and applied steps f2 and

play03:06

we're gonna rename this buffer table and

play03:09

enter now we go to add column custom

play03:12

column we'll name the new column and

play03:14

just to remind you how custom columns

play03:18

work we're just going to fill the column

play03:20

with ones click OK we can see in the

play03:22

formula bar that when we use custom

play03:24

column it uses the power query function

play03:27

table dot add column now buffer table

play03:30

here just means it's looking to the full

play03:32

table from the previous step that's the

play03:35

name of this new column the word each is

play03:38

a special word in M code that is

play03:40

shorthand for a custom function now

play03:43

because we're going to use two custom

play03:45

functions back-to-back we want to

play03:47

explicitly define the functions rather

play03:49

than use each so here's how we do it

play03:52

we're going to define our variable in

play03:54

parenthesis boaty then we're gonna use

play03:57

the go-to operator equal sign greater

play04:00

than all of that is explicitly doing

play04:04

what each did if I hit enter right now

play04:06

it just says hey for that function

play04:08

please go to each row and put a 1 now

play04:11

that's not what we want because we want

play04:13

first to filter a table so we type table

play04:16

dot select rows tab the first argument

play04:20

in this table dot select rows we're

play04:23

gonna put a table buffer table

play04:26

, the condition well this is where we

play04:28

need to get a column from the inside

play04:31

buffer table remember that table will be

play04:33

a full table in each row we're gonna

play04:35

have to get a column from this table and

play04:37

then compare it to whatever's in the row

play04:40

from the outside table so we're going to

play04:43

define our second function this one will

play04:45

be in side table and the go-to operator

play04:50

now we need a full column from this

play04:52

table and then the condition from the

play04:54

outside table so we say hey inside table

play04:57

open square brackets and we better spell

play05:00

this transaction number correctly

play05:03

let's click expand and now we're going

play05:05

to run our condition with our

play05:06

comparative operators how many of you in

play05:09

the full column are less than or equal

play05:10

to outside table and then we repeat the

play05:14

same column name so now for every row in

play05:17

the table because we have ot it will

play05:20

always see the actual number for I T

play05:22

it'll always have the full column now

play05:25

watch what happens when we hit enter if

play05:27

we click down in each row well there's

play05:29

the quad

play05:30

there's the carlota it is that

play05:33

expandable range now that's not quite

play05:36

what we want we want for example right

play05:39

here we only want quad quad so we come

play05:43

up to table dot select rows after our

play05:46

first condition we use the and logical

play05:49

test operator all lowercase and now we

play05:52

say inside table product column how many

play05:56

of you are equal to outside table

play06:00

product and now when I hit enter that is

play06:04

looking good carlota and there's our two

play06:06

quads all the way down at the bottom

play06:08

there should be just four records with

play06:10

quad now we need to just count how many

play06:13

rows there are

play06:14

before table dot select rows table dot

play06:17

and I can't remember if it's count row

play06:20

or row count and look at that I type

play06:22

table dot count and this intellisense is

play06:25

kind of working so it's row count very

play06:28

carefully open parenthesis and then at

play06:31

the end close parenthesis and enter and

play06:35

there's that expandable range coughs

play06:39

running count column now the last step

play06:41

is we need to add a data type whole

play06:43

number and now we can home close and

play06:46

load closing o2 on the existing sheet

play06:49

maybe a ch3 click OK a lot more

play06:52

difficult to do than our beautiful cell

play06:55

references and countifs but sometimes

play06:57

you need to do this very same

play06:59

calculation using M code in power query

play07:03

now if you're new to power query or M

play07:06

code check out my MSP TDA class free @

play07:10

youtube videos number 3 to 12 teach you

play07:13

everything you need to know about power

play07:15

query and M code

play07:17

[Music]

Rate This

5.0 / 5 (0 votes)

相关标签
パワークエリランニングカウントデータ分析エクセルデータ変換Mコードデータタイプテーブル操作データロードYouTubeチュートリアル
您是否需要英文摘要?