Forum Discussion
John_Dodo
May 03, 2023Brass Contributor
retrieve rows with highest values in from array
Hi, I'm struggling a bit with something that should be easy ... I import a CSV and then I want to retrieve the rows with highest value for different "families". Ex: col1;col2;col3;col4 dateA;...
LainRobertson
May 15, 2023Silver Contributor
I don't actually understand the data relationship between table 1 and 2 - specifically, I don't know where "family1", "family2", etc. are coming from in table 2, col2.
For the sake of providing an example, I'm going to assume that table 1, col3 ("son1", "son2", etc.) equates to "family1", "family2", etc in table 2, col2.
If that's not the case, then you'll need to better-explain how to get from table 1 to table 2.
If it is the case, then I've used the following example along with your data from table 1 to produce the output below the script.
Script
$Hashcode = 0;
$NewHashCode;
$LastSon = "";
$Index = 0;
Import-Csv -Path D:\Data\Temp\Forum\forums.txt -Delimiter ";" |
Sort-Object -Property col3, col2, @{ e = "col4"; d = $true} |
ForEach-Object {
if ($HashCode -ne ($NewHashCode = [string]::Concat($_.col3, $_.col2).GetHashCode()))
{
$HashCode = $NewHashCode;
if ($LastSon -ne $_.col3)
{
$LastSon = $_.col3;
$Index++;
}
[PSCustomObject] @{
col2 = "family$Index";
col3 = $_.col2;
col4 = $_.col4;
}
}
}Sample output
Cheers,
Lain