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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
neonguyen1803
Frequent Visitor

Incremental Subtract

Dear all, I have a table below: 

 

DateCusIDProIDStock
13/10/20231110
13/10/2023125
13/10/2023137
13/10/20232113
13/10/2023228
13/10/20232315
19/10/20231115
19/10/2023127
19/10/2023139
19/10/20232110
19/10/20232210
19/10/2023239
24/10/20231120
24/10/20231215
24/10/20231315
24/10/2023215
24/10/2023223
24/10/2023233
31/10/20231130
31/10/20231230
31/10/20231330
31/10/20232120
31/10/20232225
31/10/20232325

Then I want to calculate remain stock by time, Customer and Product like below:

neonguyen1803_1-1686849321959.png

It means from 19/10/2023, with customer 1 and product 1, the remaining stock will calculated by stock on 19/10/2023 - stock on 13/10/2023 --> results is 5

 

Please help me explain for this case

2 ACCEPTED SOLUTIONS
tamerj1
Super User
Super User

Hi @neonguyen1803 
Please refer to attached sample file with proposed calculated column solution

1.png

Remain Stock = 
VAR CurrentStock = 'Table'[Stock]
VAR Change = 
    SUMX ( 
        FILTER ( 
            CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[CusID], 'Table'[ProID] ) ),
            'Table'[Date] < EARLIER ( 'Table'[Date] ) 
        ),
        'Table'[Stock]
    )
RETURN
    CurrentStock - Change

View solution in original post

@neonguyen1803 
Yes sure, here you go

1.png

Remain Stock = 
VAR CurrentStock = 'Table'[Stock]
VAR PreviousStock = 
    SUMX ( 
        TOPN (
            1,
            FILTER ( 
                CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[CusID], 'Table'[ProID] ) ),
                'Table'[Date] < EARLIER ( 'Table'[Date] ) 
            ),
            'Table'[Date]
        ),
        'Table'[Stock]
    )
RETURN
    CurrentStock - PreviousStock

View solution in original post

4 REPLIES 4
tamerj1
Super User
Super User

Hi @neonguyen1803 
Please refer to attached sample file with proposed calculated column solution

1.png

Remain Stock = 
VAR CurrentStock = 'Table'[Stock]
VAR Change = 
    SUMX ( 
        FILTER ( 
            CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[CusID], 'Table'[ProID] ) ),
            'Table'[Date] < EARLIER ( 'Table'[Date] ) 
        ),
        'Table'[Stock]
    )
RETURN
    CurrentStock - Change

It's really great. One more question, I just want subtract with nearlest day, example: 19/10/2023 will subtract 13/10/2023, 24/10/2023 subtract 19/10/2023. Please help me explain for extended question

@neonguyen1803 
Yes sure, here you go

1.png

Remain Stock = 
VAR CurrentStock = 'Table'[Stock]
VAR PreviousStock = 
    SUMX ( 
        TOPN (
            1,
            FILTER ( 
                CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[CusID], 'Table'[ProID] ) ),
                'Table'[Date] < EARLIER ( 'Table'[Date] ) 
            ),
            'Table'[Date]
        ),
        'Table'[Stock]
    )
RETURN
    CurrentStock - PreviousStock

Thank you so much

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.