Excel - 根据与其一起订购的配件更新商品描述

问题描述 投票:0回答:1

寻求有关业务订单假设的一些建议。根据附图,一个人订购了一些手机和一些配件。我希望根据每个订单 ID 的数量和已订购的配件来更新手机描述。

每个订单ID和手机S/N都是唯一的,并且只有手机才有S/N。配件数量不会超过手机数量。

Order ID   Phone S/N    Description    Quantity Updated Description
O-001      P-001    Samsung S22    1        Samsung S22 + Plastic
O-001      P-002    Samsung S22    1        Samsung S22 + Glass
O-001           Plastic Protector  1    
O-001           Glass Protector    1    
O-002      P-003    Samsung S22    1        Samsung S22 + Plastic
O-002      P-004    Samsung S22    1        Samsung S22 + Plastic
O-002           Plastic Protector  2    
O-003      P-005    Samsung S22    1            Samsung S22
O-003      P-006    Samsung S22    1        Samsung S22 + Glass
O-003           Glass Protector    1    

目前,我有 0 或 1 种配件的工作公式,但不知道如果有 2 种或更多配件如何继续。非常感谢任何帮助。

excel excel-formula
1个回答
0
投票

这可以使用 Windows Excel 2010+ 和 Excel 365(Windows 或 Mac)中提供的 Power Query 来完成

使用 Power Query

  • 选择数据表中的某个单元格
  • Data => Get&Transform => from Table/Range
    from within sheet
  • PQ 编辑器打开时:
    Home => Advanced Editor
  • 记下第 2 行中的表名称
  • 将下面的 M 代码粘贴到您所看到的位置
  • 将第 2 行中的表名称更改回最初生成的名称。
  • 阅读评论并探索
    Applied Steps
    以了解算法
let
    Source = Excel.CurrentWorkbook(){[Name="Orders"]}[Content],

//Repeat rows per quantity
    #"Repeat Rows" = Table.AddColumn(Source,"Repeats", each Table.Repeat(Table.FromRecords({_}),[Quantity]),
    type table[Order ID=text, #"Phone S/N"=text, Description=text, Quantity=Int64.Type]),
    #"Removed Columns" = Table.RemoveColumns(#"Repeat Rows",Table.ColumnNames(Source)),
    #"Expanded Repeats" = Table.ExpandTableColumn(#"Removed Columns", "Repeats", 
        Table.ColumnNames(Source)),

//Group by Order ID, then process group separately
    #"Grouped Rows" = Table.Group(#"Expanded Repeats", {"Order ID"}, {
        {"Updated", (t)=>
            let
               
            //create updated description text strings
                phones =  Table.SelectRows(t, each [#"Phone S/N"] <> null),
                accessories = Table.SelectRows(t, each [#"Phone S/N"]=null),
                updated = List.Transform(List.Zip({phones[Description],accessories[Description]}),each Text.Combine(_," + ")),

            //Add a column of the Table with the updated description column added
                #"Add Column" = 
                    Table.FromColumns(
                        Table.ToColumns(t)
                        & {updated},{"Order ID", "Phone S/N", "Description","Quantity", "Updated Description"} )
            in 
                #"Add Column", type table [Order ID=nullable text, #"Phone S/N"=nullable text, 
                    Description=nullable text, Quantity=nullable number, Updated Description = nullable text]}}),

//Re-expand the table and remove the duplicated rows
    #"Expanded Updated" = Table.ExpandTableColumn(#"Grouped Rows", "Updated", 
        {"Phone S/N", "Description", "Quantity", "Updated Description"}),
    #"Removed Duplicates" = Table.Distinct(#"Expanded Updated")
        
in
    #"Removed Duplicates"

© www.soinside.com 2019 - 2024. All rights reserved.