月ごとのデータをよこ軸に集計する方法
たまに下記のような、月ごとのデータを
| name | mouth | count |
|---|---|---|
| Tom | 5 | 2 |
| Jhon | 6 | 3 |
| Tom | 6 | 3 |
下記のように変換して、
月の遷移を確認したくなることがある。
| name | 5 | 6 |
|---|---|---|
| Tom | 2 | 3 |
| Jhon | 0 | 6 |
そういう場合は、Group-Objectを使うと便利
PS > $table = [PSCustomObject]@{name="Jhon";mouth=5;count=2},
[PSCustomObject]@{name="Tom" ;mouth=6;count=3},
[PSCustomObject]@{name="Jhon";mouth=6;count=3}
PS > $table
name mouth count
---- ----- -----
Jhon 5 2
Tom 6 3
Jhon 6 3
PS > $table | Group-Object -Property name |
% {
$a = [PSCustomObject]@{name=$_.name}
$_.Group | % {
$a | Add-Member -NotePropertyMembers @{$_.mouth=$_.count}
}
$a
}
name 5 6
---- - -
Jhon 2 3
Tom 3