寻求有关业务订单假设的一些建议。根据附图,一个人订购了一些手机和一些配件。我希望根据每个订单 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 种或更多配件如何继续。非常感谢任何帮助。
这可以使用 Windows Excel 2010+ 和 Excel 365(Windows 或 Mac)中提供的 Power Query 来完成
使用 Power Query
Data => Get&Transform => from Table/Range
或 from within sheet
Home => Advanced Editor
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"