Forum Discussion
Stock cumulative total DAX
Hi! I'm trying to DAX a calculated column for the stock cumulative total by product. When present the first movement of the year of a product has Initial Inventory="Y" so the cumulative calculation resets and begins from that quantity value, if there's not a Initial inventory movement the calculation won't reset, how can I write this formula?
Another problem is that I could have multiple movements of the same product in the same date with the same moved quantity, I have to use a row index number logic?
Consider that this database has several years of data.
Someone can please help me?
21 Replies
- Michiel
Resolver III
First, the best way to implement this logic is to use a measure, not a calculated column. To do this, create a separate date table and a relationship from Stock[Date] to Date[Date].
Use this formula to create a measure:
CumulativeStock = VAR MaxDate = MAX('Date'[Date]) VAR InitialDate = MAXX(FILTER(ALL(Stock);Stock[Date]<=MaxDate && Stock[Initial Inventory]="Y");Stock[Date]) RETURN CALCULATE(SUM(Stock[Quantity]);ALL('Date');'Date'[Date]>=InitialDate && 'Date'[Date]<=MaxDate)In this formula, the variable MaxDate contains the last selected date. The variable InitialDate determines the last initial inventory date before the last selected date. After this, the measure calculates the total quantity of stock lines between the initial date and the max date.
For your sample data, this results in something like:
- tringuyenminh92
Memorable Member
- tringuyenminh92
Memorable Member
Hi AGo,
please try calculated measure with expression:
Stock Cummulative = CALCULATE(sum(Stock[Quantity]),filter(ALL(Dates), Dates[Date].[Year] = MAX(Dates[Date].[Year]) && Dates[Date]<= MAX(Dates[Date]) ))
To understand more about cummulative with time, you could refer topic: http://www.daxpatterns.com/time-patterns/
If this works for you please accept it as solution and also like to give KUDOS.
Best regards
Tri Nguyen- AGo
Post Patron
tringuyenminh92 I need a calculated column (also because I need to recreate a historical column and othe calculations over it of the stock entity by product). I read that page before this post, but it doesn't consider the complexity of my case (multiple products, conditional initial inventory...)
- tringuyenminh92
Memorable Member