Forum Discussion
Algorithm and iteration with Power BI / DAX
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.
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?
- Anonymous2 years agoNot 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.