-
-
Save DennisL68/c8cad4727080f69a9a712caacf5e36fd 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
function Invoke-Pivot { | |
param ( | |
$data, | |
[string]$rotate, # Bits along the top | |
[string]$keep, # Those along the side | |
[string]$value # What to total | |
) | |
# Create variable to store the output | |
$NewRows = @() | |
# Fields of interest | |
# $rotate = "Activity" # Bits along the top | |
# $keep = "Category" # Those along the side | |
# $value = "Duration" # What to total | |
# Find the unique "Rotate" [top row of the pivot] values and sort ascending | |
$pivots = $data | select -unique $rotate | foreach { $_.$rotate} | Sort-Object | |
# Step through the original data... | |
# for each of the "Keep" [left hand side] find the Sum of the "Value" for each "Rotate" | |
$data | | |
group $keep | | |
foreach { | |
$group = $_.Group | |
# Create the data row and name it as per the "Keep" | |
$row = new-object psobject | |
$row | add-member NoteProperty $keep $_.Name | |
foreach ($pivot in $pivots) { # Cycle through the unique "Rotate" values and get the sum | |
$row | add-member NoteProperty $pivot ($group | where { $_.$rotate -eq $pivot } | measure -sum $value).Sum | |
} | |
# Add the total to the row | |
$row | add-member NoteProperty Total ($group | measure -sum $value).Sum | |
# Add the row to the collection | |
$NewRows += $row | |
} | |
# Do something with the pivot rows | |
return $NewRows | |
<# | |
.SYNOPSIS | |
Transforms rows and columns of a PSObject or other named object. | |
.DESCRIPTION | |
Long description | |
.EXAMPLE | |
PS C:\> <example usage> | |
Explanation of what the example does | |
.INPUTS | |
Inputs (if any) | |
.OUTPUTS | |
Output (if any) | |
.NOTES | |
############################################################################# | |
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 Total | |
---------- ----- ------ ----- | |
Management 1 4 5 | |
Project A 2 1 3 | |
Project B 3 3 | |
############################################################################# | |
Changes | |
========# | |
Who When Details | |
------------ ---------- ----------------------------------------------- | |
dennis 2019-10-22 Converted to advanced script | |
aphalon 2015-07-23 1. Added $NewRows variable to hold the output | |
2. Unique values were hardcoded for the example | |
-> changed to be the variable fields | |
3. Sorted $pivots | |
4. Added a Total column | |
5. Commented it a little :) | |
############################################################################# | |
#> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment