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
mtrevisiol
Helper IV
Helper IV

Month by month calculation of quantity in stock

Hi everyone. 

I would like to create a table that shows me, for each item, and for each month of the year, the change in quantity in stock and the resulting stock, starting with the quantity in stock on January 1st 2021.

 

This is the table I've got:

 

ItemYearMonthRunningQtyQty
Item A2021110-2
Item A20212 5
Item A20213 7
Item B20211150
Item B20213 -10
Item C2021100
Item C20212 5
Item C20213 -2

 

  • "Qty" represents the entered quantity entering or exting the warehouse for each item, for each month
  • "Running qty" represents the resulting stock, after entry and exit, at the end of each month

 

And this is the ideal result:

 

ItemYearMonthRunningQtyQty
Item A2021110-2
Item A2021285
Item A20213137
Item B20211150
Item B2021315-10
Item C2021100
Item C2021205
Item C202135-2

 

What formula can I use to calculate the "RunningQty" column?

Here is the pbix file: https://www.dropbox.com/s/wu51fajcuqn3chh/ItemsByMonth.pbix?dl=0

 

Thank you in advance!

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

This is exactly the solution I was searching, thank you very much!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
lbendlin
Super User
Super User

 

 

 

"Running qty" represents the resulting stock, after entry and exit, at the end of each month

 

 

 

 

That is _very_ confusing.  Can you please explain that again?  Not following your logic here. Did you mix up the column names?

 

Here is a very crude version of a Power Query script that is based on your current sample data in the PBIX.  It needs some modifications for more complete sample data.

 

let
    Origine = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixJzVVwVNJRMjIwMgRSYGwAJHSNlGJ1MOWNgBiITLFKGkMkzRGSTmgmmwIJA6zSUL26hkjSzqi6DVA1O+NzlTOmyUAPxQIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, Year = _t, Month = _t, Qty = _t, #"Running Qty" = _t]),
    #"Modificato tipo" = Table.TransformColumnTypes(Origine,{{"Item", type text}, {"Year", Int64.Type}, {"Month", Int64.Type}, {"Qty", Int64.Type}, {"Running Qty", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Modificato tipo", "Index", 0, 1, Int64.Type),
    #"Replaced Value" = Table.ReplaceValue(#"Added Index",each [Qty],each if [Qty]=null then #"Added Index"[Qty]{[Index]-1}+#"Added Index"[Running Qty]{[Index]-1} else [Qty],Replacer.ReplaceValue,{"Qty"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",each [Qty],each if [Qty]=null then #"Replaced Value"[Qty]{[Index]-1}+#"Replaced Value"[Running Qty]{[Index]-1} else [Qty],Replacer.ReplaceValue,{"Qty"})
in
    #"Replaced Value1"

 

Here is a more complete version

let
    Origine = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixJzVVwVNJRMjIwMgRSYGwAJHSNlGJ1MOWNgBiITLFKGkMkzRGSTmgmmwIJA6zSUL26hkjSzqi6DVA1O+NzlTOmyUAPxQIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, Year = _t, Month = _t, Qty = _t, #"Running Qty" = _t]),
    #"Modificato tipo" = Table.TransformColumnTypes(Origine,{{"Item", type text}, {"Year", Int64.Type}, {"Month", Int64.Type}, {"Qty", Int64.Type}, {"Running Qty", Int64.Type}}),
    cm = (tb)=>
      let
          #"Added Index" = Table.AddIndexColumn(tb, "Index", 0, 1, Int64.Type),
          #"Replaced Value" = Table.ReplaceValue(#"Added Index",each [Qty],each if [Index]=0 then [Qty] else List.Accumulate({0..[Index]-1},#"Added Index"[Qty]{0},(state,current)=>state+#"Added Index"[Running Qty]{current}),Replacer.ReplaceValue,{"Qty"})
       in
      #"Replaced Value",
    #"Grouped Rows" = Table.Group(#"Modificato tipo", {"Item"}, {{"Count", each _, type table [Item=nullable text, Year=nullable number, Month=nullable number, Qty=nullable number, Running Qty=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each cm([Count])),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Item", "Year", "Month", "Qty", "Running Qty", "Index"}, {"Item", "Year", "Month", "Qty", "Running Qty", "Index"})
in
    #"Expanded Custom"

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.