Forum Discussion
praveen raju
Sep 10, 2020Copper Contributor
Query to find count and sum
Here is my data-table Looks like......... Category Annual savings ------- ------------------ Category-1. 1 Category-2. 3 Category-1 5 Category-1 ...
jleechpe
Sep 24, 2020Copper Contributor
praveen raju Slightly delayed (found my way to the tech community via Ignite) but if only for any future searches.
You'll want to use Group-Object to collect the values by category (You have an extra period on the first 2 entries which would skew the results). Assuming you had the data as a csv and imported it to $data you can use the below
$data | Group-Object Category | ForEach-Object {
[pscustomobject]@{
Category = $_.name
'Sum(savings)' = ($_.group.'annual savings' | Measure-object -sum).sum
'Count(Category)' = $_.count
}
}