Forum Discussion
conditional calculated column based on previous rows
| ID | Status | Cost |
1 | Start | 10 |
| 1 | Apple | |
| 2 | Start | |
| 2 | End | 10 |
| 3 | Banana | |
| 3 | Carrot | |
| 4 | Durian | |
| 4 | End |
Need help creating the calculated column "cost".
If "status = start", the cost = 10. However, if for the same ID, there is also "status = end", then "cost = 10" only for the "status = end", while cost is blank for "status = start".
Any other type of status, results in "cost = (blank)" [ID #3]
Need at least one line to have "status = start" for the cost calculation to work [ID #4].
Here is one way to do it (there is probably a shorter way too). Please try this expression in your calculated column, replacing the Cost table name with your actual one.
New Cost Column = VAR isstartrow = Cost[Status] = "Start" VAR isendrow = Cost[Status] = "End" VAR startrow = NOT ( ISBLANK ( CALCULATE ( COUNTROWS ( cost ), ALLEXCEPT ( Cost, Cost[ID] ), Cost[Status] = "Start" ) ) ) VAR endrow = NOT ( ISBLANK ( CALCULATE ( COUNTROWS ( cost ), ALLEXCEPT ( Cost, Cost[ID] ), Cost[Status] = "End" ) ) ) RETURN SWITCH ( TRUE (), AND ( startrow, isendrow ), 10, AND ( NOT ( endrow ), AND ( isstartrow, startrow ) ), 10, BLANK () )If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat
1 Reply
- mahoneypatMicrosoft Employee
Here is one way to do it (there is probably a shorter way too). Please try this expression in your calculated column, replacing the Cost table name with your actual one.
New Cost Column = VAR isstartrow = Cost[Status] = "Start" VAR isendrow = Cost[Status] = "End" VAR startrow = NOT ( ISBLANK ( CALCULATE ( COUNTROWS ( cost ), ALLEXCEPT ( Cost, Cost[ID] ), Cost[Status] = "Start" ) ) ) VAR endrow = NOT ( ISBLANK ( CALCULATE ( COUNTROWS ( cost ), ALLEXCEPT ( Cost, Cost[ID] ), Cost[Status] = "End" ) ) ) RETURN SWITCH ( TRUE (), AND ( startrow, isendrow ), 10, AND ( NOT ( endrow ), AND ( isstartrow, startrow ) ), 10, BLANK () )If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat