2

我有一个这样的csv:

MPN,Per_Pallet,Customer_Order,Customer_Order_Date,Backordered_by_Pallet,Reserved_Sum
501,116.82,12055,4/28/2021,3.18,1.02
501,116.82,12421,6/7/2021,2.36,1.02
501,116.82,12424,6/7/2021,3.91,1.02
2243,30,12014,4/26/2021,1.4,1
2243,30,12425,6/7/2021,4.8,1
2243,30,12817,7/21/2021,0.4,1
2243,30,13359,9/29/2021,0.6,1
2435,50.22,12014,4/26/2021,1,2
2435,50.22,13311,9/24/2021,1.14,2
218,40,13236,9/15/2021,3,5
218,40,13382,10/4/2021,3,5
7593,64,12670,7/2/2021,5,5
484,8,12582,6/22/2021,0.38,2
484,8,12798,7/16/2021,1.38,2
484,8,13255,9/18/2021,1,2
484,8,13288,9/22/2021,1,2
5647,87,13304,9/23/2021,0.01,1

我需要按 MPN 列分组,然后首先检查最早的订单,看看 Backordered_by_Pallet 是否大于或等于 Reserved_Sum。

如果是 -cge,则只显示该组的那一行。如果不是,则检查下一个订单和第一个订单是否是并显示它们,依此类推。直到 backored 总数大于 Reserved_Sum

这就是我脑海中的样子:

look at oldest order first for matching MPN
    if oldest orders Backordered > Reserved Sum
        Then only display oldest order
    Else if oldest order + second oldest order > Reserved Sum
            then display both orders
        Else If Less Than, Add Next Order etc

预期输出:

MPN,Per_Pallet,Customer_Order,Customer_Order_Date,Backordered_by_Pallet,Reserved_Sum
501,116.82,12055,4/28/2021,3.18,1.02
2243,30,12014,4/26/2021,1.4,1
2435,50.22,13311,9/24/2021,1.14,2
218,40,13236,9/15/2021,3,5
218,40,13382,10/4/2021,3,5
7593,64,12670,7/2/2021,5,5
484,8,12582,6/22/2021,0.38,2
484,8,12798,7/16/2021,1.38,2
484,8,13255,9/18/2021,1,2
5647,87,13304,9/23/2021,0.01,1

我有不同的工作,但我不知道如何把它们放在一起:

如果它更大或不是很容易返回:

$Magic | ForEach-Object {
        If ($_.Backordered_by_Pallet -cge $_.Reserved_Sum) {$_}
        Else {"Nothing To Order"}
        }

我已经尝试通过添加一个组

$Magic | Group-Object MPN | ForEach-Object {
        If ($_.group.Backordered_by_Pallet -cge $_.group.Reserved_Sum) {$_}
        Else {"Nothing_Left_To_Order"}
        } 

但这会显示整个组或什么都没有,我不确定如何将它们组合在一起,更不用说如何在需要时添加前一行的数量。

我相信我需要为每个深度做几层,所以我对 MPN 进行分组,为那个 mpn 创建一个数组,然后为该数组上的每个创建一个数组(按最旧排序)(不知道如何将前一行拉到add) 然后只导出结果,然后循环移动到下一个组,依此类推。

