Ben Donachie
Ben Donachie

Reputation: 35

How to combine/group total of two properties

I've played around with group objects with a few variations with no luck.

How would I go about summing the total row count by each directory?

Rowcount Directory                  
-------- ---------                  
      23 Collateral        
     289 Collateral        
      14 Collateral        
      63 Collateral        
      21 Credit            
     398 CreditValuation   
     101 CreditValuation   
     116 CreditValuation   
      36 EmirCollateral    
       3 EmirFx            
      35 EmirFXValuation   
      18 EmirRates         
      20 EmirRates         
     397 EmirRatesValuation

Upvotes: 0

Views: 108

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

You could use the Group-Object cmdlet to group your records based on the Directory property. Then you can calculate the sum using the Measure-Object cmdlet. Here is an example:

$sample | Group-Object Directory | ForEach-Object { 
    [PSCustomObject]@{
        Directory = $_.Group.Directory[0]
        Rowcount  = ($_.Group.Rowcount | Measure-Object -sum).Sum
    }
}

Upvotes: 1

Related Questions