SUMIF but for products? - excel

I have a table like the following:
X 1
X 3
X 2
Y 2
Y 5
Z 3
Z 4
I know I could use SUMIF to calculate the sum of the second column for each value in the first column, e.g., =SUMIF(A1:A7,"X",B1:B7) would give me the sum of values for X: 1+3+2=6. However, I want the product of values in the second column for each value in the first column. I.e., the output would be:
A 6
B 10
C 12
When I try to search for SUMIF but for product, I only see suggestions to use SUMPRODUCT, but that multiplies arrays together and then adds their values, whereas I don't want to sum anything, just multiply all the values within a column.
My only idea is to create a new column with the log of the values, then do EXP(SUMIF(...)) to get the product. However, this will not work if any values are 0 or negative. Is there any way to directly compute the product I want?

If one has the Dynamic Array formula FiLTER:
=PRODUCT(FILTER(B1:B7,A1:A7="X",0))
You can also use a Pivot Table:

There are probably cleverer* ways, but you could use this array formula:
=PRODUCT(IF($A$1:$A$7=D1,$B$1:$B$7))
*Edit - as I said, see Scott's answer. I always forget the new formulas.

Related

Get Spill-result to reference correct row #

As a result of this closed topic I started wondering myself the following:
Let's say we have data like this:
A
1
a
2
b
3
c
4
a
5
b
6
c
7
g
8
h
9
i
I want to divide the list into 3 (I used TEXTJOIN) and check which of the 3 of these is unique.
I used a combination of MATCH and COUNTIF (and SEQUENCE) for this.
Question:
I managed to get that correct, but wanted to get the 3 textjoin-results all at once as a spill range.
In the picture below you can see my results of attempts, but I couldn't get the TEXTJOIN to reference the correct column in it's spill-result. What is missing, or what am I doing wrong?
Not sure what you want for output.
If all you want is to determine which is/are unique entries, just use the UNIQUE function. Note the exactly_once argument.
For example:
C1: =OR(B1=UNIQUE($B$1:$B$3,,TRUE))
and fill down, but the formula will fill down automatically.
Edit:
I don't know how to get the TEXTJOIN function to spill down in groups of three like you want. However, to be able to enter the formula just once, and have the results appear in column B, and have that adjust as you add/remove entries from column A, you can use a Table structure.
In that case, row 1 would be the column headers, and the formula in B2 would be:
=IFERROR(TEXTJOIN(",",TRUE,INDEX([Column1],SEQUENCE(3)+(ROW()-ROW(Table2[#Headers]))*3-3)),"")`
No need to fill down.
Edit2:
To create a list of unique (listed only once) values from Column B in Column C, you can use the UNIQUE function. However, due to limitations on SPILL functions within a Table, this column cannot be part of the table.
In the example below, where Column C is NOT part of the table:
B2: =IFERROR(TEXTJOIN(",",TRUE,INDEX([Column1],SEQUENCE(3)+(ROW()-ROW(Table2[#Headers]))*3-3)),"")
C2: =UNIQUE(Table2[Column2],,TRUE)
Edit3:
If you were OK with omitting columnB, and reporting the unique triplets in separate cells instead of concatenated, you could do that with the UNIQUE function:
or, without a table,
=UNIQUE(INDEX($A:$A,SEQUENCE(COUNTA($A:$A)/3,3,2)),,TRUE)
Note that the start argument in the SEQUENCE function represents the first line of data in column A, (2 in this example)

How to make countifs and sumifs operate as "OR" instead of "AND"

I am trying to make a formula that uses SUMIFS and COUNTIFS to find an average of numbers with one of two criteria from different columns. So I want it to add the numbers in one column if the criteria is met in either range 1 or range 2 or both, not if its met only for both. And I need to do the same for the COUNTIFS.
Right now I have it formatted as
=SUM(SUMIF(XX:XX,"<130"ZZ:ZZ),SUMIF(YY:YY,"<80",ZZ:ZZ))/SUM(COUNTIF(XX:XX,"<130"ZZ:ZZ),COUNTIF(YY:YY,"<80",ZZ:ZZ))
But I know it is adding some numbers twice since it goes down both columns and counts the ones that have both, twice.
This is my attempt in using COUNTIF to work as COUNTIFS as an OR operator instead of AND, does anybody have any suggestions?
You can do it combining 2 SUMPRODUCTS, 1 to get the sum of all values that met criteria, and a second one to count how many values met criteria, and then do the average.
In this case I want to get the average of values in column A only if column B or C equals to 1. The green values are the only ones that meet this requirement, a total of 8 values.
If we do (3+4+5+7+8+9+11+12) we get 59, and 59/8=7.375, the result of my formula
My formula is:
=SUMPRODUCT(--(((B1:B15<2)+(C1:C15<2))<>0);A1:A15)/SUMPRODUCT(--(((B1:B15<2)+(C1:C15<2))<>0))
The first SUMPRODUCT will return the sum value:
SUMPRODUCT(--(((B1:B15<2)+(C1:C15<2))<>0);A1:A15)
This is how it works:
(B1:B15<2) will return an array or False/True if value is <2. In this case we get {0;0;1;1;1;0;0;1;1;0;1;1;0;0;0}
(C1:C15<2) same than before, but in column C, so we get {0;0;1;1;1;0;1;1;0;0;1;1;0;0;0}
--(((B1:B15<2)+(C1:C15<2))<>0) The double unary operator will check if the sum of both previous arrays are distinct to 0 (that means that ROW is valid), returning 1 if true, 0 if false. so it returns {0;0;1;1;1;0;1;1;1;0;1;1;0;0;0}
We multiply array from step 3 with values in column A and we sum them up, obtaining 59
The second SUMPRODUCT works exactly the same, but there isn't step 4, so we get just a sum of 1 and 0 to get how many values meet criteria, in this case 8.
And finally 59/8=7.375

EXCEL Formula: Multiply SUMIFS outcome with the corresponding row value from a different column

I am trying to use SUMIFS and multiply its output with the a values from the corresponding row. Example:
Example
In This example I will use just 1 criteria for the SUMIFS, but the concept remains the same:
SUMIFS(C2:C5; B2:B5; "=Sum"). This would return 5 + 6 = 11
Now I want to multiple that by the corresponding probability. Meaning 5 * 50% + 6 * 20%: SUMIFS(C2:C5; B2:B5; "=Sum") *
Any idea how to include the probability in the equation?
Thanks!
You can't use SUMIFS for this unless you add a helper column - try using SUMPRODUCT instead
=SUMPRODUCT((B2:B5="Sum")+0;C2:C5;D2:D5)
SUMPRODUCT multiplies all the arrays/ranges and then sums the result, so we can include your value and probability ranges with a conditional array based on "Sum" in the include range
With a helper column you can just use column E to multiply C and D, e.g. this formula in E2 copied down
=C2*D2
and then use SUMIFS like this
=SUMIFS(E2:E5;B2:B5;"Sum")

Formula to sum items in a column only if other column has specific value

I have an excel table with 2 columns. First column contains a string such as A,B,C and second column contains numbers. I want to obtain sum of numbers in each row if text of first column for that row is equal to given text.
For example:
A 2
C 3
B 4
A 1
C 3
formula(A) = 3
formula(B) = 4
formula(C) = 6
and so on.
One way to achieve this is via SubTotal option. Follow the example in this link Creating subtotals
Another way to do the same is using sumif function follow this link Sumif function
SUMIFS() is capable of doing this. As is SUMPRODUCT()
In european excel this would look like this. I don't know where your reference is so I am putting it in a cell named Reference.
SUMIFS($B:$B;$A:$A;"="Reference)
If you want a "running" count, then replace the arrays - from $B:$B to B$2:B2 and similar with $A:$A, and copy it down the rows.

Excel - Formula or Macro to fill a cell based on another cell that links to yet another cell

In Excel, I am trying to make a cell based of the values contained in two other cells.
I need Cells X and Y to have data based on Cells L and #, like so....
X Y L 1 2 3 4 5 6
A 6 1 1 6;1 6;1 7;1 7;2 7;2 8;1
B 7 2 4 6;1 6;1 7;1 7;2 7;2 8;1
So row A, has columns X and Y filled based of the values in the number columns. The specific number needed is what is filled in in column L.
I am not sure the best way to phrase this question. If my example doesn't make sense, I can try to clarify or provide more examples.
I have no idea if this can be done with fancy formulas or with a VBA macro or two. I am an excel noob.
If I've understood your question correctly you can do this with a combination of Left/Right, Index and search.
In my example images, the user inputs their value in column D, and then columns B and C use the formulea
=LEFT(INDEX($F2:$K2,1,$D2),(SEARCH(";",INDEX($F2:$K2,1,$D2))-1))
=RIGHT(INDEX($F2:$K2,1,$D2),(SEARCH(";",INDEX($F2:$K2,1,$D2))-1))
respectively
Here, the Index function returns the correct column to look at (i.e. the value chosen by the user, the Search function finds the position of the semi-colon, and the left/right functions return the values either side of the semi-colon.

Resources