SOLVED

How to sum an array, after applying a few conditions to it? It's complicated, I'm attaching a file.

%3CLINGO-SUB%20id%3D%22lingo-sub-2266068%22%20slang%3D%22en-US%22%3EHow%20to%20sum%20an%20array%2C%20after%20applying%20a%20few%20conditions%20to%20it%3F%20It's%20complicated%2C%20I'm%20attaching%20a%20file.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2266068%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20attaching%20a%20file.%20It's%20self%20explanatory.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EI%20wanna%20convert%20the%20array%20to%20be%20summed%20base%20on%20a%20different%20array%20and%20then%20do%20the%20final%20summation.%3CBR%20%2F%3EIs%20it%20possible%20to%20achieve%20this%20without%20adding%20a%20helper%20column%3F%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2266068%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdmin%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20for%20web%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20on%20Mac%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20on%20mobile%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETraining%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EUser%20Adoption%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2266272%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20sum%20an%20array%2C%20after%20applying%20a%20few%20conditions%20to%20it%3F%20It's%20complicated%2C%20I'm%20attaching%20a%20fi%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2266272%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F523656%22%20target%3D%22_blank%22%3E%40Nishkarsh31%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20is%20quite%20possible%20but%20...%3C%2FP%3E%3CP%3E1.%20You%20must%20perform%20the%20summation%20before%20converting%20multiple%20measures%20to%20text%3C%2FP%3E%3CP%3E2.%20You%20cannot%20use%20OR%20to%20evaluate%20an%20array%20of%20Boolean%20operations%20because%20it%20combines%20the%20entire%20array%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20(plus%20'%2B'%20provides%20a%20workaround)%3C%2FP%3E%3CP%3E3.%20I%20do%20not%20recommend%20adding%20Kg%20and%20Lt%20unless%20you%20know%20the%20density%20of%20the%20ingredients.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAre%20you%20able%20to%20use%20the%20LET%20function%20to%20make%20sense%20of%20your%20calculation%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2266278%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20sum%20an%20array%2C%20after%20applying%20a%20few%20conditions%20to%20it%3F%20It's%20complicated%2C%20I'm%20attaching%20a%20fi%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2266278%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1639%22%20target%3D%22_blank%22%3E%40Detlef%20Lewin%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20argument%20would%20be%20%22do%20you%20need%20to%20see%20the%20value%3F%22%2C%20%22why%20is%20it%20important%20to%20you%3F%22%26nbsp%3B%20If%20there%20is%20no%20reason%20to%20see%20the%20intermediate%20values%2C%20then%20they%20merely%20constitute%20'sheet%20junk'%2C%20in%20much%20the%20same%20manner%20as%20extraneous%20ornamentation%20of%20charts%20is%20described%20a%20'chart%20junk'.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDespite%20that%2C%20there%20are%20reasons%20of%20computational%20efficiency%20that%20might%20justify%20creating%20an%20array%20of%20intermediate%20results.%26nbsp%3B%20Also%2C%20many%20users%20like%20to%20be%20able%20to%20check%20Excel%20calculations%20with%20a%20pocket%20calculator.%20For%20me%2C%20that%20is%20mis-directed%20effort.%26nbsp%3B%20They%20would%20be%20better%20advised%20to%20create%20readable%20formulas%20and%20check%20those%2C%20because%20errors%20are%20(virtually)%20always%20to%20be%20found%20in%20the%20users'%20code%2C%20not%20the%20Excel%20evaluation.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2266376%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20sum%20an%20array%2C%20after%20applying%20a%20few%20conditions%20to%20it%3F%20It's%20complicated%2C%20I'm%20attaching%20a%20fi%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2266376%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F523656%22%20target%3D%22_blank%22%3E%40Nishkarsh31%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EA%20formula%20for%20the%20cost%20could%20be%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3D%20LET(%0A%20%20%20%20unitCosts%2C%20%0A%20%20%20%20%20%20Ingredient%5BCost%5D%20*%20SWITCH(Ingredient%5BUnit%5D%2C%22kg%22%2C1%2C%22g%22%2C1000%2C%22lt%22%2C1%2C%22ml%22%2C1000)%2C%0A%20%20%20%20qty%2C%20Recipe%5BQty1%5D%20%2F%20SWITCH(Recipe%5BUnit1%5D%2C%22kg%22%2C1%2C%22g%22%2C1000%2C%22lt%22%2C1%2C%22ml%22%2C1000)%2C%0A%20%20%20%20cost%2C%20qty%20*%20XLOOKUP(Recipe%5BRecipe%201%5D%2CIngredient%5BIngredient%5D%2CunitCosts)%2C%0A%20%20SUM(cost)%26amp%3B%22%20INR%22)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThe%20weight%20and%20volume%20of%20dry%20and%20liquid%20ingredients%20are%20given%20by%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3D%20SUM(SUMIFS(Recipe%5BQty1%5D%2C%20Recipe%5BUnit1%5D%2C%7B%22kg%22%2C%22g%22%7D)%20*%20%7B1%2C0.001%7D)%26amp%3B%20%22%20kg%22%0A%0A%3D%20SUM(SUMIFS(Recipe%5BQty1%5D%2C%20Recipe%5BUnit1%5D%2C%7B%22lt%22%2C%22ml%22%7D)%20*%20%7B1%2C0.001%7D)%20%26amp%3B%20%22%20lt%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Erespectively.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20could%20always%20try%20the%20LAMBDA%20function%20so%20that%20the%20same%20formula%20can%20be%20applied%20to%20recipe1%20and%20recipe2%2C%20but%20that%20is%20probably%20several%20steps%20too%20far.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2266200%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20sum%20an%20array%2C%20after%20applying%20a%20few%20conditions%20to%20it%3F%20It's%20complicated%2C%20I'm%20attaching%20a%20fi%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2266200%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F523656%22%20target%3D%22_blank%22%3E%40Nishkarsh31%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20do%20you%20have%20against%20helper%20columns%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

