Is there a simpler way to write this formula for calculating totals?
Hello fellow redditors!
I'm hoping you experts can tell me if I've gone the long way around with a formula, or if my solution is a reasonable one.
The Setup
Let's say I have a flavored ice cream company. I'm trying to figure out the total amount of cocoa I need for each product line.
I have one table, tbl_percentages, with the cocoa percentage required for each flavor:
| Line | Flavor | Required Cocoa Percentage |
|---|---|---|
| Ice Cream | Chocolate | 44% |
| Ice Cream | Chocochip | 77% |
| Ice Cream | Cookies n Cream | 59% |
| Ice Cream | Brownie | 57% |
| Vegan | Chocolate | 72% |
| Vegan | Chocochip | 51% |
| Vegan | Cookies n Cream | 68% |
| Vegan | Brownie | 54% |
| Gluten Free | Chocolate | 66% |
| Gluten Free | Chocochip | 26% |
| Gluten Free | Cookies n Cream | 23% |
| Gluten Free | Brownie | 36% |
| Protein | Chocolate | 63% |
| Protein | Chocochip | 73% |
| Protein | Cookies n Cream | 27% |
| Protein | Brownie | 30% |
Table formatting by ExcelToReddit
And another table, tbl_production, with my production orders:
| Line | Flavor | Amount to produce |
|---|---|---|
| Vegan | Chocolate | 269 |
| Vegan | Chocochip | 61 |
| Vegan | Cookies n Cream | 149 |
| Vegan | Brownie | 121 |
| Gluten Free | Chocolate | 118 |
| Gluten Free | Chocochip | 382 |
| Gluten Free | Cookies n Cream | 15 |
| Gluten Free | Brownie | 281 |
| Gluten Free | Chocolate | 79 |
| Gluten Free | Chocochip | 133 |
| Gluten Free | Cookies n Cream | 171 |
| Gluten Free | Brownie | 330 |
| Vegan | Chocolate | 391 |
| Vegan | Chocochip | 22 |
| Vegan | Cookies n Cream | 384 |
| Vegan | Brownie | 70 |
| Vegan | Chocolate | 267 |
| Vegan | Chocochip | 345 |
| Vegan | Cookies n Cream | 299 |
| Vegan | Brownie | 283 |
| Ice Cream | Chocolate | 206 |
| Ice Cream | Chocochip | 346 |
| Ice Cream | Cookies n Cream | 129 |
| Ice Cream | Brownie | 189 |
| Protein | Chocolate | 55 |
| Protein | Chocochip | 16 |
| Protein | Cookies n Cream | 370 |
| Protein | Brownie | 146 |
| Ice Cream | Chocolate | 19 |
| Ice Cream | Chocochip | 296 |
| Ice Cream | Cookies n Cream | 126 |
| Ice Cream | Brownie | 173 |
| Protein | Chocolate | 64 |
| Protein | Chocochip | 390 |
| Protein | Cookies n Cream | 181 |
| Protein | Brownie | 143 |
| Protein | Chocolate | 234 |
| Protein | Chocochip | 253 |
| Protein | Cookies n Cream | 200 |
| Protein | Brownie | 334 |
| Ice Cream | Chocolate | 354 |
| Ice Cream | Chocochip | 380 |
| Ice Cream | Cookies n Cream | 108 |
| Ice Cream | Brownie | 225 |
| Vegan | Chocolate | 287 |
| Vegan | Chocochip | 25 |
| Vegan | Cookies n Cream | 271 |
| Vegan | Brownie | 3 |
| Gluten Free | Chocolate | 16 |
| Gluten Free | Chocochip | 47 |
| Gluten Free | Cookies n Cream | 245 |
| Gluten Free | Brownie | 84 |
| Vegan | Chocolate | 68 |
| Vegan | Chocochip | 271 |
| Vegan | Cookies n Cream | 223 |
| Vegan | Brownie | 129 |
What I Did
I want the final calculation to automatically spill and update if I add new lines or flavors.
First, I created a sorted, unique list of the product lines:
=SORT(UNIQUE(tbl_percentages[Line]))
Then, I used this BYROW and LET formula to get the total cocoa for each line:
=BYROW(J14#, LAMBDA(r,
LET(
prodAmounts, FILTER(tbl_production[Amount to produce], tbl_production[Line]=r),
prodFlavors, FILTER(tbl_production[Flavor], tbl_production[Line]=r),
pctFlavors, FILTER(tbl_percentages[Flavor], tbl_percentages[Line]=r),
pctValues, FILTER(tbl_percentages[Required Cocoa Percentage], tbl_percentages[Line]=r),
percentages, XLOOKUP(prodFlavors, pctFlavors, pctValues),
SUMPRODUCT(prodAmounts, percentages)
)
))
It works, but is there a simpler way to do this? I feel like there might be a more direct approach I'm missing.
Thanks for taking a look
[link] [comments]
Want to read more?
Check out the full article on the original site