I have the columns below (column I, J and K):
Qty1, Qty2, TotalQty
Where TotalQty for each row is simply the sum of the cells in columns I and J (Qty1 and Qty2).
How can I write an array formula that simply sums column I and J for each row?
I have the below but this just repeats the sum for the row the arrayformula is applied in to all cells below it:
ARRAYFORMULA(IF(A2:A="","",SUM($I2:$J2)))
What I want it to do is calculate each row's I,J sum separately.
BYROW or MAP will do the trick. For this case, you can try MAP:
=MAP(A2:A,I2:I,J2:J,LAMBDA(ax,ix,jx,IF(ax="","",ix+jx)))
try in K2:
=ARRAYFORMULA(I2:I10+J2:J10)
with if it would be:
=ARRAYFORMULA(IF(A2:A="";;I2:I+J2:J))
you can shorten it:
=INDEX(IF(A2:A="";;I2:I+J2:J))
or use a different approach like:
=QUERY(I2:J; "select I+J label I+J''")
or some more advanced techniques like:
=BYROW(I2:J; LAMBDA(x; SUM(x)))
or the old way:
=INDEX(MMULT(I2:J*1; SEQUENCE(2; 1; 1; ))
Related
I am a beginner in google sheets and I couldn't get around this formula. I have range of cells and I want to subtract last non empty cell to first cell (Z-A), here is the image:
As the values are updated in columns C, D, E and so on. I want to get the last non empty cell (from right) and subtract the values by moving backward (left). Like this:
sub = 10(Column G)-0(Column F)-10(Column E)-0(Column D)-10(Column C)
Can we devise a formula which will get the last non empty cell and subtract values until the first value? Here is the link to the sample sheet Thank you
try:
=LOOKUP(1, INDEX(1/(C2:F2<>"")), C2:F2)-(SUM(C2:F2)-
LOOKUP(1, INDEX(1/(C2:F2<>"")), C2:F2))
Suggestion: Use a custom function
You may use the following script as a custom function to get the difference between the value of the last cell and the sum of the other cells:
function SUBTRACTFROMLASTNUMBER(data) { //you can rename the custom function name
var sum = 0;
var data2 = data[0].filter(x => {
return (x != "") ? x : null;
}); //filtered data
var lastNumber = data2.pop(); //last number
data2.map(x => sum += x); //sums the remaining values
return (lastNumber - sum); //returns the output
}
This custom function extracts the selected data from the sheet and then separates the value of the last cell using pop() and then filters and sums the remaining data using filter() and map() and then subtracts the sum from the value of the last cell.
Usage
You may use this function as:
=SUBTRACTFROMLASTNUMBER(<range>)
Reference:
How to Manipulate Arrays in JavaScript
I want to display a letter grade based on a percentage grade. So Say one cell (A1) has a value of 96% and based on A1's value the second cell (A2) would Give an output of "A".
I've tried =if(ISBETWEEN(A1,90%,100%)=TRUE),,"A" (this formula would go into cell A2)
and if anyone could help me with having multiple if formula statements and putting them in one cell.
A = 100%-90%
B = 89%-80%
C = 79%-70%
D = 69%-60%
F = 59%-0%
try:
=INDEX(IFNA(VLOOKUP(A2:A, {0,"F"; 0.6,"D"; 0.7,"C"; 0.8,"B"; 0.9,"A"}, 2, 1)))
How to calculate a cell with % based on another cell's value?
For example: Let's say A2 = 5000, I want C2 to calculate 1% of A2 if B2 = yes
Will the following work?
SUMIFS(B2,"TRUE",C2*1%)
In C2, Try something like
=sumproduct(B2="yes", A2*0.01)
or
=if(B2="yes", A2*0.01,)
and see if that works?
You can use the following as a formula for C2:
=IF(B2="yes", A2*0.01, 0)
I have a google sheet that I want to add a conditional formatting, but the merged cells kinda of get in the way.
In my spreadsheet, I have the columns B,C,D and E where i'm using a formula in column B for when columns C,D and E are "done" column B gets a green background. The problem is, for some rows, columns D and E are merged.
This is the formula I'm using in column B
=and(C:C="done",D:D="done")
My desired result is: When the columns D and E are not merged, column B only gets the green background if columns C,D and E are "done", or else, it stays blank.
When D and E are Merged : B only gets green background if C and DE are "done", or else, stays blank.
Thanks in advance!!
If E always has a non-empty value when D and E aren't merged, you can use that to check for when E is blank using ISBLANK(E:E):
=AND(C:C="done", D:D="done", OR(E:E="done", ISBLANK(E:E)))
Otherwise, there isn't a way to test if a cell is part of a merged range without using a custom function, which is far less efficient, but could technically work with the assistance of a new helper column (e.g. column Z):
/**
* #customfunction
*/
function ISMERGED(cellAddress) {
var cell = SpreadsheetApp.getActiveSheet().getRange(cellAddress);
return cell.isPartOfMerge();
}
Where column Z (or whatever you choose for your helper) has the following in every relevant row (assuming data starts in row 1):
=ISMERGED(ADDRESS(ROW(E1), COLUMN(E1)))
=ISMERGED(ADDRESS(ROW(E2), COLUMN(E2)))
=ISMERGED(ADDRESS(ROW(E3), COLUMN(E3)))
...and so on.
And then for formatting B, use:
=AND(C:C="done", D:D="done", OR(E:E="done", $Z:$Z))
WARNING: If you toggle between merging and unmerging, Z wont contain the correct values immediately, though, because custom functions only re-compute when input changes (and the address of the cell wont in this case).
Update
Here's how you could compute your helper for the whole column downward, by row, by just putting the value in the topmost cell (ex: =ISMERGED("E:E") in cell Z1):
/**
* #customfunction
*/
function ISMERGED(rangeAddress) {
var range = SpreadsheetApp.getActiveSheet().getRange(rangeAddress);
var numCols = range.getNumColumns();
var numRows = range.getNumRows();
var result = [];
for (var i=0; i < numRows; i++) {
var rowRange = range.offset(i, 0, 1, numCols);
result.push(rowRange.isPartOfMerge());
}
return result;
}
best you can get is:
=OR(AND(C:C="done", D:D="done", E:E="done"),
AND(C:C="done", D:D="done", ISBLANK(E:E)))
which will work unless 4th row:
but you could pre-populate the whole E column with =CHAR(1) and then:
Say I have a spreadsheet with 18 rows, and I can hard code the range of the last 7 values in column D and average their values like so:
=AVERAGE(D12:D18)
How then could I do the same without hard coding them, so it'll work as I add more rows?
If you want to calculate the average or sum, there's no need for a script. You can accomplish this with an array filter as well. The following formula calculates the average over the last 7 rows in the A column:
=AVERAGE(FILTER(A:A;ARRAYFORMULA(ROW(A:A)>COUNT(A:A)-7+1)))
This assumes that the data starts at row 1. Otherwise you have to change the latter constant in the formula to the row number where the data starts.
You can also use OFFSET() as this Webapps.SO answer does for aggregating over the last X cells of a row.
For the opposite---aggregating the last X cells of a column---the OFFSET params just need moving around. Here's a command should should work for your example (this assumes your data starts at D2 and goes to D18):
=iferror(average(offset($D$2, max(0, count($D$2:$D18)-7), 0, 7, 1)), 0)
Turns out you can use your own code. Messy, but it works:
function lastValues(column, num) {
var lastRow = SpreadsheetApp.getActiveSheet().getMaxRows();
var values = SpreadsheetApp.getActiveSheet().getRange(column + "1:" + column + lastRow).getValues();
for (; values[lastRow - 1] == "" && lastRow > 0; lastRow--) {};
arr = [];
for(i=0; i < num+1; i++){
arr.push(values[lastRow - i]);
};
return arr;
};