Forum Discussion

Ben75's avatar
Ben75
New Member
2 years ago

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

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Ben75 

     

    For your question, here is the method I provided:

     

    Note whether the correct relationship exists between the two tables.

     

     

    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.

     

     

    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.

     

     

     

     

    • bg75's avatar
      bg75
      New Member

      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's avatar
        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.

    • Ben75's avatar
      Ben75
      New Member

      Thank you 🙏

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