Excel Dynamic sumproduct - excel

I have created a vector (range of values) of the form (a_11,..,a_1n,...,a_m1...,a_mn) for m and n which are input from another sheet and a vector of probabilities (p_1,...,p_n). In another sheet I now have different subsets of 1,..,m as input so for example I have the string "1,3,4". I would now like to calculate for this string
a_11*a_31*a_41*p_1+a_12*a_32*a_42*p_2+...+a_1n* a_3n* a_4n*p_n
(basically the sumproduct of the vectors (a_11,...,a_1n), (a_31,...,a_3n), (a_41,...,a_4n) and (p_1,...,p_n)
As the string "1,3,4" differs each time (also in length) I would like to do this dynamically but without using VBA. I have already succeeded doing it in VBA by means of a double loop but I want to use the solver so I want to implement it in a sheet refering to the entries.
Could anyone please help me?

I suggest keeping it simple and just copying across the ai,j to another sheet, setting them to one for rows you don't want to be in the multiplication. So if your Sheet1 looked like this
Use this formula in Sheet2
=IF(ISNUMBER(FIND(","&ROWS(A$2:A2)&",",","&$E$2&",")),Sheet1!A2,1)
and just do the multiplications using PRODUCT on each column so you get
The products may also be calculated in a single formula as
=SUMPRODUCT(SUBTOTAL(6,OFFSET(A1,1,COLUMN(A2:C5)-COLUMN(A:A),ROWS(A2:C5),1)),Sheet1!A8:C8)
if preferred.

Related

Return Array of SUMPRODUCT Results

I am trying to merge the contents of tables, rank them, and return the results as an array so that, eventually, values can be found with INDEX or VLOOKUP. I'm on a work network. I am stuck with Excel 2013 without PowerQuery and macros are a hard no. I am trying to avoid hidden sheets and helper columns. I have managed to successfully combine the output of tables into a single array, but am having trouble accomplishing getting a ranking of each item into an array.
Here is the sample workbook I'm using to do a proof of concept:
https://1drv.ms/x/s!AmnV99KYATO-g5kxvJBpTSSjF_XBXw
Everything is currently on just one sheet, but the goal is to eventually link tables from multiple sheets or even workbooks together and have the results automatically aggregated in a table that is both sortable and filterable. I'm trying to end the endless Ctrl+C and Ctrl+V I currently do to aggregate data.
For readability, most of the formulas in the workbook have been turned into named arrays. I have the last few steps broken out so you can see where I am stuck. The components of the named array RANKED only return a single digit, not an array of 24 values like the COMBINED formula. When I Ctrl+Shift+Enter my final RANKED array I get the expected result, but when I do VLOOKUP and INDEX I get errors because RANK only returns one value. You can see in the table on the right VLOOKUP fails after the first row.
I am using SUMPRODUCT instead of COUNTIF because (as far as I know) COUNTIF cannot take arrays as input. There is then an offset so that the ranking works like RANK.EQ. Does anyone know how to accomplish what I am trying to do or am I attempting the impossible?

Taking average of certain values in one Excel column based on values in another

I have a (large) array of data in Excel of which I need to compute the average value of certain values in one column, based on the values of another column. For example, here's a snippet of my data:
So specifically, I want to take the average of the F635 mean values corresponding with Row values of 1. To take it a step further, I want this to continue to Row values of 2, Row values of 3 etc.
I'm not familiar with how to run code in Excel but have attempted to solve this by using the following:
=IF($C = "1", AVERAGE($D:$D), "")
which (to my understanding) can be interpreted as "if the values (anywhere) in column C are equal to 1, then take the average of the corresponding values in column D."
Of course, as I try this I get a formula error from Excel.
Any guidance would be incredibly appreciated. Thanks in advance.
For more complicated cases, I would use an array-formula. This one is simple enough for the AVERAGEIF formula. For instance =AVERAGEIF(A1:A23;1;B1:B23)
Array-formula allows for more elaborate ifs. To replicate the above, you could do =SUM(IF($A$1:$A$23=1;$B$1:$B$23;0))/COUNT(IF($A$1:$A$23=1;$B$1:$B$23;0)).
Looks like more work but you can create extremely elaborate if-statements. Instead of hitting ENTER, do CTRL-ENTER when entering the formula. Use * between criteria to replicate AND or + for OR. Example: SUM(IF(($A$1:$A$23="apple")*($B$1:$B$23="green");$C$1:$C$23;0)) tallies values for green apples in c1:c23.
Your sample data includes three columns with potential ifs so my guess is that you're going to need array formulas at some point.
Excel already has a builtin function for exactly this use; AVERAGEIF().
=AVERAGEIF(C:C,1,D:D)

Sumproduct or Countif on a 2D matrix

I'm working on data from a population of people with allergies. Each person has a unique ExceptionID, and each allergen has a unique AllergenID (451 in total).
I have a data table with 2 columns (ExceptionID and AllergenID), where each person's allergies are listed row by row. This means that the ExceptionID column has repeated values for people with multiple allergies, and the AllergenID column has repeated values for the different people who have that allergy.
I am trying to count how many times each pair of allergies is present in this population (e.g. Allergen#107 & Allergen#108, Allergen#107 & Allergen#109,etc). To keep it simple I've created a matrix of 451 rows X 451 columns, representing every pair (twice actually because A/B and B/A are equivalent).
I somehow need to use the row name (allergenID) to lookup the ExceptionID in my data table, and count the cases where that matches the ExceptionIDs from the column name (also AllergenID). I have no problem using Vlookup or Index/Match, but I'm struggling with the correct combination of a lookup and Sumproduct or Countif formula.
Any help is greatly appreciated!
Mike
PS I'm using Excel 2016 if that changes anything.
-=UPDATE=-
So the methods suggested by Dirk and MacroMarc both worked, though I couldn't apply the latter to my full data set (17,000+ rows) because it was taking a long time.
I've since decided to turn this into a VBA macro because we now want to see the counts of triplets instead of pairs.
With the 2 columns you start with, it is as good as impossible... You would need to check every ExceptionID to have 2 different specific AllergenID. Better use a helper-table with ExceptionID as rows and AllergenID as columns (or the opposite... whatever you like). The helper table needs a formula like:
=COUNTIFS($A:$A,$D2,$B:$B,E$1)
Which then can be auto-filled. (The ranges are from my example, you need to change them to your needs).
With this helper-matrix you can easily go for your bigger matrix like this:
=COUNTIFS(E:E,1,INDEX($E:$G,,MATCH($I2,$E$1:$G$1,0)),1)
Again, you can auto-fill with this formula, but you need to change it, so it fits your needs.
Because the columns have the same ID2 (would be your AllergenID), there is no need to lookup them because E:E changes automatically with the auto-fill.
Most important part of the formulas are the $ which should not be messed up, or you can not auto-fill it.
Picture of my self-made example (formulas are from the upper left cell in each table):
If you still have any questions, just ask :)
It can be done straight from your original set-up with array formulas:
Please note that array formulas MUST be entered with Ctrl-Shift-Enter, before copying across and down:
In the example pic, I have NAMED the data ranges $A$2:$A$21 as 'People' and $B$2:$B$21 as 'Allergens' to make it a nicer set-up. You can see in the formula bar how that looks as a formula. However you could use the standard references like this in your first matrix cell:
EDIT: silly me, N function is not needed to turn the booleans into 1's and 0's, since multiplying booleans will do the trick. Below formula works...
SUM(IF(MATCH($A$2:$A$21,$A$2:$A$21,0)=ROW($A$2:$A$21)-1, NOT(ISERROR(MATCH($A$2:$A$21&$E2,$A$2:$A$21&$B$2:$B$21,0)))*NOT(ISERROR(MATCH($A$2:$A$21&F$1, $A$2:$A$21&$B$2:$B$21,0))), 0))
Then copy from F2 across and down. It can be perhaps improved in technique with sumproduct or whatever, but it's just a rough example of the technique....

(excel) sumproduct multiplying with another sheet

i have a little problem with final formulas in one of my column. How to start. maybe i will explain what i have a then what i want.
i have an excel worksheet with 3 sheets. i want to record goods and what are these goods made of. first is sheet called Goods where is possible to put number of goods i want to make. In this case i want to make 1x sandwich1 and at the same time 3x sandwich2. i dont want make sandwich3 this time.
Second sheet is Matrix sheet where I record every good and what it is made of. This sheet is basic sheet and all other sheets take list of goods (resp. ingredients) from this sheet. Simply when i want to make sandwich1 i look at matrix and know that i need 1x1pc of egg + 1x5g of cheese. And for 3x sandwiche2 i need 3x10g of sausages.
Final sheet is called Ingredients. It is a list of used ingredients from Matrix sheet (exactly same order) to make these sandwiches. I want to fill formulas into column B which would go through one ingredient ofter ingredient and count needed amount of it. So it would look into matrix in the same row and where there is some number it would multiply with number of items from Goods sheet. The list of goods is also in the same order as in the matrix sheet.
I hope you understand now what i want and will try to help me. I think there will be SUMPRODUCT, SUMIF and maybe INDERECT functions but i am not that skilled in excel
thanks for any suggestions
You can use MMULT function here - it's an "array formula" which you need to enter in a range. You can do that like this:
In Ingredients worksheet enter this formula in B2
=MMULT(Matrix!C2:E4+0;Goods!B2:B4+0)
[I'm assuming you have a European version of Excel where ; is used to separate arguments]
Now select the whole range B2:B4, press F2 key to select formula and hold down CTRL and SHIFT keys and press ENTER. This "array enters" the formula in the range and you should now see curly braces like { and } around the formula and also the correct results.
You cannot change part of that array now, only the whole thing
Note that I'm assuming that the contents of Goods!A2:A4 will be the same as Matrix!C1:E1 and in the same order. You can extend the ranges to be as large as you like as long as that principle still holds
I suspect that this is an issue of "when all you have is a hammer, every problem is a nail". For reasons known only to you you are using a spreadsheet to solve a problem that databases were made to do. Any solution to this problem in a spreadsheet will be entirely dependent on the integrity of your data - add another column or get things out of order and it will fail.
That said, what you have in your link is effectively a pivot table and what you need is the unpivoted version of this - the instructions for getting this are here.
When you have that, you can use the various database functions in excel to get your answer.

How are Google Sheets array formulas different from Excel (count until sum reached)

I'd like to know which column is the last column where the sum of the values of the row is smaller or equal to a given value. (Count the columns until a sum is reached.)
In Microsoft Excel the following array formula works just fine:
{=MATCH(7;SUBTOTAL(9;OFFSET(C1;;;1;COLUMN(C1:G1)-COLUMN(C1)+1));1)}
But Google Sheets always returns 1 as an answer:
=ARRAYFORMULA(MATCH(7;SUBTOTAL(9;OFFSET(C1;;;1;COLUMN(C1:G1)-COLUMN(C1)+1));1))
Is there some difference between Excel and Google Sheets array formulas that I'm missing?
If there is a difference is it documented somewhere?
Is there another way to implement this in Google Sheets (preferably without custom functions)?
Link to sample spreadsheet.
Is there some difference between Excel and Google Spreadsheet array
formulas that I'm missing?
The difference is how specific functions are supported in array formulae. In this case, you're out of luck on two counts: OFFSET can't be iterated over an array (ie it can't produce an "array of arrays" as it can in Excel), and the second argument of SUBTOTAL can't be iterated either; in Sheets, it must be an explicit range.
If there is a difference is it documented somewhere?
No, not that I know of.
Is there another way to implement this in Google Spreadsheet
(preferably without custom functions)?
=ArrayFormula(MATCH(7;SUMIF(COLUMN(C1:G1);"<="&COLUMN(C1:G1);C1:G1)))

Resources