0
$Export_ByDates = $GetMyImport | Group-Object Ddate, LastName | ForEach-Object {

        if($_.Group | Where Ddate -eq $GetDates) {
            New-Object PSObject -Property @{
                LastName = ($_.Group.LastName -join ',')
                Date = ($_.Group.Ddate -join ',')
                Sum = ($_.Group | Measure-object Donate -sum).Sum
        }
    }
} | Sort-Object Sum -Descending
$Export_ByDates | Select -Property Date, LastName, Sum -First 1 | Export-Csv $Export_ByDater -NoTypeInformation

This is working great; however, I stumbled across 3 donation values that are the same and are the highest donation. My question is, how do I get it to display all three records if they're the same and are the highest number?

Records:

Ddate | LastName | Donate
12/25/2021 | Lee | 100
12/25/2021 | Frank | 50
12/25/2021 | Macy | 100
12/25/2021 | Lee | 20
12/25/2021 | Mark | 120
12/25/2021 | Jackson | 120

1 Answer 1

1

You can store the maximum sum value in a variable and then use that to query the object. Replacing the last line in your code sample with the following should work:

$maxsum = $Export_ByDates | 
Select -ExpandProperty Sum -First 1 

$Export_ByDates | 
Where-Object {$_.Sum -eq $maxsum} | 
Export-Csv $Export_ByDater -NoTypeInformation
Sign up to request clarification or add additional context in comments.

4 Comments

Using backticks as line continuation characters is considered very bad style. And it's not even needed since you can have a line break after the pipe symbol. ;-)
why didn't I think of that lolz. Thanks, it worked!
thanks @Olaf! - I've edited the question to remove
@mjsqu Cool. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.