Have a question on powershell and hopeful will get some hint to get me going in the right direction.
I have a variable which has data stored in it for example $A
will output
Year Cost
---- ----
1997 12
1998 42
1999 6
2000 12
2001 14
I want to add another column here, lets say Trend and compute the difference between the years for example
Year Cost Trend
1997 12 0
1998 42 30 (42-12)
1999 6 -36 (6-42)
Any hints will be really helpful
You could loop through each row comparing it to the previous and then create a new object from the results like so:
$result = 0..($a.count - 1) | % {
if($_ -eq 0) {$t = $a[$_].Cost} else {$t = $a[$_].Cost - $a[$_ - 1].Cost}
New-Object PSObject -property @{
Year = $a[$_].Year
Cost = $a[$_].Cost
Trend = $t
}
}
$result
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.