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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
ClaudioF
Helper II
Helper II

Sum interval considering many filters

Hello everyone, 

I have been here a time ago to solve a problem within this same topic, the thing is, the solution was complete but i found out that one other thing was needed.

I need the following dax code, not to consider all the order as "out", when the sum of items reach the total of stok available, but, i need to test others orders to see if the amount of the stok can be used still, axample: i have orders 333, 444 and 555 with the item bbb, with quantities 2, 30 and 5, the stok is 10 for this item, the actual code, tests the stok to the orders and the result is : available, ou, out; but i need the result to be: available, out, available.. of course, following the same logic of the earlier order num, only inside of the range date i select. When i select the date 01/03/25 to 01/03/25, the only orders that have in this date are the in the example..

Captura de tela 2025-02-05 003613.png

Measure =
VAR _date = CALCULATE(MAX('Table date'[date]), ALLSELECTED('Table date'))
VAR _quantity =
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
)
)
VAR _stock =
CALCULATE(
SUM('Table stock'[quantity]),
FILTER(
ALL('Table stock'),
[item] = MAX('Table orders'[Item])
)
)
VAR _result = IF(_quantity <= _stock, "available", "out")
RETURN
IF(ISBLANK(_quantity), BLANK(), _result)

 

Thankyou a lot allready!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @ClaudioF , hello bhanu_gautam, thank you for your prompt reply!

 

To meet your requirments, we suggest you use the measure instead of the column(we could not use measure in slicer).


Create the DateRange first, then add the measure to filter the visual:

DateRange = 
VAR _maxdate= CALCULATE(MAX('Table date'[Date]),ALLSELECTED('Table date'[Date]))
VAR _mindate=CALCULATE(MIN('Table date'[Date]),ALLSELECTED('Table date'))
RETURN IF(MAX('Table orders'[Date])<=_maxdate&& MAX('Table orders'[Date])>=_mindate,1,0)

vyajiewanmsft_0-1738920645358.png

Additionally, use this measure to verify the stock status for each line item based on the order number:

ItemStatus = 
VAR CurrentItem = SELECTEDVALUE('Table orders'[Item]) 
VAR CurrentOrderNum = SELECTEDVALUE('Table orders'[Order num])  

VAR StockAvailable = 
    CALCULATE(
        SUM('Table stock'[Quantity]),
        FILTER('Table stock', 'Table stock'[Item] = CurrentItem)
    )

VAR RunningStock = 
    SUMX(
        FILTER(
            ALLSELECTED('Table orders'),
            'Table orders'[Order num] <= CurrentOrderNum && 'Table orders'[Quantity]<=StockAvailable &&'Table orders'[Item]=CurrentItem
        ),
        'Table orders'[quantity]
    )

RETURN    
IF(
    RunningStock <= StockAvailable && MAX('Table orders'[Quantity])<=StockAvailable,
    "available",
    "out"
)

Result for your reference:

vyajiewanmsft_1-1738920863737.png

vyajiewanmsft_2-1738920880103.png

 

Best regards,

Joyce

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

 

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @ClaudioF , hello bhanu_gautam, thank you for your prompt reply!

 

To meet your requirments, we suggest you use the measure instead of the column(we could not use measure in slicer).


Create the DateRange first, then add the measure to filter the visual:

DateRange = 
VAR _maxdate= CALCULATE(MAX('Table date'[Date]),ALLSELECTED('Table date'[Date]))
VAR _mindate=CALCULATE(MIN('Table date'[Date]),ALLSELECTED('Table date'))
RETURN IF(MAX('Table orders'[Date])<=_maxdate&& MAX('Table orders'[Date])>=_mindate,1,0)

vyajiewanmsft_0-1738920645358.png

Additionally, use this measure to verify the stock status for each line item based on the order number:

ItemStatus = 
VAR CurrentItem = SELECTEDVALUE('Table orders'[Item]) 
VAR CurrentOrderNum = SELECTEDVALUE('Table orders'[Order num])  

VAR StockAvailable = 
    CALCULATE(
        SUM('Table stock'[Quantity]),
        FILTER('Table stock', 'Table stock'[Item] = CurrentItem)
    )

VAR RunningStock = 
    SUMX(
        FILTER(
            ALLSELECTED('Table orders'),
            'Table orders'[Order num] <= CurrentOrderNum && 'Table orders'[Quantity]<=StockAvailable &&'Table orders'[Item]=CurrentItem
        ),
        'Table orders'[quantity]
    )

RETURN    
IF(
    RunningStock <= StockAvailable && MAX('Table orders'[Quantity])<=StockAvailable,
    "available",
    "out"
)

Result for your reference:

vyajiewanmsft_1-1738920863737.png

vyajiewanmsft_2-1738920880103.png

 

Best regards,

Joyce

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

 

wonderfull!,

its working just the way i need, its a little complex, i thought i would be able just to incude more things later onde, but, if its not much, how could i add a other filter of date? i mean, this one will filter a new column in the orders table which is the column of the max date of the order, then , i have the orders created from a range of time to other, and then, the dates that these orders might be finished. this delivery date, will impact in the calculation of the orders just like the creation date, the thing is, the delivery date is the last date to be filtered;

How could it be done?

Thankyou a lot allready for your help;

 

Its no longer an issue, i just gost the solution by adding a new table called "delivery table", i made the correct relation between this and the orders table, then i placed a new data segmentation visual on the dashboard and inserted the date of delivery. The result was just right!

Thankyou all!! a lot. 

ClaudioF
Helper II
Helper II

https://app.powerbi.com/reportEmbed?reportId=4934aedb-3f6c-42ce-8ebc-7376d4af98ca&autoAuth=true&ctid...

 

this is the link to the dashboard, it didnt work using the code you provided;

bhanu_gautam
Super User
Super User

@ClaudioF , Try using

dax
Measure =
VAR _date = CALCULATE(MAX('Table date'[date]), ALLSELECTED('Table date'))
VAR _item = MAX('Table orders'[Item])
VAR _orders =
FILTER(
ALLSELECTED('Table orders'),
[Item] = _item &&
[date] <= _date
)
VAR _stock =
CALCULATE(
SUM('Table stock'[quantity]),
FILTER(
ALL('Table stock'),
[item] = _item
)
)
VAR _runningTotal =
SUMX(
FILTER(
_orders,
[Order num] <= EARLIER('Table orders'[Order num])
),
[quantity]
)
VAR _result = IF(_runningTotal <= _stock, "available", "out")
RETURN
IF(ISBLANK(_runningTotal), BLANK(), _result)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






This is the result i get when select the range date 01/03/25 using the code i gave:

The needed result should be: available, available, out, available;

The stock of the item bbb is 10;

Captura de tela 2025-02-05 133918.png

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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