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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
tzanouman
Frequent Visitor

Dynamic value replace

Hello Team, a bit of help please regarding the following issue. 

I have a table named Inventory with the columns Yearweek, Material_id, Total Demand, Total Stock in the warehouse, and Ending Stock. 

Ending Stock value is just the subtraction between the value of Inventory[Total Demand] - Inventory[Total Stock]. 

What I need is, the new Total Stock of the current week to be replaced by the Ending Stock value of the previous week e.g the Total Stock value of 202415 week will be the Ending Stock of 202414 which is 95.000 items etc. 

 

Any support on this will be highly appreciated.

Thanks in advance, 

Manolis

 

WeekMaterial_IDTotal DemandTotal StockEnding Stock
202414802142500010000095000
20241580214250000 
20241680214250000 
20241780214250000 
20241880214250000 
20241980214250000 
20242080214250000 
20242180214250000 
20242280214250000 
20242380214250000 
20242480214250000 
20242580214250000 
20242680214250000 
20242780214250000 
20242880214250000 
20242980214250000 
20243080214250000 
20243180214250000 
20243280214250000 
20243380214250000 
20243480214250000 
20243580214250000 
20243680214250000 
20243780214250000 
20243880214250000 
20243980214250000 
20244080214250000 
20244180214250000 
20244280214250000 
20244380214250000 
20244480214250000 
20244580214250000 
20244680214250000 
20244780214250000 
20244880214250000 
20244980214250000 
20245080214250000 
20245180214250000 
20245280214250000 
20250180214250000 
20250280214250000 
20250380214250000 
20250480214250000 
20250580214250000 
20250680214250000 
20250780214250000 
20250880214250000 
20250980214250000 
20251080214250000 
20251180214250000 
20251280214250000 
20251380214250000 
20251480214250000 
20251580214250000 
20251680214250000 
20251780214250000 
20251880214250000 
20251980214250000 
20252080214250000 
20252180214250000 
20252280214250000 
2 REPLIES 2
Ashish_Mathur
Super User
Super User

Hi,

Please study my solution to a similar problem in the attached PBI files.


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

 You can use this pseudo code.

 

RUNNINGTOTAL(SUMX(Total Stock)-SUMX(Demand)))

 

Here is a Power Query version

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdW9TsNAEEXhV0Fbu5idmZuE3g0FFUgUVhStEhcofxLy+wvTgAtz4sZenXXzNXcYysc4nktXXts0fn22y+Gln0/v96ldnvrx2m6n3+PbdD+ey74biptnzTnszGv6/CEzm1/Vfp6/O1q5s8gbzlvOO87PmN04V87OOTiv0S0yqzmrOas5qzmrBasFqwWrBasFqwWrBasFqwWrBaslqyWrJaslqyWrJaslqyWrJaslq4nVxGpCNRn+LXvwN5rL0FyG5jI0l6G5DM1laK6K5qqsVlmtstrqoCwyq/GWiLdEvCXiLRFviXhL9N+W7L8B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Week", Int64.Type}, {"Material_ID", Int64.Type}, {"Total Demand", Int64.Type}, {"Total Stock", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Ending Stock", (k)=> let a=Table.SelectRows(#"Changed Type",each [Week]<=k[Week])
in List.Sum(a[Total Stock])-List.Sum(a[Total Demand]))
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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