Forum Discussion
Ben75
2 years agoNew 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' Fruit Quantity available Banana 10 Orange 7 Apple 15 This tabl...
Anonymous
2 years agoNot 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.
Ben75
2 years agoNew Member
Thank you 🙏
How to proceed if we want to take the order into account only if all the different fruits are available?