Forum Discussion
Need Help to built the formula
Hi All,
I have the below dataset. I want to calculate the usage, cost and usage/hour with the below formula and as per desired output.
Usage=
| usage=calculate max-min where hours<>0 | 60-10=50 |
| cost=calculate sum where hours<>0 and (index<>1 or index <> max(index)) | 20+30+40+50+60=200 |
| cost/hour=cost/usage | 200/50=40 |
The reason to use index in the formula is ; we don't want to include cost if the 0 in hours came in beginning(index 1) or end (max index). My formula works find, however when I calculate the cost/hour, it also exclude the cost on second row (30) despite adding the index condition. Please help me!
| Index | Hours | Cost |
| 1 | 10 | 20 |
| 2 | 0 | 30 |
| 3 | 40 | 40 |
| 4 | 50 | 50 |
| 5 | 60 | 60 |
Hi hamzashafiq
Thanks for your reply.
>> we don't want to include cost if the 0 in hours came in beginning(index 1) or end (max index)
In this scenario, you can try this,
usage measure= MAXX('Table','Table'[Hours])-MINX(FILTER('Table','Table'[Hours]<>0),'Table'[Hours])cost measure = var _maxIndex= MAXX('Table','Table'[Index]) var _minIndex= MinX('Table','Table'[Index]) return SUMX(FILTER(ALL('Table'),NOT('Table'[Hours]=0 && ('Table'[Index]=_minIndex || 'Table'[Index]=_maxIndex))),'Table'[Cost])cost/hour = DIVIDE([cost measure],[usage measure])Best Regards,
Community Support Team _Tang
If this post helps, please consider Accept it as the solution to help the other members find it more quickly.
3 Replies
- v-xiaotang
Community Support
Hi hamzashafiq
Thanks for reaching out to us. According to your formula, the conditions on both sides of and must be met at the same time, so it will exclude the second line. It is recommended to use or, and then subdivide the original formula, for example (hours = 0 and...) or (hours<>0 and ...)
Best Regards,
Community Support Team _Tang
If this post helps, please consider Accept it as the solution to help the other members find it more quickly.
- hamzashafiq
Kudo Collector
Thanks v-xiaotang could you please send me the updated formula, I am still confused.
- v-xiaotang
Community Support
Hi hamzashafiq
Thanks for your reply.
>> we don't want to include cost if the 0 in hours came in beginning(index 1) or end (max index)
In this scenario, you can try this,
usage measure= MAXX('Table','Table'[Hours])-MINX(FILTER('Table','Table'[Hours]<>0),'Table'[Hours])cost measure = var _maxIndex= MAXX('Table','Table'[Index]) var _minIndex= MinX('Table','Table'[Index]) return SUMX(FILTER(ALL('Table'),NOT('Table'[Hours]=0 && ('Table'[Index]=_minIndex || 'Table'[Index]=_maxIndex))),'Table'[Cost])cost/hour = DIVIDE([cost measure],[usage measure])Best Regards,
Community Support Team _Tang
If this post helps, please consider Accept it as the solution to help the other members find it more quickly.