Created
January 21, 2012 07:10
-
-
Save andyoakley/1651859 to your computer and use it in GitHub Desktop.
Simple PivotTable in Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rotates a vertical set similar to an Excel PivotTable | |
# | |
# Given $data in the format: | |
# | |
# Category Activity Duration | |
# ------------ ------------ -------- | |
# Management Email 1 | |
# Management Slides 4 | |
# Project A Email 2 | |
# Project A Research 1 | |
# Project B Research 3 | |
# | |
# with $keep = "Category", $rotate = "Activity", $value = "Duration" | |
# | |
# Return | |
# | |
# Category Email Slides Research | |
# ---------- ----- ------ -------- | |
# Management 1 4 | |
# Project A 2 1 | |
# Project B 3 | |
$rotate = "Activity" | |
$keep = "Category" | |
$value = "Duration" | |
$pivots = $data | | |
select -unique $rotate | | |
foreach { $_.Activity} $data | | |
group $keep | | |
foreach { | |
$group = $_.Group | |
$row = new-object psobject $row | add-member NoteProperty $keep $_.Name | |
foreach ($pivot in $pivots) { $row | add-member NoteProperty $pivot ($group | where { $_.$rotate -eq $pivot } | measure -sum $value).Sum } | |
$row | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think i found another version that was enhanced from above, it was intended to support multiple fields but i found several problems. My final version supports multiple row and column fields, handles null values and also supports pipeline input. Unfortunately i can't credit the original author as i can't find that original post which I enhanced.