I'm attaching a file. It's self explanatory.

I wanna convert the array to be summed base on a different array and then do the final summation.
Is it possible to achieve this without adding a helper column?

@Peter Bartholomew @Sergei Baklan @Riny_van_Eekelen 

15 Replies

@Nishkarsh31 

It is quite possible but ...

1. You must perform the summation before converting multiple measures to text

2. You cannot use OR to evaluate an array of Boolean operations because it combines the entire array

    (plus '+' provides a workaround)

3. I do not recommend adding Kg and Lt unless you know the density of the ingredients. 

 

Are you able to use the LET function to make sense of your calculation?

@Detlef Lewin 

My argument would be "do you need to see the value?", "why is it important to you?"  If there is no reason to see the intermediate values, then they merely constitute 'sheet junk', in much the same manner as extraneous ornamentation of charts is described a 'chart junk'.

 

Despite that, there are reasons of computational efficiency that might justify creating an array of intermediate results.  Also, many users like to be able to check Excel calculations with a pocket calculator. For me, that is mis-directed effort.  They would be better advised to create readable formulas and check those, because errors are (virtually) always to be found in the users' code, not the Excel evaluation.

@Nishkarsh31 

A formula for the cost could be

= LET(
    unitCosts, 
      Ingredient[Cost] * SWITCH(Ingredient[Unit],"kg",1,"g",1000,"lt",1,"ml",1000),
    qty, Recipe[Qty1] / SWITCH(Recipe[Unit1],"kg",1,"g",1000,"lt",1,"ml",1000),
    cost, qty * XLOOKUP(Recipe[Recipe 1],Ingredient[Ingredient],unitCosts),
  SUM(cost)&" INR")

The weight and volume of dry and liquid ingredients are given by

= SUM(SUMIFS(Recipe[Qty1], Recipe[Unit1],{"kg","g"}) * {1,0.001})& " kg"

= SUM(SUMIFS(Recipe[Qty1], Recipe[Unit1],{"lt","ml"}) * {1,0.001}) & " lt"

respectively.

 

You could always try the LAMBDA function so that the same formula can be applied to recipe1 and recipe2, but that is probably several steps too far.

@Peter Bartholomew 

Very often intermediate results are re-used multiple times. So a single formula tends to get bigger and takes more calculation time.

I know there is now LET()  but I am not getting warm with it - yet.

 

If you really want to build a single formula you have to build helper columns first and then try to put them together.

 

best response confirmed by Nishkarsh31 (Contributor)

@Detlef Lewin 

Agreed.  I probably glossed over "despite that, there are reasons of computational efficiency that might justify creating an array of intermediate results" too hastily.

That said, one of the benefits of the LET function is that the local names are not re-evaluated upon each use, so the computational efficiency is achieved without repainting the screen with intermediate results.

 

Yes, I did use helper ranges (scratch space) while I was developing parts of the formula.  The final steps were the 'packaging' of the solution, but the process does not create the nested formula nightmares that results from developing traditional formulas without helper ranges.

@Nishkarsh31 

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.

 

@Detlef Lewin 

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).

 

 

@Peter Bartholomew 

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?

@Nishkarsh31 

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.

@Nishkarsh31 


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.

 

Thank you sir.
You've made my life easier couple of times in the past weeks.

@Nishkarsh31 

Glad to have at least contributed some ideas.

The attached file uses index to identify the columns for the ingredients, quantity and units, but the main difference is that it encloses the entire formula within a Lambda function which it then names as SUMMARY.  That makes SUMMARY(1) the summary data for recipe 1 etc.

 

This is no help whatsoever to you right now, unless you happen to be on the 365 insiders beta channel, but it should work some time in the future when the Lambda functionality is rolled out.

image.png

Yes, I'm too waiting for lambda to be rolled out on regular Microsoft 365.