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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
ClaudioF
Helper II
Helper II

sum only if the value attends to a condition

Hi everyone!

I am have a big trouble with finding the logic and the way to apply to dax;

I need to cumulatively sum values of each item for each order num, following the order, always giving the priority to the earlier order num, but i need the cumulative sum of each item to not sum some values that are bigger than the available stock i have got; i will explain better in an example:

 

using the table:

Order numitemquantity

date

111aaa2001/01/2025
111bbb1001/01/2025
222aaa1001/02/2025
333aaa2001/03/2025
333bbb201/03/2025
444bbb3001/03/2025
555bbb501/03/2025

 

If i focus only on the orders 333, 444 and 555; considering that my Table stock contains item bbb (20 quantity) and item aaa (20 quantity); if i apply the logic my first item is aaa in order 333, the cumulative sum will be now 20, wich is <= to my stock, then the status of this item in this order will be "available", in the order 333 item bbb, the sum of the item bbb will be 2, and it is <= to my stock, the result will be "available", in the order number 444, the sum of bbb will be now 32, wich is bigger than my stock, the result will be "out" and the sum will be at the end 2 again (i will not num if this is bigger than the stock), to the order number 555, the sum will be now 7, and the result will be "available". 

 

I don't know if this logic is the best way to try this, but, i cannot find out anything else; 

I would like to implement this logic to my dax code:

Measure =
VAR _date = CALCULATE(MAX('Table date'[date]), ALLSELECTED('Table date'))

-- available stock
VAR _stock =
    CALCULATE(
        SUM('Table stock'[quantity]),
        FILTER(
            ALL('Table stock'),
            'Table stock'[item] = MAX('Table orders'[Item])
        )
    )

-- Cumulative sum
VAR _runningTotal =
    CALCULATE(
        SUM('Table orders'[quantity]),
        FILTER(
            ALLSELECTED('Table orders'),
            [Item] = MAX('Table orders'[Item]) &&
            [date] <= MAX('Table orders'[date]) &&
            [Order num] <= MAX('Table orders'[Order num]) -- Considera o número do pedido
        )
    )


-- Status
VAR _status =
    IF(_runningTotal <= _stock, "available", "out")
   

RETURN
    IF(ISBLANK(_runningTotal), BLANK(), _status)
 
Can anyone pleeease help me? thats consuming my whole time..
Thankyou

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

HI @ClaudioF,

You can try to use following measure formula and it will dynamic calculate result based on current filter effects:

Status = 
VAR stock =
    CALCULATE ( SUM ( T1[quantity] ), ALLSELECTED ( T1 ), VALUES ( T1[item] ) )
VAR rolling =
    CALCULATE (
        SUM ( T1[quantity] ),
        FILTER (
            ALLSELECTED ( T1 ),
            [date] <= MAX ( T1[date] )
                && [Order num] <= MAX ( T1[Order num] )
        ),
        VALUES ( T1[item] )
    )
RETURN
    IF ( stock <> BLANK (), IF ( stock <= rolling, "available", "out" ) )

1.png

Regards,

Xiaoxin Sheng

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @ClaudioF ,

Any update on this? Did the above suggestions help with your scenario? if that is the case, you can consider Kudo or Accept the helpful suggestions to help others who faced similar requirements.

If these also don't help, please share more detailed information and description to help us clarify your scenario to test.

How to Get Your Question Answered Quickly 

Regards,

Xiaoxin Sheng

Anonymous
Not applicable

HI @ClaudioF,

You can try to use following measure formula and it will dynamic calculate result based on current filter effects:

Status = 
VAR stock =
    CALCULATE ( SUM ( T1[quantity] ), ALLSELECTED ( T1 ), VALUES ( T1[item] ) )
VAR rolling =
    CALCULATE (
        SUM ( T1[quantity] ),
        FILTER (
            ALLSELECTED ( T1 ),
            [date] <= MAX ( T1[date] )
                && [Order num] <= MAX ( T1[Order num] )
        ),
        VALUES ( T1[item] )
    )
RETURN
    IF ( stock <> BLANK (), IF ( stock <= rolling, "available", "out" ) )

1.png

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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