Automatically sum and minus to a new column - google-sheets

In google sheets i use the following formula to get the total of column A in column C for each cell and minus if column B has a value.
C2 =A2
C3 =C2+A3-B3 (Click and drag)
My question is if there is a formula to make column C autocomplete if column A has a value.
--------------------
| A | B | C
--------------------
| 100 | | 100
--------------------
| 150 | | 250
--------------------
| 200 | | 450
--------------------
| 250 | 80 | 620
--------------------
| 300 | | 920
--------------------

This formula should work in cell C2:
=ArrayFormula(IF(ISNUMBER(A2:A250)+ISNUMBER(B2:B250),MMULT(TRANSPOSE(A2:A250)*(ROW(A2:A250) >=TRANSPOSE(ROW(A2:A250))), SIGN(ROW(A2:A250)))-MMULT(TRANSPOSE(B2:B250)*(ROW(B2:B250) >=TRANSPOSE(ROW(B2:B250))), SIGN(ROW(B2:B250))),IFERROR(1/0)))
I've restricted this to the first 250 rows of the sheet but you can amend as necessary. You can calculate the whole column if needed but it will take a while to output if there are a large number of rows

Alternatively, assuming your data starts in row 1, in C1 try this formula:
=ArrayFormula(iferror(if(row(A:A)=1, A1, if(A:A,mmult(transpose(if(transpose(row(A:A))>=row(A:A),A:A+(B:B*-1), 0)),row(A:A)^0),))))
and see if that works ?

Related

Google sheets COUNTIF excluding hidden rows

In google sheets, I have a list of strings (1 per row) where each string is split with 1 character per column, so my sheet looks something like below:
A
B
C
D
E
F
1
F
R
A
N
K
2
P
A
S
S
1
2
I then have this sheet filtered, so Can select only the rows where the first character is F, for example. On another sheet in the same workbook, I have a table of how often each character appears in each column, that looks something like this:
A
B
C
D
E
F
1
Char
Overall
1
2
3
2
A
979
141
304
165
3
B
281
173
69
15
I would like to have this table dynamically update, so that when I filter the first sheet my table shows the frequency only for the strings that meet the filter.
In Excel, this can be accomplished using a combination of SUMPRODUCT and SUBTOTAL but this doesn't work in google sheets. I've seen this done in sheets using helper columns, but I would like the solution to work for a string of an arbitrary number of strings with different lengths without having to change the sheet. Can this be done in Google Sheets?
Thanks!
Hidden cells are assigned with the value 0. One way to solve this is by adding a "helper" column in column A and set all the values in it to 1.
| A | B | C | D | E | F | G
--+--------+------+---+---------+-----+-----+-----
1 | Helper | Char | | Overall | 1 | 2 | 3
--+--------+------+---+---------+-----+-----+-----
2 | 1 | A | | 979 | 141 | 304 | 165
3 | 1 | B | | 281 | 173 | 69 | 15
Now instead of using COUNTIF, use the COUNTIFS formula where the second condition A2:A = 1. For example:
=COUNTIFS([YOUR_CONDITION], A2:A,"=1")
the A column values of hidden rows will calculate as 0, therefore will not be counted.

Create column taking number of values from one column and actual value from another column

I have two columns, A and B, where A contains the number of values for corresponding value in B. I want to create column C that contains the number of values from A but with the value from B. So for example:
| A | | B |
| 2 | | 40 |
| 3 | | 60 |
Should produce:
| C |
| 40 |
| 40 |
| 60 |
| 60 |
| 60 |
So 2 of 40 and 3 of 60. This could be in memory (I only want to use C in a formula, don't really need it as an actual column) or as its own column.
Give a try on below formula-
=ArrayFormula(TRANSPOSE(SPLIT(TEXTJOIN("#",TRUE,REPT(B1:B2&"#",A1:A2)),"#")))

Count if a cell has the same value as another one in the same column

I'm trying to find a formula to count the number of times, in a given row, a cell has the same value as another one in another row (same column)
Basically, I have something like
-----------------
| 1 | S | M | M |
-----------------
| 2 | M | M | M |
-----------------
| 0 | S | M | S |
-----------------
| 1 | S | M | M |
-----------------
| 3 | M | S | M |
-----------------
....
| | M | S | M | // <--- the reference row
-----------------
The numbers 1, 2, 0, 1, 3 are to be calculated with the formula.
1 -> Only one matches the reference row, which is the last M
2 -> There are two matches, first cell and last cell
Etc...
I have tried stuff like =COUNTIF(E5:Z5, "="&INDIRECT(ADDRESS(21,COLUMN(),2))).
But the COLUMN() has the value of the column where this is written and not the value of the column that is being evaluated in the COUNTIF().
If a general way, I can't seem to find a way to refer to the cell being currently evaluated in the COUNTIF.
May someone has the answer :).
Regards
Should be easier with Sumproduct:
=SUMPRODUCT((B1:Z1=B$21:Z$21)*(B1:Z1<>""))

Conditional formatting comparing previous cell value in column while skipping blanks

In Google Sheets I have columns of numeric values and I would like to style a decrease in value as red. I have done so with a simple formula comparing the previous cell's value, but I cannot figure out how to get the last entered value in the column, skipping any blank cells.
| | A |
|---|---------|
| 1 | Numbers |
|---|---------|
| 2 | 100|
| 3 | 75| // Red (decrease from 100)
| 4 | 90|
| 5 | |
| 6 | 70| // Red (decrease from 90)
| 7 | 71|
| 8 | |
| 9 | 68| // Red (decrease from 71)
|10 | 65| // Red (decrease from 68)
My simple formula for A2:A is =AND(A1<>"",A2<A1) which works fine for A3 and A10. What I need is that last A1 in my formula to actually be the last valid cell value, no just the previous.
Please select ColumnA from A2 to wherever suits and Format, Conditional formatting..., Format cells if..., Custom formula is enter:
=and(A2<>"",A2<vlookup(1E+100,A$1:A1,1,1))
and for Formatting style choose red, Done.

Google Spreadsheets: How do you concat strings in an aggregation function

Say I have a table:
A, 1
B, 1
C, 2
D, 1
E, 2
How do I view the table grouping by the 2nd column and aggregating by the first with a comma separated concat function ie:
1, "A,B,D"
2, "C,E"
In both defining a pivot table and using the QUERY syntax, it seems that the only aggregation functions available are numerical aggregations like MIN, MAX, SUM, etc. Can I define my own aggregation function?
You have to add a "Calculated Field" to the pivot table, and then select "Summarise by > Custom". This will make the column names in your formula refer to an array of values (instead of a single value). Then you can type a formula like:
= JOIN(", ", MyStringColumn)
More specifically, if you have the following table:
Create a pivot table by going to "Data > Pivot table", with the following configuration. Ensure "Summarize by" is set to "Custom"!
Another option: if the data is in A2:B, then, say, in D2:
=UNIQUE(B2:B)
and then in E2:
=JOIN(",",FILTER(A$2:A,B$2:B=D2))
which is filled down as required.
There are one-formula, auto-expanding solutions, although they get quite convoluted.
You're right, there's no easy way with pivot tables. This though, will do the trick. Inspired by this brilliant answer here.
First, have a header row and run a sort on column A to group by category.
So far, in your example, we have
| A | B
---+-----------+-----------
1 | CATEGORY | ATTRIBUTE
2 | 1 | A
3 | 1 | B
4 | 1 | D
5 | 2 | C
6 | 2 | E
In column C, let's prep the concatenated strings. Start in cell C2 with the following formula, and fill out vertically.
=IF(A2<>A1, B2, C1 & "," & B2)
...looking good...
| A | B | C
---+-----------+-----------+-----------
1 | CATEGORY | ATTRIBUTE | STRINGS
2 | 1 | A | A
3 | 1 | B | A,B
4 | 1 | D | A,B,D
5 | 2 | C | C
6 | 2 | E | C,E
In column D, let's validate the rows we want to select in a later step, with the following formula, starting in cell D2 and filling out. Basically we are marking the final category rows that carry the full concatenated strings.
=A2<>A3
...almost there now
| A | B | C | D
---+-----------+-----------+----------+-----------
1 | CATEGORY | ATTRIBUTE | STRINGS | VALIDATOR
2 | 1 | A | A | FALSE
3 | 1 | B | A,B | FALSE
4 | 1 | D | A,B,D | TRUE
5 | 2 | C | C | FALSE
6 | 2 | E | C,E | TRUE
Now, lets copy column C and D and paste special as values in the same place. Then add a filter on the whole table and filter out column D for the rows labeled TRUE. Now, remove the filter, delete columns B and D and row 1.
| A | B
---+-----------+-----------
1 | 1 | A,B,D
2 | 2 | C,E
Done. Get ice cream. Watch Road House.

Resources