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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Ben75
New Member

Algorithm and iteration with Power BI / DAX

Hello,

I don't know if algorithms are possible with PowerBI.

 

I have several existing tables as input

'Fruits'

FruitQuantity available
Banana10
Orange7
Apple15

This table contains the list of all fruits, with the quantity available and the price per unit.

 

'Orders'

Order IDFruitQuantity requested
1Banana5
1Orange2
2Apple10
3Banana10
4Orange4
4Apple10
5Apple1

 

 

The algorithm should iterate each order (determine the delivery notes with the fruits to be served to each order taking into account the available fruits (Quantity delivered = min (quantity requested; quantity available), and updating the current stock = previous stock - delivered quantity).

 

As out I want the delivery notes table, and the stock after deliveries

Output

'Delivery note'

Order numberFruitQuantity deliveredComment
1Banana5

#Remaining Banana: 10-5

=5

1Orange2#Remaining Orange: 5-2 = 3
2Apple10#Remaining Apple: 15-10=5
3Banana5#Not enough stock. Delivered 5, remaining 0
4Orange4# Remaining 1
4Apple5#Remaining 0
5Apple0#Remaining 0

 

'Fruits after deliveries'

FruitQuantity available
Banana0
Orange1
Apple0

 

 

The difficulty is to update the available quantity between each order.

 
 
 
 
 
5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

Why isn't there a date column in both inut tables?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi @Ben75 

 

For your question, here is the method I provided:

 

Note whether the correct relationship exists between the two tables.

 

vnuocmsft_0-1717383012265.png

 

First, you need to create two MEASURES to calculate the cumulative sum of each fruit in the order and to sort the order.

 

 

Rank = 
RANKX(
    FILTER(
        ALL('Orders'), 
        'Orders'[Fruit] = MAX('Orders'[Fruit])
    ), 
    CALCULATE(SELECTEDVALUE(Orders[Order ID])),,ASC, Dense
)

 

 

 

total orders = 
CALCULATE(
    SUM('Orders'[Quantity requested]), 
    FILTER(
        ALL('Orders'),
        'Orders'[Fruit] = MAX('Orders'[Fruit])
        &&
        'Orders'[Order ID] <= MAX('Orders'[Order ID])
    )
)

 

 

Create measures.

 

 

Quantity available = 
var _fruit = SELECTEDVALUE('Fruits'[Quantity available])
var _orderTotal = 
    CALCULATE(
        SUM('Orders'[Quantity requested]), 
        FILTER(
            ALL('Orders'), 
            'Orders'[Fruit] = MAX('Orders'[Fruit])
        )
    )
var _QuantityAvailable = _fruit - _orderTotal
RETURN
IF(_QuantityAvailable > 0, _QuantityAvailable, 0)

 

Quantity delivered = 
var _minRank = MINX(FILTER(ALL('Orders'), 'Orders'[Fruit] = MAX('Orders'[Fruit])), 'Orders'[Rank])
RETURN
IF(
    SELECTEDVALUE('Fruits'[Fruit]) = SELECTEDVALUE('Orders'[Fruit]), 
    IF(
        SELECTEDVALUE('Fruits'[Quantity available]) - 'Orders'[total orders] > 0,
        SELECTEDVALUE('Orders'[Quantity requested]), 
        IF('Orders'[Rank] = _minRank + 1,
        SELECTEDVALUE('Fruits'[Quantity available]) -
        CALCULATE(
            SELECTEDVALUE('Orders'[Quantity requested]),
            FILTER(
                ALL('Orders'),  
                'Orders'[Fruit] = MAX('Orders'[Fruit]) 
                &&
               'Orders'[Rank] = _minRank)
            ),
            0
        )
    ),        
    BLANK()
)

 

As for the COMMENT section being displayed according to your table, I'm afraid that's difficult to achieve.

 

Here is the result.

 

vnuocmsft_0-1717383337169.png

 

I hope this helps you.

 

Regards,

Nono Chen

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

 

 

 

 

Thank you 🙏

How to proceed if we want to take the order into account only if all the different fruits are available?

Hi,

Thanks for you help.

What is doing exactly the Rank measure?

Could you please elaborate on:

'Orders'[Fruit] = MAX('Orders'[Fruit])

 

Also, how can I get the result in a table?

Anonymous
Not applicable

Hi @bg75 

 

Sort your orders by grouping them according to fruits. The purpose is to define the order of orders and help to determine the inventory.

 

Click Visual and select the fields you need to create a table.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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