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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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