Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Stubrg
Helper I
Helper I

Subtracting Orders from Grouped Stock whilst adding in Stock Delivery's "Help Please!!"

Hi I am really struggling here, I am trying to create a new Column called "Running Stock" I can easily create this in Excel but Power Query is causing me a headache!

The calculation is needed by group and date order, The first calc subtracts the "Forecast qty" from the "Stock Total" the following calculation needs to subtract the "Forecast qty" from the first value of the "Running Stock" + any value in the "On order BF required date"

This then needs to reset at the next group.

I have tried to refer to the previous row using grouped index columns but cant work out how to start this calc after the first calc from a different column? and keeping it in date order?

Below is how I want the report to look:

 

Help.PNG

I have tried to show with colour coding what cells i need to subrtact and add together.

Any assitance with this would be greatly apprecaited

1 REPLY 1
latimeria
Solution Specialist
Solution Specialist

Hi @Stubrg ,

 

You can try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdG9CsIwEAfwd8lcuK80TVYfwMWxdBI3QRDF1/cu1jZpCoUcyfDj+r/rODokQA+MLK5zl+vz9tGbtAYtzPfUKeMdJi3zgP2WDQ2jBBiOGQsQNtksXNLitR2DHnW+dNI4mzW7/sBJMcXpcX/pFbT8nM4vs4aNsh45Ba+OfJFudtG299/yj3FqmXWWWLHqf53fpsi+JTiH00d2EfRUrt9hul+imtkrLC3NTV8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Due Date" = _t, Part = _t, #"Forecast qty" = _t, #"Stock Total" = _t, #"On order BF required date" = _t, #"Quantity in Store" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Forecast qty", Int64.Type}, {"Stock Total", Int64.Type}, {"On order BF required date", Int64.Type}, {"Quantity in Store", Int64.Type}}),
    #"Grouped Rows" = Table.Group(
        #"Changed Type", 
        {"Part"}, 
        {
            {"AllRows", each 
                let
                    ListForecast = List.Buffer([Forecast qty]),
                    ListSOrderBF = List.Buffer([On order BF required date]),
                    RunningStock = 
                        List.Generate(
                            ()=> [RT = [Stock Total]{0} - ListForecast{0} , Counter = 0 ], /* record first row */
                            each [Counter] < List.Count(ListForecast),
                            each [
                                RT = List.Sum({[RT], -ListForecast{[Counter]+1}, ListSOrderBF{[Counter]+1}}),
                                Counter = [Counter] +1
                            ],  /* record next row */
                            each [RT] /*select RT as result*/
                        )
                in /* transform table to columns, add new column and back to table */
                    Table.FromColumns(
                        Table.ToColumns(_) & { Value.ReplaceType(RunningStock, type {Int64.Type})},
                        Table.ColumnNames(_) & {"Running Stock"}
                    )
            }
        }
    ),
    Expanded = Table.Combine(#"Grouped Rows"[AllRows])
in
    Expanded

 

Result:

latimeria_0-1680446513318.png

You can find explantion here Compute a Running Total by Category in Power Query - BI Gorilla

and here Quickly Create Running Totals in Power Query • My Online Training Hub

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors