Is there a multiple-and-add formula in Google's spreadsheet? - google-sheets

What I want is to easily multiply a number by another number for each column and add them up at the end in Google Sheets. For example:
User | Points 1 | Points 2 | Points 3 | Total
| 5 | 1 | 4 |
-----+----------+----------+----------+------
Jane | 2 | 3 | 0 | 13 (2*5 + 3*1 + 0*4)
John | 1 | 11 | 4 | 32 (1*5 + 11*1 + 4*4)
So it's easy enough to make this formula for the total:
= B3*$B$2 + C3*$C$2 + D3*$D$2
The problem is I frequently need to insert additional columns or even remove some columns. So then I have to mess with all the formulas. It's a pain... we have many spreadsheets with these formulas. I wish there was a formula like SUM(B3:D3) where I could just specify a range. Is there anything like MULTIPLY_AND_SUM(B2:D2, B3:D3) that would do this? Then I could insert columns in the middle and the range would still work.

There is a built in function in Google Sheets that does exactly what you are looking for: SUMPRODUCT.
In your example the formula would be:
=sumproduct(B$2:D$2,B3:D3)
Click here for more information about this function.

You can accomplish that without requiring a special-purpose function.
In E3, try this (and copy it to the rest of your rows):
=sum(arrayformula(B3:D3*B$2:D$2))
You can read about arrayformula here.
As long as you introduce new columns between B and D, this formula will automatically adjust. If you add new columns outside of that range, you'll need to edit (and cut & paste).
On it's own, arrayformula(B3:D3*B$2:D$2) operates over each value in B3:D3 in turn, multiplying it by the corresponding value in B$2:D$2. (Note the use of absolute references to 'lock down' to row 2.) The result in this case is three values, [10,3,0], arranged horizontally in three rows because that matches the dimensions of the ranges.
The enveloping sum() function adds up the values of the array produced by arrayformula, which is 13 in this case.
As you copy that formula to other rows, the relative range references get updated for the new row.

Related

Can I filter out pivot table results that only have one row for a value in column A?

I created a pivot table in googlesheets, and it returns results that look like:
first | second | CountOf3
--------------------------
thing | value | 23
| newVal | 3
| cool | 34
that | value | 234
otherThing | cool | 4
| newVal | 345
And I want to filter out results with just one resulting row for the item in the first column.
So in this example, that would be the row: that | value | 234.
I would like the filter to remove that row, and leave the remaining rows. This is a pivot table in a 2nd sheet that updates when Sheet1 changes.
I have been trying all day, and have not been able to come up with a solution. I was hoping there would be some sort of filter, or spreadsheet formula to do this. I've tried multiple combinations of filters, but nothing seems to work - I'm starting to wonder if this is even possible.
It isn't pretty, but a brute force way is to have a check column beside your pivot table, with this formula on the first data row, ie beside "thing | value | 23".
It flags each row where the subsequent cell in column D is not blank. Then use a query (or filter) to list only the output rows you want. Note that you would hide the columns or rows with the actual (unfiltered) pivot output.
This is the simplest version, to see the logic:
=AND(LEN(D3),LEN(D4))
which results in a TRUE value for pivot chart rows that only have one value.
A more elegant version is an arrayformula, adds the header lable, and uses "Skip" as the flag for which rows to filter out.
={"Better Check";ARRAYFORMULA(IF(LEN(D3:D998)*LEN(D4:D999)*LEN(E3:E998),"Skip",))}
Note that this formula allows for a pivot table result effectively to the bottom of the sheet, but it does have a finite range, due to the constraint of checking two rows at once. It could be enhanced by using a COUNTA on the third data column to measure the exact length of the pivot table results and control the range dynamically, Like this:
={"Better Check";
ARRAYFORMULA( IF( LEN(INDIRECT("D3:D" & (COUNTA(F$3:F)+ROW(F$2)))) *
LEN(INDIRECT("D4:D" & (COUNTA(F$3:F)+1+ROW(F$2)))),
"Skip",))}
Let us know if this helps at all.

Conditionally formatting duplicate rows in google sheets

I want to apply conditional formatting so that all the rows which match another row exactly are highlighted.
Let's say I have a spreadsheet like the following
| | a | b | c |
|---|---|---|---|
| 1 | A | B | C | // Matches row 3 and 6
| 2 | A | B | A | // Matches row 5
| 3 | A | B | C | // Matches row 1 and 6
| 4 | B | B | C | // Matches no other row
| 5 | A | B | A | // Matches Row 2
| 6 | A | B | C | // Matches row 1 and 3
| 7 | B | B | A | // Matches no other row
All the rows except for row 4 and 7 would be highlighted.
For to rows to be considered duplicates, the value of each/every cell in a given row must exactly match the value of the corresponding cell (cell in the same column) in a duplicate row.
My attempt so far can only return the values of rows with only the first 2 cells being duplicate and returns the concatenation of all the duplicate values in each row, which is very far away from what I want.
CC = arrayformula(A:A&" "&B:B&" "&C:C) returns a new row which is the concatenation of A, B, and C, which is coercing the cell values into strings so "1" and 1 which are not the same appear to be the same, and also doesn't work across the entire row (could do If I just kept adding Columns, but would look terrible).
=filter(unique(CC), arrayformula(countif(CC, unique(CC)) > 1)) CC is the returned value from the previous equation
This would output
A B C
A B A
Then I could add a conditional formatting rule with a custom formula that Highlights a row if it's concatenated contents "Match" one of the return values from the previous equation, but I don't know how to do that, and the previous equation is already pretty flawed.
Ideally I want a solution that involves no string concatenation or entering in all column names.
Let's go over what is needed to create this function.
1st you need to get the rows as a string to be able to compare them like you did. I didn't use space like you did because it takes place, but you can keep them.
=ARRAYFORMULA(A:A&B:B&C:C)
The issue with that is that since the formula will be on 3 column, we don't want it to become C:C&D:D&E:E so we have to fix the column.
=ARRAYFORMULA($A:$A&$B:$B&$C:$C)
Yay! Now we have a list of string that represent the "value" of each row. We can now count for each line how many times they are found. I used A2 cause I guess you have a header, but if you don't, simply replace it with A1.
=COUNTIF(ARRAYFORMULA($A:$A&$B:$B&$C:$C);A2&B2&C2)
We also have to fix the column here or the function will only work on the 1st one.
=COUNTIF(ARRAYFORMULA($A:$A&$B:$B&$C:$C);$A2&$B2&$C2)
And now all that's left is check if you want to see thoses who are unique or thoses who have matches
=COUNTIF(ARRAYFORMULA($A:$A&$B:$B&$C:$C);$A2&$B2&$C2)>1
This solution doesn't involved converting the values to strings, but it still requires adding a function for every column, so it's almost there.
=countifs(arrayformula($A:$A=$A1),TRUE,arrayformula($B:$B=$B1),TRUE,arrayformula($C:$C=$C1),TRUE)>1
It's just a conditional for each column conditional = arrayformula($A:$A=$A1) in a countifs, countifs(conditional, true).
I just need to make it so it can take the column values as an array which i'm guessing will require an arrayformula
There is a MUCH simpler way.
Load Conditional Formatting (under Format).
Select "custom formula is" (way at the bottom of the formula list)
Use the formula "=countif(A:A,A1)>1", where A is the column that contains the cells you want to be formatted for duplicates.

Google Sheets Function to get value of adjacent cell based on key

I need some help figuring out some Google Sheets function. I'm trying to look for the value of an adjacent cell based on a key. The problem is that the table has several rows and columns to search for. For example:
| A | B | C | D |
1 | Alpha | 5 | Bravo | 10 |
2 | Charlie | 15 | Delta | 20 |
The list goes on for several more rows and colums. What I'm looking for for example, is when a search use "Delta" as my search key, it will return the value 20 for me. I appreciate your help. Thanks!
Here is the answer I got on the web application community:
https://webapps.stackexchange.com/questions/90198/how-to-get-value-of-adjacent-cell-based-on-key-if-the-table-is-split-into-multi
You can still use vlookup for this, by stacking the lookup tables into one using the array notation {first ; second; third} which means first above second above third. Example:
=vlookup("Delta", {A:B; C:D}, 2, False)
returns 20.
Try:
=offset(lookup("Delta",A1:D2),0,1)
You could use the following:
=ArrayFormula(INDEX($A$1:$D$2,MIN(IF($A$1:$D$2=$G$1,ROW($A$1:$D$2))),MIN(IF($A$1:$D$2=$G$1,COLUMN($A$1:$D$2)))+1))
This could by done in several ways.
Use RegEx
=regexextract(CONCATENATE(A1:D),F6 & "(\d+)")
where F6 is cell with lookup value i.e. "Delta"
Use lookup
=INDEX(A:D,MATCH(LOOKUP(F6,A1:D,A1:A),A1:A),MATCH(F6,INDIRECT(MATCH(LOOKUP(F6,A1:D,A1:A),A1:A)&":"&MATCH(LOOKUP(F6,A1:D,A1:A),A1:A)))+1)
where F6 is cell with lookup value i.e. "Delta", look at example.
MATCH(LOOKUP(F6,A1:D,A1:A),A1:A) is used 3 times, so it could be counted in separate cell.

Count rows with not empty value

In a Google Spreadsheet: How can I count the rows of a given area that have a value? All hints about this I found up to now lead to formulas that do count the rows which have a not empty content (including formula), but a cell with
=IF(1=2;"";"") // Shows an empty cell
is counted as well.
What is the solution to this simple task?
I just used =COUNTIF(Range, "<>") and it counted non-empty cells for me.
=counta(range)
counta: "Returns a count of the number of values in a dataset"
Note: CountA considers "" to be a value. Only cells that are blank (press delete in a cell to blank it) are not counted.
Google support: https://support.google.com/docs/answer/3093991
countblank: "Returns the number of empty cells in a given range"
Note: CountBlank considers both blank cells (press delete to blank a cell) and cells that have a formula that returns "" to be empty cells.
Google Support: https://support.google.com/docs/answer/3093403
If you have a range that includes formulae that result in "", then you can modify your formula from
=counta(range)
to:
=Counta(range) - Countblank(range)
EDIT: the function is countblank, not countblanks, the latter will give an error.
Here's what I believe is the best solution so far:
=CountIf(ArrayFormula(range<>""),TRUE)
Here's why in 3 easy steps
Step 1: Simple As Pie - Add Extra Column
The answer by eniacAvenger will yield the correct solution without worrying about edge cases as =A1<>"" seems to arrive at the correct truthy/falsy value based on how we intuitively think of blank cells, either virgin blanks or created blanks.
So imagine we have this data and we want the Count of non-blanks in B2:B6:
| | A | B | C |
|---|-------------|-------|---------|
| 1 | Description | Value | B1<>"" |
| 2 | Text | H | TRUE |
| 3 | Number | 1 | TRUE |
| 4 | IF -> "" | | FALSE |
| 5 | IF -> Text | h | TRUE |
| 6 | Blank | | FALSE |
If we relied on Column C, we could get the count of values in B like this:
=COUNTIF(C2:C6,True)
Step 2: Use FormulaArray to dynamically create Extra Column
However, consideRatio's comment is a valid one - if you need an extra column, you can often accomplish the same goal with an ArrayFormula which can create a column in memory without eating up sheet space.
So if we want to create C dynamically, we can use an array formula like this:
=ArrayFormula(B2:B6<>"")
If we simply put it in C2, it would create the vertical array with a single stroke of the pen:
| | A | B | C |
|---|-------------|-------|--------------------------|
| 1 | Description | Value | =ArrayFormula(B2:B6<>"") |
| 2 | Text | H | TRUE |
| 3 | Number | 1 | TRUE |
| 4 | IF -> "" | | FALSE |
| 5 | IF -> Text | h | TRUE |
| 6 | Blank | | FALSE |
Step 3: Count Values in Dynamic Column
But with that solved, we no longer need the column to merely display the values.
ArrayFormula will resolve to the following range: {True,True,False,True,False}.
CountIf just takes in any range and in this case can count the number of True values.
So we can wrap CountIf around the values produced by ArrayFormula like this:
=CountIf(ArrayFormula(B2:B6<>""),TRUE)
Further Reading
The other solutions in this thread are either overly complex, or fail in particular edge cases that I've enumerated in this test sheet:
Google Spreadsheet - CountA Test - Demo
For why CountA works the wonky way it does, see my answer here
For me, none of the answers worked for ranges that include both virgin cells and cells that are empty based on a formula (e.g. =IF(1=2;"";""))
What solved it for me is this:
=COUNTA(FILTER(range, range <> ""))
It works for me:
=SUMPRODUCT(NOT(ISBLANK(F2:F)))
Count of all non-empty cells from F2 to the end of the column
Solved using a solution i found googling by Yogi Anand: https://productforums.google.com/d/msg/docs/3qsR2m-1Xx8/sSU6Z6NYLOcJ
The example below counts the number of non-empty rows in the range A3:C, remember to update both ranges in the formula with your range of interest.
=ArrayFormula(SUM(SIGN(MMULT(LEN(A3:C), TRANSPOSE(SIGN(COLUMN(A3:C)))))))
Also make sure to avoid circular dependencies, it will happen if you for example count the number of non-empty rows in A:C and place this formula in the A or C column.
Given the range A:A, Id suggest:
=COUNTA(A:A)-(COUNTIF(A:A,"*")-COUNTIF(A:A,"?*"))
The problem is COUNTA over-counts by exactly the number of cells with zero length strings "".
The solution is to find a count of exactly these cells. This can be found by looking for all text cells and subtracting all text cells with at least one character
COUNTA(A:A): cells with value, including "" but excluding truly empty cells
COUNTIF(A:A,"*"): cells recognized as text, including "" but excluding truly blank cells
COUNTIF(A:A,"?*"): cells recognized as text with at least one character
This means that the value COUNTIF(A:A,"*")-COUNTIF(A:A,"?*") should be the number of text cells minus the number of text cells that have at least one character i.e. the count of cells containing exactly ""
A simpler solution that works for me:
=COUNTIFS(A:A;"<>"&"")
It counts both numbers, strings, dates, etc that are not empty
As far as I can see, most of the solutions here count the number of non empty cells, and not the number of rows with non empty cell inside.
One possible solution for the range B3:E29 is for example
=SUM(ArrayFormula(IF(B3:B29&C3:C29&D3:D29&E3:E29="";0;1)))
Here ArrayFormula(IF(B3:B29&C3:C29&D3:D29&E3:E29="";0;1)) returns a column of 0 (if the row is empty) and 1 (else).
Another one is given in consideRatio's answer.
You can define a custom function using Apps Script (Tools > Script editor) called for example numNonEmptyRows :
function numNonEmptyRows(range) {
Logger.log("inside");
Logger.log(range);
if (range && range.constructor === Array) {
return range.map(function(a){return a.join('')}).filter(Boolean).length
}
else {
return range ? 1 : 0;
}
}
And then use it in a cell like this =numNonEmptyRows(A23:C25) to count the number of non empty rows in the range A23:C25;
In Google Sheets, to count the number of rows which contain at least one non-empty cell within a two-dimensional range:
=ARRAYFORMULA(
SUM(
N(
MMULT(
N(A1:C5<>""),
TRANSPOSE(COLUMN(A1:C5)^0)
)
>0
)
)
)
Where A1:C5 is the range you're checking for non-empty rows.
The formula comes from, and is explained in the following article from EXCELXOR - https://excelxor.com/2015/03/30/counting-rows-where-at-least-one-condition-is-met/
A very flexible way to do that kind of things is using ARRAYFORMULA.
As an example imagine you want to count non empty strings (text fields) you can use this code:
=ARRAYFORMULA(SUM(IF(Len(B3:B14)>0, 1, 0)))
What happens here is that "ArrayFormula" let you operate over a set of values. Using the SUM function you indicates "ArrayFormula" to sum any value of the set. The "If" clause is only used to check "empty" or "not empty", 1 for not empty and 0 otherwise. "Len" returns the length of the different text fields, there is where you define the set (range) you want to check. Finally "ArrayFormula" will sum 1 for each field inside the set(range) in which "len" returns more than 0.
If you want to check any other condition, just modify the first argument of the IF clause.
Make another column that determines if the referenced cell is blank using the function "CountBlank". Then use count on the values created in the new "CountBlank" column.

Performing lookups with merged cells in Google Spreadsheets

Given a set of multi-dimensional data represented in a Google Spreadsheet:
A | 1 | x
A | 2 | y
A | 3 | z
It is pretty straightforward to do a lookup on multiple values using the FILTER and INDEX functions. Doing a lookup for A2 would result in y. However, if the similar data is merged into a single cell:
| 1 | x
A | 2 | y
| 3 | z
Is there a function that would accomplish the same thing? the FILTER method only returns the first row in the implicit set because there is only an A actually on the first row.
If interested, you can view the live sheet here using the 'Rules' sheet.
I have been working on a custom function, but am hoping for something more portable. Helper cells would be fine, as long as I can hide them on a separate sheet.
One option, using the helper cells idea, would be to "normalise" your Rules sheet on a separate (hidden) sheet, and perform lookups on that sheet. Eg, in A3 of that sheet:
=ArrayFormula(HLOOKUP(IF(ROW(Rules!A3:A),Rules!A2),Rules!A2:A,VLOOKUP(ROW(Rules!A3:A),FILTER(ROW(Rules!A3:A),LEN(Rules!A3:A)),1)-1,0))
and fill that formula across to the right as far as required. Note: this formula will only work on the new version of Sheets (which you are using).

Resources