Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Calculate required wait time of material

Hi all,  I have the following problem. I want to caluclate some required wait time of some material.  The data shows the receipt of some qty material.  The data shows the dispatch of some qty mate...
  • v-karpurapud's avatar
    v-karpurapud
    1 year ago

    Hi Anonymous 

    Please try the below updated DAX:

    let
      
        Source = Table.FromRows(
            Json.Document(
                Binary.Decompress(
                    Binary.FromText("i45WMjDUAyIjAyNTJR0lE3NDQyBVlJqcmllQAmQZGhgoxeoAVZliqkrJLC5ILEnOADJNIaoMDfCqAgrjUoVpoyFRNhoYoaoyQjHLCOZ6M0xVyO4CKYsFAA==", BinaryEncoding.Base64),
                    Compression.Deflate
                )
            ),
            let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Product = _t, Booking = _t, Qty = _t]
        ),
    
        ChangedType = Table.TransformColumns(Source, {
            {"Date", each Date.FromText(_, "de-DE"), type date},
            {"Product", each Int64.From(_), Int64.Type},
            {"Booking", each _, type text},
            {"Qty", each Int64.From(_), Int64.Type}
        }),
    
        Normalized = Table.ReplaceValue(ChangedType, each [Booking], each if [Booking] = "Eingang" then "receipt" else if [Booking] = "Ausgang" then "dispatch" else [Booking], Replacer.ReplaceValue, {"Booking"}),
    
     
        Grouped = Table.Group(Normalized, {"Product"}, {
            {"Result", (grpTable) =>
                let
                    DispatchMap = Function.Invoke(
                        Record.FromList,
                        Table.ToColumns(
                            Table.TransformColumns(
                                Table.SelectRows(grpTable, each [Booking] = "dispatch")[[Qty], [Date]],
                                {{"Date", each Date.ToText(_, "yyyy-MM-dd")}}
                            )
                        )
                    ),
    
                    Gen = List.Accumulate(
                        Table.ToRows(Table.SelectRows(grpTable, each [Booking] = "receipt")[[Date], [Qty]]),
                        {{}, DispatchMap, Record.FieldNames(DispatchMap)},
                        (s, c) =>
                            [
                                la = List.Accumulate(s{2}, {c{1}, {}, s{1}}, (d, v) =>
                                    let
                                        a = Record.Field(d{2}, v),
                                        qtyUsed = List.Min({a, d{0}}),
                                        newRemainingQty = a - qtyUsed,
                                        updatedDispatchMap = if newRemainingQty > 0
                                            then Record.TransformFields(d{2}, {{v, each newRemainingQty}})
                                            else Record.RemoveFields(d{2}, v)
                                    in
                                        {
                                            d{0} - qtyUsed,
                                            d{1} & {{c{0}, Date.FromText(v), qtyUsed, Duration.Days(Date.FromText(v) - c{0})}},
                                            updatedDispatchMap
                                        }
                                ),
                                result = {s{0} & la{1}, la{2}, Record.FieldNames(la{2})}
                            ][result]
                    ),
    
                    Output = Table.FromRows(Gen{0}, type table [Receipt = date, Booking = date, Qty = number, Days = number]),
                    WithProduct = Table.AddColumn(Output, "Product", each grpTable{0}[Product])
                in
                    WithProduct
            }
        }),
    
        Combined = Table.Combine(Grouped[Result])
    in
        Combined



    I have attached a snapshot for reference:



    I hope this meets your requirement. If this post helps , kindly, mark it as Accepted Solution.

    Thank You!