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 August 31st. Request your voucher.

Reply
vipett
Helper II
Helper II

Subtraction "with memory" in columns

I've generated a gross demand list per item per customer and month that would be input for a breakdown to sub-components.

However, as we have some stock of the same items, I want to deduct that first to generate a net demand that would go to the breakdown.


For example 1), if I have 100 pieces on stock in the first month and the demand is 120, then I want to use those 100 pieces I have in stock and only put 20 to the breakdown. (see example below, last row for item "A")

2) If I have 100 pieces on stock and the demand is 90, then I will not put anything to the breakdown, assume I will only consume from the inventory and those 10 pieces remaining will be put forward for the next month. (first rows for A in the example + all rows for B)

3) If I have 0 pieces on stock, 100 in demand, then 100 will be put forward to the next step. ( all rows for C in the example below)

 

Sample data below

ItemCustomerRequest monthAmountStock todayRemaining stockTo Breakdown
AX230910100900
AY230930100600
AT230910100500
AK230910100400
AX231020100200
AX231110100100
AZ231240100030
BX2309255004750
BK2309105004650
BR2309505004150
BX2310155004000
BR2310405003600
BX2311205003400
BR2311205003200
BX2312105003100
BX2401205002900
BX2402305002600
CX2309100010
CY2309150015
CZ2309300030
CX2310300030
CY2310200020
CZ2310200020
CX2311100010
CY2311250025
CX231260000
8 REPLIES 8
Anonymous
Not applicable

Hi @vipett ,

 

According to your description, here are my steps you can follow as a solution.

(1) My test data is the same as yours.

(2) Click "transform data" to enter the power query editor and add index a column.

(3)We can create calculated columns.

Remaining stock = 
var _a='Table'[Stock today]-CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[Item]=EARLIER('Table'[Item]) && 'Table'[Index]<=EARLIER('Table'[Index])))
return IF(_a<0,0,_a)
To Breakdown = 
var _a='Table'[Stock today]-CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[Item]=EARLIER('Table'[Item]) && 'Table'[Index]<=EARLIER('Table'[Index])))
var _b= IF(_a<0,ABS(_a),0)
return IF('Table'[Stock today]=0,[Amount],_b)

(4) Then the result is as follows.

vtangjiemsft_0-1692673916404.png

 

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

 

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

@Anonymous Here is a simplified version: https://easyupload.io/o4dy14

@Anonymous  looked again at your pbix, copied the formulas and just changed the name.. it looks alright in you file but I can't seem to make it work.. everything looks the same, just that my index column is calculated from [item]&[month]&[customer].

But the index works and it is in the right order, but still, won't get the same result..

Any ideas?

vipett_0-1694695646514.png

 

For this item it is correct for the first bit, but then it starts accumulating again:

vipett_1-1694695981058.png

 

 

Thanks, looks good in theory, however my table is a crossjoined table with DAX, any way to do this without the index column or can it be done in some other way?

Anonymous
Not applicable

Hi @vipett ,

 

Your data doesn't look like it has an exact order or descending pattern, the index column can only be added in power query. If you want the data to be sorted in order or descending order and then calculate the final result, you can use the rankx function or the rank function to add a index column to the calculated table and then create other calculated columns. Please note that different index column may result in inconsistencies between the expected results and the output you provide.

 

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

Feels like I am almost there, however, I get the accumulated numbers in "To Breakdown", I would like to have just how much that is remaining.

Any ideas?

 

vipett_0-1693315836342.png

 

Anonymous
Not applicable

Hi @vipett ,

 

You can create another calculated column.

Column='Table'[Stock today] - 'Table'[accumulated numbers]

 

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

@Anonymous Well, I want the first line in the picture to say 342 just like it does today, and then I want the second line to say 6571 (as all available stock was consumed for the first line).

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.

Top Solution Authors