I had a question from a friend this weekend how he could transpose a powershell table. I couldn't find anything working on the web, so created my own.
function transpose($a,$name,$label){ # transposing labels $props = $a."$name" | select -unique $names = ($a |select -First 1 | gm -MemberType NoteProperty | ?{$_.Name -ne $name}).Name # transposing data foreach($n in $names){ $r = [ordered]@{} foreach($p in $props){ $r."$label" = $n $r."$p" = $a | ?{$_."$name" -eq "$p"} | %{$_."$n"} } New-Object -TypeName psobject -Property $r } } $data PMI Production New Orders Sector --- ---------- ---------- ------ 9 10 -1 IT 92 -30 62 Marketing 13 36 -100 Petrol 93 4 80 Food 36 98 -67 Gold transpose -a $data -name "Sector" -label "Props" Props IT Marketing Petrol Food Gold ----- -- --------- ------ ---- ---- New Orders -1 62 -100 80 -67 PMI 9 92 13 93 36 Production 10 -30 36 4 98
No comments :
Post a Comment