Forum Discussion
How to sum an array, after applying a few conditions to it? It's complicated, I'm attaching a file.
My idea would be to use the CONVERT() function. But that requires to change "Lt" to "l" and "Kg" to "kg".
The next step would be to unpivot your data into a flat table.
Next steps beyond spreadsheet formulas would be Power Query or Power Pivot.
I like the CONVERT idea; it is a function that I have never used as an array operator.
= LET(
unitCost, XLOOKUP(Recipe[Recipe1], Ingredient[Ingredient], Ingredient[Cost]),
ingredientUnits, XLOOKUP(Recipe[Recipe1], Ingredient[Ingredient], Ingredient[Unit]),
cost, CONVERT(+Recipe[Qty1], +Recipe[Unit1], ingredientUnits) * unitCost,
dry, "Dry " & SUM(SUMIFS(Recipe[Qty1], Recipe[Unit1],{"kg","g"}) * {1,0.001})&" kg",
wet, "Wet " & SUM(SUMIFS(Recipe[Qty1], Recipe[Unit1],{"l","ml"}) * {1,0.001}) & " l",
total, SUM(cost)&" INR",
TEXTJOIN(ΒΆ,,dry,wet,total))
I am less convinced by evolution path you plot from spreadsheet to Power Query. There are transformations that used to require PQ that can now revert quite happily to formulas; not the heavy database stuff but, rather, the smaller interactive calculations (1000x100 arrays rather than 100,000 records x 100 fields with a few inner joins).
- Nishkarsh31Apr 11, 2021Brass Contributor
Hi sir, the formula looks good. There's minor problem
1) If There's one ingredient missing the formula doesn't work. There would be cases, we might not use one ingredient. How can we work around that?
2) Also, instead of giving table reference, I want to give array reference since I'll be adding a lot of columns and the expanded table won't automatically change the column names.
(I know it would make the table use redundant, but can't help it)3)Can you let me know how to put that delimiter in textjoin to skip a line, where to find it on keyboard?
4) What would you recommend, Switch or Convert in the final formula?
- Detlef_LewinApr 11, 2021Silver Contributor
2) Also, instead of giving table reference, I want to give array reference since I'll be adding a lot of columns and the expanded table won't automatically change the column names.
(I know it would make the table use redundant, but can't help it)As I suggested earlier you should unpivot your table. The table grows down and not to the right. It would make formula building easier.
- PeterBartholomew1Apr 11, 2021Silver Contributor
I have attached a new copy of the workbook - I hadn't saved the original.
1) Missing ingredients: I have used the 'if not found' parameter in XLOOKUP to return 0 or "".
2) Table references is how I reference source data. If you select the rightmost 3 columns and drag the fill handle to the right suitably numbered columns should appear for you to enter new recipe data.
3) The Pilcrow symbol is ASCII 182 and is not found on a keyboard. I used the Insert/Symbol dialogue box to generate it and then used it as a defined Name that refers to
= CHAR(10)
(line feed). I used that symbol because I am used to seeing it in Word.
4) I like Detlef_Lewin's use of CONVERT.
5) To help the process of expanding the table, I have written the summary data formula so that it reads the recipe number (or name) from the table header in the same column and uses that to lookup the quantity and units. That way, the formula does not change from recipe to recipe and is easy to replicate.
p.s. Sorry, I have only just noticed the new attachment. I hope its not so different that it invalidates the formula.
Comment: The formula is now so large that I would consider refactoring it to present the name extraction, Total Cost, Dry & Wet amounts as separate Lambda functions but, since Lambda is still only beta release, that would be somewhat premature.
- Nishkarsh31Apr 12, 2021Brass ContributorThank you sir.
You've made my life easier couple of times in the past weeks.