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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
geemian93
Frequent Visitor

Calculate Age Days from the purchase history of an item deducting the current on hand Stock (FIFO)

Dear DAX Experts, @amitchandak  @lbendlin @Greg_Deckler @Ritaf1983 

 


I am looking for a help to calculate aging days based on the FIFO method of inventory from the two tables (Both tables have relationship based on Material No.),

 

First Table (Stock) include the closing stock of a particular item:

DateMaterial No.Quantity PC
30-Apr-23    10001    72,000.00

 

Second Table (PurchaseTable) contains the purchase history of this item:

Material No.Date of Purchase Quantity PC 
1000131-Mar-14       25,000.00
1000115-May-15       22,000.00
1000123-Sep-18       15,000.00
1000103-Oct-20       15,000.00
1000108-Jun-22       11,000.00
1000130-Sep-22         9,000.00
1000131-Mar-23         8,000.00
1000115-Apr-23       13,000.00


I just wanted to assign age (in days from current or specific date, 11-May-23 in this example) deducting the most recent purchased quantity from the closing quantity of particular item (i.e. 72,000 - 13,000 pc) until the closing quantity reaches "Zero" then leave rest of the purchase history line items age days to "0".

 

Desired Results should be like this:

Material No.Date of Purchase Quantity PC Age in DaysStock Balance
1000131-Mar-14       25,000.00       0       (46,000.00)
1000115-May-15       22,000.00       0       (21,000.00)
1000123-Sep-18       15,000.00     1691           1,000.00
1000103-Oct-20       15,000.00     950         16,000.00
1000108-Jun-22       11,000.00     337         31,000.00
1000130-Sep-22         9,000.00     223         42,000.00
1000131-Mar-23         8,000.00      41         51,000.00
1000115-Apr-23       13,000.00      26         59,000.00
4 REPLIES 4
lbendlin
Super User
Super User

Does it have to be DAX or can it be done in Power Query?

@lbendlin  Thanks for your reposne.
I am working in power BI right now, but it can ben done in Power Query as well.
Appreciate if you can suggest the soluion in Power Query also would be great.

@lbendlin  Would you please suggested your solution?

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hc+xCsMwDATQXzGefUUn1eCMWQOhQ8eQoZSuJZRmyN/HNJC44FJNAj1O3DB4igh98Eb0txd4zrvbRmPIx5OIH8MBGTNcwFhArUE1XB8TmA7IaqIYLvc3VP7ChG5+QrWArEGTz+sSOtdU5VZbrZTpR+92+pa0HY4r", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Material No." = _t, #"Date of Purchase" = _t, #"Quantity PC" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Material No.", type text}, {"Date of Purchase", type date}, {"Quantity PC", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Stock Balance", each List.Accumulate({[Index]..7},72000,(state,current)=>state-#"Added Index"{current}[Quantity PC]),Int64.Type),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Age in Days", each if [Stock Balance]<0 then 0 else Int64.From(#date(2023,5,11)-[Date of Purchase]),Int64.Type)
in
    #"Added Custom1"

 

Here is the basic process.  Still needs to be adjusted for multiple Material numbers  - your sample data was not good enough for that.

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.