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
BC123
Frequent Visitor

Cumulative Column But only adding if previous row was negative

Hi All,

 

I'm trying to create a calc column that is cummulative but only with negative values. Ive tried a bunch of solutions but havent had much luck so any help is appreciated!

 

This is my code(Note: It does just a regular cummulative atm):

 

 

 

VAR Expiry = CALCULATE(SUM('Table'[Qty]), FILTER('Table', EARLIER('Table)'[Date])>='Table'[Date]))

 

 

Example Table:

DateQtyExpired
1/1/20233030
1/4/20232020
1/5/2023-10-10
1/10/2023-60-70
1/11/202310030
5 REPLIES 5
BC123
Frequent Visitor

This is pretty close, the issue is that the negative values should decrease(if postitive) or increase(if negative) depending on the Qty and not just be a sum of all negative vaules.  I've include a example that showcases that a bit better. This ones got me stumped. Thank you for you time!

DateQtyExpired
1/1/20233030
1/4/2023-20-20
1/5/202310-10
1/10/20234030
1/11/2023-50-50
1/12/202310-40
1/1/2024-5-45
1/2/202430-15
1/3/2024205

what result do you expect?

BC123
Frequent Visitor

Effectivly what I showed in the table, im creating this to find expected expiry stock. so for each date period any positive values(expired stock) would not be added to the next date but any negative values(remaining consumption) would be added to the calculation of next dates expired stock. The issue is it should really only be factoring whatever the previous date expired stock would be in calculating the current dates expired stock.

Ahmedx
Super User
Super User

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

 

FreemanZ
Super User
Super User

hi @BC123 ,

 

try to add a calculated column like:

Column = 
VAR _NegAcc =
SUMX(
    FILTER(
        data, 
        data[Qty]<0
            &&data[date]<EARLIER(data[date])
    ),
    data[Qty]
)
RETURN [Qty]+_NegAcc

 

it worked like:

FreemanZ_0-1699425077091.png

 

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.