分发产品股票数量。表到多行订单表

发布于 2025-02-02 05:03:05 字数 631 浏览 2 评论 0原文

我有库存桌&我想将可用的库存数量分配到订单表中,其中相同的产品可用多个订单...最终结果作为功率查询或电源枢轴中的结果表所需的结果...

I have Stock Table & I want to distribute the available stock qty to Order table where same products multiple orders available...Final result required as Result table in Power Query or Power Pivot...

enter image description here

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年九夏 2025-02-09 05:03:05

如果我了解您在做什么,这是使用电源查询M代码来执行此操作的一种方法:
算法在代码注释中概述
我更改了数据以允许更多变化

“

let

//Read in the Stock and Order Tables
//Change these four lines as appropriate for your data sources
//NOTE: You can change data types for Order No and Product ID to "text" if that's what they really are
    Source = Excel.CurrentWorkbook(){[Name="StockTBL"]}[Content],
    Stock = Table.TransformColumnTypes(Source,{{"Product ID", Int64.Type}, {"Product Stock", Int64.Type}}),
    Source2 = Excel.CurrentWorkbook(){[Name="OrderTBL"]}[Content],
    Orders = Table.TransformColumnTypes(Source2,{
        {"Order No", Int64.Type}, {"Customer", type text}, {"Product ID", Int64.Type}, {"Order Qty", Int64.Type}}),

//Group Orders table by "Product ID"
// for each group, calculate the remaining stock quantity after each order
//    Running Total of all QTY's subtracted from the Product Stock quantity in the Stock table
    #"Grouped Rows" = Table.Group(Orders, {"Product ID"}, {
        {"Order RT", (t)=> 
            Table.FromColumns(
                Table.ToColumns(t) & {
                    List.Generate(
                        ()=>[rt=t[Order Qty]{0},
                        rm=List.Sum(Table.SelectRows(Stock, each [Product ID]=t[Product ID]{0})[Product Stock]) - t[Order Qty]{0}, 
                            idx=0],
                        each [idx] < Table.RowCount(t),
                        each [rt = [rt] + t[Order Qty]{[idx]+1},
                        rm=List.Sum(Table.SelectRows(Stock, each [Product ID]=t[Product ID]{0})[Product Stock]) - ([rt] + t[Order Qty]{[idx]+1}),
                                idx = [idx]+1],
                        each [rm])},{"Order No.", "Customer","Product ID","Order Qty","Remaining Stock"}),type table}
       }),

//Remove unneeded column and expand the grouped table
//then set data types
    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Product ID"}),
    #"Expanded Order RT" = Table.ExpandTableColumn(#"Removed Columns", 
        "Order RT", 
        {"Order No.", "Customer", "Product ID", "Order Qty", "Remaining Stock"}, 
        {"Order No.", "Customer", "Product ID", "Order Qty", "Remaining Stock"}
        ),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Order RT",{{"Order No.", Int64.Type}, {"Customer", type text}, {"Product ID", Int64.Type}, {"Order Qty", Int64.Type}, {"Remaining Stock", Int64.Type}}),

//Add custom column to calculate Required Stock Qty depending on Order vs Remaining
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Required Stock Qty", each 
        if [Remaining Stock] >=0 then [Order Qty] 
        else List.Max({[Order Qty]+[Remaining Stock],0}), Int64.Type),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Remaining Stock"})
in
    #"Removed Columns1"

”在此处输入图像说明”

If I understand what you are doing, here's one way to do this using Power Query M Code:
Algorithm is outlined in the code comments
I changed the data a bit to allow for more variations

enter image description here

let

//Read in the Stock and Order Tables
//Change these four lines as appropriate for your data sources
//NOTE: You can change data types for Order No and Product ID to "text" if that's what they really are
    Source = Excel.CurrentWorkbook(){[Name="StockTBL"]}[Content],
    Stock = Table.TransformColumnTypes(Source,{{"Product ID", Int64.Type}, {"Product Stock", Int64.Type}}),
    Source2 = Excel.CurrentWorkbook(){[Name="OrderTBL"]}[Content],
    Orders = Table.TransformColumnTypes(Source2,{
        {"Order No", Int64.Type}, {"Customer", type text}, {"Product ID", Int64.Type}, {"Order Qty", Int64.Type}}),

//Group Orders table by "Product ID"
// for each group, calculate the remaining stock quantity after each order
//    Running Total of all QTY's subtracted from the Product Stock quantity in the Stock table
    #"Grouped Rows" = Table.Group(Orders, {"Product ID"}, {
        {"Order RT", (t)=> 
            Table.FromColumns(
                Table.ToColumns(t) & {
                    List.Generate(
                        ()=>[rt=t[Order Qty]{0},
                        rm=List.Sum(Table.SelectRows(Stock, each [Product ID]=t[Product ID]{0})[Product Stock]) - t[Order Qty]{0}, 
                            idx=0],
                        each [idx] < Table.RowCount(t),
                        each [rt = [rt] + t[Order Qty]{[idx]+1},
                        rm=List.Sum(Table.SelectRows(Stock, each [Product ID]=t[Product ID]{0})[Product Stock]) - ([rt] + t[Order Qty]{[idx]+1}),
                                idx = [idx]+1],
                        each [rm])},{"Order No.", "Customer","Product ID","Order Qty","Remaining Stock"}),type table}
       }),

//Remove unneeded column and expand the grouped table
//then set data types
    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Product ID"}),
    #"Expanded Order RT" = Table.ExpandTableColumn(#"Removed Columns", 
        "Order RT", 
        {"Order No.", "Customer", "Product ID", "Order Qty", "Remaining Stock"}, 
        {"Order No.", "Customer", "Product ID", "Order Qty", "Remaining Stock"}
        ),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Order RT",{{"Order No.", Int64.Type}, {"Customer", type text}, {"Product ID", Int64.Type}, {"Order Qty", Int64.Type}, {"Remaining Stock", Int64.Type}}),

//Add custom column to calculate Required Stock Qty depending on Order vs Remaining
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Required Stock Qty", each 
        if [Remaining Stock] >=0 then [Order Qty] 
        else List.Max({[Order Qty]+[Remaining Stock],0}), Int64.Type),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Remaining Stock"})
in
    #"Removed Columns1"

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文