像这样?我知道这不是真的,我就是想不通

 $Magic_Hash = $Magic_File | Group-Object -Property MPN -AsHashTable | Sort $_.group.Customer_Order_Date
        
        ForEach ($item in $Magic_Hash) {
           If ($item.group.Backordered_by_Pallet -cge $_.group.Reserved_Sum) {$_}
        Elseif ($item.group.Backordered_by_Pallet + $item.group.Backordered_by_Pallett["2nd oldest order"] -cge $_.group.Reserved_Sum) {$_}
        else {"Nothing_Left"}
        } 
        ```
Thank you so much for all your help this community is amazing
4

2 回答 2

1

第一步是根据MPN列/属性对记录进行分组,所以让我们首先使用恰当命名的Group-Objectcmdlet 进行分组:

$records = @'
MPN,Per_Pallet,Customer_Order,Customer_Order_Date,Backordered_by_Pallet,Reserved_Sum
501,116.82,12055,4/28/2021,3.18,1.02
501,116.82,12421,6/7/2021,2.36,1.02
501,116.82,12424,6/7/2021,3.91,1.02
2243,30,12014,4/26/2021,1.4,1
2243,30,12425,6/7/2021,4.8,1
2243,30,12817,7/21/2021,0.4,1
2243,30,13359,9/29/2021,0.6,1
2435,50.22,12014,4/26/2021,1,2
2435,50.22,13311,9/24/2021,1.14,2
218,40,13236,9/15/2021,3,5
218,40,13382,10/4/2021,3,5
7593,64,12670,7/2/2021,5,5
484,8,12582,6/22/2021,0.38,2
484,8,12798,7/16/2021,1.38,2
484,8,13255,9/18/2021,1,2
484,8,13288,9/22/2021,1,2
5647,87,13304,9/23/2021,0.01,1
'@ |ConvertFrom-Csv

$groups = $records |Group-Object MPN

现在它们都正确分组在一起,我们可以开始遍历每个组,按日期/订单号对关联的记录进行排序,然后输出第一个匹配条件的记录:

foreach($group in $groups){
  # sort records by order number
  $recordsInGroup = $group.Group |Sort-Object Customer_Order

  # filter records based on the criteria, output only the first 1
  $recordsInGroup |Where-Object { +$_.Backordered_by_Pallet -ge $_.Reserved_Sum } |Select-Object -First 1
}

过滤器中的前面+将PowerShell 将值转换为 a ,确保正确的数字比较$_.Backordered_by_PalletWhere-Object[double]$_.Reserved_Sum

于 2021-10-21T15:48:12.160 回答
1

代码本身非常糟糕,但我相信这是可行的。我添加了评论以或多或少地理解思考过程。

需要注意的一点是,"Nothing To Order"没有位置或未定义您希望如何显示它,因为它是一个string,如果您需要显示此信息,它可能必须插入其中一个单元格或创建一个新列这个。

@'
MPN,Per_Pallet,Customer_Order,Customer_Order_Date,Backordered_by_Pallet,Reserved_Sum
501,116.82,12055,4/28/2021,3.18,1.02
501,116.82,12421,6/7/2021,2.36,1.02
501,116.82,12424,6/7/2021,3.91,1.02
2243,30,12014,4/26/2021,1.4,1
2243,30,12425,6/7/2021,4.8,1
2243,30,12817,7/21/2021,0.4,1
2243,30,13359,9/29/2021,0.6,1
2435,50.22,12014,4/26/2021,1,2
2435,50.22,13311,9/24/2021,1.14,2
218,40,13236,9/15/2021,3,5
218,40,13382,10/4/2021,3,5
7593,64,12670,7/2/2021,5,5
484,8,12582,6/22/2021,0.38,2
484,8,12798,7/16/2021,1.38,2
484,8,13255,9/18/2021,1,2
484,8,13288,9/22/2021,1,2
5647,87,13304,9/23/2021,0.01,1
'@ |ConvertFrom-Csv |
Group-Object MPN | ForEach-Object {
    
    $skip = $false
    [double]$backorderSum = 0

    # Sort by Customer_Order_Date, oldest will be first in line
    foreach($line in $_.Group | Sort-Object {[datetime]$_.Customer_Order_Date})
    {
        if($skip)
        {
            continue
        }

        # If Backordered_by_Pallet is greater than or equal to Reserved_Sum
        if([double]$line.Backordered_by_Pallet -ge [double]$line.Reserved_Sum)
        {
            # Display this line and skip the rest
            $skip = $true
            $line
        }
        else
        {
            # Display this line
            $line

            # Keep a record of previous Values
            $backorderSum += $line.Backordered_by_Pallet

            # Until this record is greater than or equal to Reserved_Sum
            if($backorderSum -ge [double]$line.Reserved_Sum)
            {
                # Skip the rest when this condition is met
                $skip = $true
            }
        }
    }
} | FT

输出

MPN  Per_Pallet Customer_Order Customer_Order_Date Backordered_by_Pallet Reserved_Sum
---  ---------- -------------- ------------------- --------------------- ------------
501  116.82     12055          4/28/2021           3.18                  1.02        
2243 30         12014          4/26/2021           1.4                   1           
2435 50.22      12014          4/26/2021           1                     2           
2435 50.22      13311          9/24/2021           1.14                  2           
218  40         13236          9/15/2021           3                     5           
218  40         13382          10/4/2021           3                     5           
7593 64         12670          7/2/2021            5                     5           
484  8          12582          6/22/2021           0.38                  2           
484  8          12798          7/16/2021           1.38                  2           
484  8          13255          9/18/2021           1                     2           
5647 87         13304          9/23/2021           0.01                  1   
于 2021-10-21T22:47:36.963 回答