Forum Discussion
Running Stock Total based on forecasts and delivery's
- 3 years ago
By the way, I see now that your error message is due to the fact that you added the Part column to the table type specification incorrectly. The columns should be in the form: [...Column Name = type...]. You show [...#"Part", type text...] which is adding two columns, one with the name "Part", and the second named "type text". See my code below for proper method.
Below is code that should work to give you a running stock qty per part number.
Here is the table with the Part column added:
Code:
let //Change next line to reflect actual data source Source = Excel.CurrentWorkbook(){[Name="Table16"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"Part", type text}, {"Forecast qty RT - MAR", Int64.Type}, {"Stock Total", Int64.Type}, {"On order (Bf required date)", Int64.Type}, {"J24 Quantity", Int64.Type}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Part"}, { {"Tables", (t)=> let //lists to compute running stock total fq = t[#"Forecast qty RT - MAR"], oo = t[#"On order (Bf required date)"], #"rtList" = List.Generate( ()=>[rt=t[Stock Total]{0} - fq{0} + oo{0}, idx=0], each [idx] < List.Count(fq), each [rt= if oo{[idx]} <> oo{[idx]+1} then [rt] - fq{[idx]+1} + oo{[idx]+1} else [rt] - fq{[idx]+1}, idx=[idx]+1], each [rt]), //combine running total column with original table #"Result" = Table.FromColumns( {t[Part]} & {fq} & {#"rtList"} & {t[Stock Total]} & {oo} & {t[J24 Quantity]}, { "Part", "Forecast qty RT - MAR", "Running Stock", "Stock Total", "On order (Bf required date)", "J24 Quantity"}) in #"Result", type table[ #"Part" = text, #"Forecast qty RT - MAR"=Int64.Type, Running Stock=Int64.Type, Stock Total=Int64.Type, #"On order (Bf required date)"=Int64.Type, J24 Quantity=Int64.Type] }}), #"Expanded Tables" = Table.ExpandTableColumn(#"Grouped Rows", "Tables", {"Forecast qty RT - MAR", "Running Stock", "Stock Total", "On order (Bf required date)", "J24 Quantity"}) in #"Expanded Tables"Results:
Hi Thank you again for your quick response, appoligies for the screenshots below is code copy/pasted, I have added an extra column in the original spreedsheet and created a new query which i have copy/pasted the code you kindly provided.
I have then attempted to edit and add in the additonal column name, the idea is to have the Stock "Running Total" reset at each Part by using the grouping function in Power Query.
Thank you again for your continued support, i appreciate it.
let
//Change next line to reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Part", type text},
{"Forecast qty RT - MAR", Int64.Type}, {"Stock Total", Int64.Type}, {"On order (BF required date)", Int64.Type},
{"J24 Quantity", Int64.Type}}),
//lists to compute running stock total
fq = #"Changed Type"[#"Forecast qty RT - MAR"],
oo = #"Changed Type"[#"On order (BF required date)"],
#"rtList" = List.Generate(
()=>[rt=#"Changed Type"[Stock Total]{0} - fq{0} + oo{0}, idx=0],
each [idx] < List.Count(fq),
each [rt= if oo{[idx]} <> oo{[idx]+1}
then [rt] - fq{[idx]+1} + oo{[idx]+1}
else [rt] - fq{[idx]+1},
idx=[idx]+1],
each [rt]),
//combine running total column with original table
#"Result" = Table.FromColumns(
{#"Changed Type"[Part]}
& {fq}
& {#"rtList"}
& {#"Changed Type"[Stock Total]}
& {oo}
& {#"Changed Type"[J24 Quantity]},
type table[#"Part", type text,#"Forecast qty RT - MAR"=Int64.Type, Running Stock=Int64.Type, Stock Total=Int64.Type,
#"On order (Bf required date)"=Int64.Type, J24 Quantity=Int64.Type])
in
#"Result"
OK, now I see that you added a column named "Part" to one of the tables. However, I don't see where you posted a table that included that column.
Again, please post as text which can by copy/pasted.
Also, the way to have the this running stock total by group, would be to group by Part first, and then use the code I provided in a custom aggregation within the Table.Group method.
- Anonymous3 years agoNot applicable
Hi So here is the table with the added "Part" column. I thought i could just add the missing column into the code by doing the following:
So from a new Spreadsheet with this table in I added a new query, then using the original code I added in {"Part", type text}, into this line:
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Part", type text},
{"Forecast qty RT - MAR", Int64.Type}, {"Stock Total", Int64.Type}, {"On order (BF required date)", Int64.Type},
{"J24 Quantity", Int64.Type}}),and then edited this section adding in the part coulm again:
#"Result" = Table.FromColumns(
{#"Changed Type"[Part]}
& {fq}
& {#"rtList"}
& {#"Changed Type"[Stock Total]}
& {oo}
& {#"Changed Type"[J24 Quantity]},
type table[#"Part", type text,#"Forecast qty RT - MAR"=Int64.Type, Running Stock=Int64.Type, Stock Total=Int64.Type,
#"On order (Bf required date)"=Int64.Type, J24 Quantity=Int64.Type])PartForecast qty RT - MARStock TotalOn order (BF required date)J24 J24 Quantity=Int64.Type])
Thank you again for all your assistance.
PartForecast qty RT - MARStock TotalOn order (BF required date)J24 Quantity Screw 1 7 0 7 Screw 3 7 0 7 Screw 7 7 0 7 Bolt 7 7 0 7 Bolt 11 9 2 7 Bolt 13 9 2 7 Bolt 13 9 2 7 Clamp 6 4 0 4 Clamp 2 24 20 4 Clamp 8 31 7 4 Clamp 3 38 7 4 Fire Damper 10 30 0 30 Fire Damper 5 30 0 30 Fire Damper 3 60 30 30 - ronrsnfld3 years ago
Super User
The code I supplied with a "guess" for the Part column seems to work OK with your posted table.
Check it out.