Here is a sample sheet with no formulas yet. I want something to return a 1 based on B and C
https://docs.google.com/spreadsheets/d/1-DWYgyrOm3JryO6uq_ZfJzNGcsWFfBBFYcSxjF4NKXI/edit?usp=sharing
Eg1 if B2=Classic Espresso Blend and C2=contains the word "medium" the result is a 1 in F2
Eg2 ( slightly more complex ) if B21=Mixed Medium Subscription and C21 contains "espresso and filter" return a 1 in both H21 and L21. Other possibilities could be "espresso and blend" and "filter and blend" and they would correspond to the 225 columns
This will allow me to tally up at the bottom of each column what's needed
Hope this makes sense
I'm not very experienced. I've used Vlookup for other things in the past but this has a more complex range and I can't get Vlookup to work for this
try this formula in cell D2; it auto applies to whole columns of D2:O
=MAKEARRAY(COUNTA(B2:B),COUNTA(D1:O1),LAMBDA(r,c,IF(REGEXMATCH(LAMBDA(ax,bx,IFS(REGEXMATCH(ax,"Mixed")*REGEXMATCH(INDEX(C2:C,r),"Blend")*REGEXMATCH(INDEX(C2:C,r),"Filter"),"BLEND-"&bx&"|FILTER-"&bx,REGEXMATCH(ax,"Mixed")*NOT(REGEXMATCH(INDEX(C2:C,r),"Blend"))*REGEXMATCH(INDEX(C2:C,r),"Filter"),"ESP-"&bx&"|FILTER-"&bx,REGEXMATCH(ax,"Mixed")*NOT(REGEXMATCH(INDEX(C2:C,r),"Filter")),"BLEND-"&bx&"|ESP-"&bx,LEN(ax),SUBSTITUTE(ax&"-"&bx,"Espresso","ESP")))(regexextract(INDEX(B2:B,r),"([^\s]*?) Subscription"),IFNA(SWITCH(REGEXEXTRACT(INDEX(C2:C,r),"Small|Medium|Large"),"Small",250,"Medium",450,"Large",900),SWITCH(REGEXEXTRACT(INDEX(B2:B,r),"Medium|Large"),"Medium",225,"Large",450))),"(?i)"&INDEX(D1:O1,,c)),1,)))
you may need to test this extensively for 'Mixed' subscription scenarios.
Currently its tested for 3 scenarios of mix-match (BLEND+FILTER, ESP+FILTER, BLEND+ESP) as you can see in rows 21-24 of the screenshot
-
I am trying to make the following without using google-script.
I have this sheet (A):
And I want to get Sheet (B) "auto-updated", every time the sheet (A) changes.
What I am trying to get on sheet (B):
Thank you very much in advance.
Thanks for sharing a sheet. This formula is in A2 on a new tab called MK.Help.
=ARRAYFORMULA(VLOOKUP(SEQUENCE(COUNTA('sheet 1'!A2:A)*4;1;0)/4+2;{ROW('sheet 1'!A2:A)\'sheet 1'!A2:A\'sheet 1'!B2:E\IF(ROW('sheet 1'!A2:A);'sheet 1'!B1:E1)};MOD(SEQUENCE(COUNTA('sheet 1'!A2:A)*4;1;0);4)*{0\1\1}+{2\3+4\3}))
This solution is designed for 4 columns. In your case, there are no empty cells, but if there were, you would use a query around the solution like this:
=ARRAYFORMULA(QUERY(VLOOKUP(SEQUENCE(COUNTA('sheet 1'!A2:A)*4;1;0)/4+2;{ROW('sheet 1'!A2:A)\'sheet 1'!A2:A\'sheet 1'!B2:E\IF(ROW('sheet 1'!A2:A);'sheet 1'!B1:E1)};MOD(SEQUENCE(COUNTA('sheet 1'!A2:A)*4;1;0);4)*{0\1\1}+{2\3+4\3});"where Col2 is not null"))
It can also be built for an unknown number of columns. If that's something you need, let me know.
I call this a "retabulation" problem and it comes up often enough that i tried to create a lesson for my method. Here's a link to that lesson. It's a bit of a work in progress, but maybe it will help.
https://docs.google.com/spreadsheets/d/1EV_iziWtrTrkPdwY0FI2l0lzTFt-IRQHCOg1punnN5c/edit#gid=0
here the formula:
= arrayformula(
{
"Date","Country","Value";
split
(
transpose
(
split
(
textjoin
(
":",false,filter(A2:A&","&B1:D1&"," & B2:D,A2:A<>"")
)
,":",true,false
)
)
,",",true,false
)
}
)
To start, this is my first time posting and so please let me know if I can fix my post in any way to make it easier to answer.
I am trying to create an auto-expanding array formula
I have a sheet with my investment asset mix that including amounts of shares owned of each particular stock, and a sheet that tracks when I receive dividends. My goal is to write an automatically expanding array formula that will sum up the amount of shares that own of a stock on the date a dividend is received and return that value. I have written three different formulas that all accomplish this but none of them will auto-expand as an array.
I'm sure there are a lot of solutions I've overlooked. To boil it down, I need an expanding array formula that will sum the "Shares" column of my asset mix sheet ('Asset Mix'!D2:D, or 'AssetMixShares') conditionally. The name of the stock entered in 'Dividends'!C2:C needs to match the name of the stock in 'Asset Mix'!A2:A (or the named range 'AssetMixStocks'). It then needs to check the dates in 'Asset Mix'!C2:C (or 'AssetMixDates') against the dates in 'Dividends'!A2:A and sum all share amounts where the purchase date is less than (earlier than) the Ex-Dividend Date.
I could probably write some sort of vlookup array on the "Running Total" column -- 'Asset Mix'!E:E -- that would solve the issue, but I'm hoping to eliminate that column. I feel very strongly that what I'm trying to do should be possible without the help of a running total column -- I just don't have the knowledge.
I have tried countless functions and formulas, but the four that I currently have in my example worksheet are SUM, SUMPRODUCT, DSUM, and QUERY.
Attempt 1
SUM and IF
=ArrayFormula(SUM(IF('Asset Mix'!A:A=C2,IF('Asset Mix'!C:C<A2,'Asset Mix'!D:D))))
Attempt 2
SUMPRODUCT
=({arrayformula(SUMPRODUCT(--((AssetMixStock=(indirect("C"&ROW())))*(AssetMixDate<(indirect("A"&ROW())))),AssetMixShares))})
Attempt 3
DSUM
=DSUM('Asset Mix'!A:E,"Shares",{"Date","Stock";"<"&A2,C2})
Attempt 4
QUERY
=arrayformula(query(AssetMix,"Select sum(D) where A = '"&C2:C&"' and C < date'"&(text(year(A2:A),"0000") & "-" & text(month(A2:A),"00") & "-" & text(day(A2:A),"00"))&"' label sum(D) ''",0))
These will all work, as long as I manually drag the formula down, but I want to write some sort of formula that will auto-expand to the bottom of the Dividends sheet.
I have tried to create a Dummy sheet that has both of the relevant sheets. Please let me know if you can access it -- the link is below.
https://docs.google.com/spreadsheets/d/1wlKffma0NJ0KrlWxyX_N20y62azsGpFp3enhmjzJK1Q/edit?usp=sharing
Thanks so much for getting this far and any help you can provide!
We can focus in the first formula to understand a way to make it "self-expandable". As we see it contains references to the cells A2 and C2 in "Dividends" sheet:
=ArrayFormula(SUM(IF('Asset Mix'!A:A=C2,IF('Asset Mix'!C:C<A2,'Asset Mix'!D:D))))
Every time some data appears in these columns (A and C), the formula should work. We can control the presence of the formula by onEdit trigger, if editing is manual. Consider the code:
function onEdit(e) {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
if (sheet.getName() == 'Dividends') {
var row = e.range.getRow();
for (var offset = 0; offset < e.range.getHeight(); offset++) {
sheet.getRange(3, 10).copyTo(sheet.getRange(row + offset, 10));
}
}
}
It checks any modification on the sheet "Dividends" and copies required formula to the modified row(s). This way the formula is expanded for other rows in use.
Well, it's solved! I'll leave this up in case anyone else has the same question.
A kind soul explained the magic of MMULT() to me, and wrote this solution.
=ARRAYFORMULA(MMULT((C2:C=TRANSPOSE('Asset Mix'!A2:A))*(A2:A>TRANSPOSE('Asset Mix'!C2:C)),N('Asset Mix'!D2:D))
I've been keeping a log of something for a game which regularly updates. For the most part, I've been handling a lot of it manually and slowly over time I add in some automation to reduce the burden of manual input (and reduce the chance of data entry error).
Specifically, I'm trying to find a way to streamline the process on the following sheet: https://docs.google.com/spreadsheets/d/1nbP0VgXjHozZikZFejERfw_dkilj26pyfhK29uaPAIQ/edit#gid=688163851
How this sheet is set up right now:
I manually enter the "Item Name" when something new is added
I manually enter the "Weapon Type" when something new is added
I manually enter in the "Date" when something new is added and when it returns
The first step I could automate relatively easily with something like this:
=FILTER('DUCAT COST HISTORY'!A2:A,'DUCAT COST HISTORY'!B2:B="Weapon")
The second step could similarly be automated if I added another column to the 'DUCAT COST HISTORY'! sheet which further specified the item.
What I'm having trouble with is how to automate entering the dates on this particular sheet. And the other sheets similar to this one.
The way things work right now is I use a script to grab the new inventory from a .php the developers put together and that information goes in 'TEST SHEET, PLEASE IGNORE'!. After that, the 'DUCAT COST HISTORY'! sheet will automatically update for all existing items, though I still have to manually add new items to the bottom of the list (not a big problem).
What I want is a formula which can look at a range and then return from a different range. Ex.: Let's say I want to look up the "Mara Detron" - I know it's appeared on 7 different dates through the simple use of =COUNTA(F2:2) on the 'DUCAT COST HISTORY'! sheet.
When I try to use INDEX(MATCH()), it's only going to return the value of the cell specified, so I can't use it to check the whole row. The best solution I can come up with is to manually match the item to its respective row and then run the filter like this:
=FILTER('DUCAT COST HISTORY'!F1:1,NOT(ISBLANK('DUCAT COST HISTORY'!F__:__)))`
Where _ would be the corresponding row value.
That's about as far as I got in automating them, though. Is there a way to get the formula to automatically match the corresponding row?
You can use OFFSET with MATCH to get the right row for the filter condition:
=
FILTER(
'DUCAT COST HISTORY'!$I$1:$1,
NOT(
ISBLANK(
OFFSET(
'DUCAT COST HISTORY'!$I$3:$3,
MATCH(A2, 'DUCAT COST HISTORY'!$A$3:$A, 0) - 1,
0,
1
)
)
)
)
This goes in the cell C2 and you just propagate it downwards. To tolerate empty cells you can add IF:
=
IF(
ISBLANK(A2),
,
FILTER(
'DUCAT COST HISTORY'!$I$1:$1,
NOT(
ISBLANK(
OFFSET(
'DUCAT COST HISTORY'!$I$3:$3,
MATCH(A2, 'DUCAT COST HISTORY'!$A$3:$A, 0) - 1,
0,
1
)
)
)
)
)
You were totally right about the chance of data entry error: you've used row 22 instead of row 32 for Prisma Skana. :)
How to create a Google Spreadsheet sum() which always ends on the cell above, even when new cells are added? I have several such calculations to make on each single column so solutions like this won't help.
Example:
On column B, I have several dynamic ranges which has to be summed. B1..B9 should be summed on B10, and B11..B19 should be summed on B20. I have tens such calculations to make. Every now and then, I add rows below the last summed row , and I want them to be added to the sum. I add a new row (call it 9.1) before row 10, and a new raw (let's call it 19.1) before row 20. I want B10 to contain the sum of B1 through B9.1 and B20 to contain the sum of B11:B19.1.
On excel, I have the offset function which does it like charm. But how to do it with google spreadsheet? I tried to use formulas like this:
=SUM(B1:INDIRECT(address(row()-1,column(),false))) # Formula on B10
=SUM(B11:INDIRECT(address(row()-1,column(),false))) # Formula on B20
But on Google Spreadsheet, all it gives is a #name error.
I wasted hours trying to find a solution, maybe someone can calp?
Please advise
Amnon
You are probably looking for formula like:
=SUM(INDIRECT("B1:"&ADDRESS(ROW()-1,COLUMN(),4)))
Google Spreadsheet INDIRECT returns reference to a cell or area, while - from what I recall - Excel INDIRECT returns always reference to a cell.
Given Google's INDIRECT indeed has some hard time when you try to use it inside SUM as cell reference, what you want is to feed SUM with whole range to be summed up in e.g. a1 notation: "B1:BX".
You get the address you want in the same way as in EXCEL (note "4" here for row/column relative, by default Google INDIRECT returns absolute):
ADDRESS(ROW()-1,COLUMN(),4)
and than use it to prepare range string for SUM function by concatenating with starting cell.
"B1:"&
and wrap it up with INDIRECT, which will return area to be sum up.
REFERRING TO BELOW ANSWER from Druvision (I cant comment yet, I didn't want to multiply answers)
Instead of time consuming formulas corrections each time row is inserted/deleted to make all look like:
=SUM(INDIRECT(ADDRESS(ROW()-9,COLUMN(),4)&":"&ADDRESS(ROW()-1,COLUMN(),4)))
You can spare one column in separate sheet for holding variables (let's name it "def"), let's say Z, to define starting points e.g.
in Z1 write "B1"
in Z2 write "B11"
etc.
and than use it as variable in your sum by using INDEX:
SUM(INDIRECT(INDEX(def!Z:Z,1,1)&":"&ADDRESS(ROW()-1,COLUMN(),4))) - sums from B1 to calculated row, since in Z1 we have "B1" ( the 1,1 in INDEX(...,1,1) )
SUM(INDIRECT(INDEX(def!Z:Z,2,1)&":"&ADDRESS(ROW()-1,COLUMN(),4))) - sums from B11 to calculated row, since in Z2 we have "B11" ( the 2,1 in INDEX(...,2,1) )
please note:
Separate sheet named 'def' - you don't want row insert/delete influence that data, thus keep it on side. Useful for adding some validation lists, other stuff you need in your formulas.
"Z:Z" notation - whole column. You said you had a lot of such formulas ;)
Thus you preserve flexibility of defining starting cell for each of your formulas, which is not influenced by calculation sheet changes.
By the way, wouldn't it be easier to write custom function/script summing up all rows above cell? If you feel like javascripting, from what I recall, google spreadsheet has now nice script editor. You can make a function called e.g. sumRowsAboveMe() and than just use it in your sheet like =sumRowsAboveMe() in sheet cell.
Note: you might have to replace commas by semicolons
NOTE
After testing this answer, it will only work if the sum is in a different column due to a circular dependency error. Otherwise, the solution is valid.
It's a bit of algebra, but we can take advantage of Spreadsheets' lower right corner drag.
=SUM(X:X) - SUM(X2:X)
Where X is the column you are working with and X2 is your ending point. Drag the formula down and Sheets will increment the X2, thus changing the ending point.
*You mentioned that you had tens of such calculations to make. So in order to fit your exact need, we would subtract your last summation to get that "middle" range that we wanted.
e.g.
B1..B9 should be summed on B10, and B11..B19 should be summed on B20
Because of the circular dependency error mentioned earlier, I can't solve it exactly and put the sum on the same line, but this could work in other cases where the sum needs to be stored in a different column.
=SUM(B:B) - SUM(B9:B) //Formula on C10 (Sum of B1..B9)
=SUM(B:B) - SUM(B19:B) - B10 // Formula on C20 (Sum of B11..B19)
This is based on #PsychoFish, here is the solution:
=SUM(INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")&"3:"&ADDRESS(ROW()-1,COLUMN(),4)))
Simply replace the "3:" for the row to start sum.
#PsychoFish is correct but cannot be dragged and copied since the column is literal and hard coded, and #Druvision was in the right direction but was wrong... basically ended up with the same issue of having to re-enter the ranges and then sliding the formulas over and over.
You guys are making this harder than you have to. I just leave a couple of empty rows above by "sum" row (you can format them to be filled with color or something to keep them from being inadvertently used), then just add your new rows just above those special rows.
Agree with what user7255446 said that everyone is overcomplicating. Keep one row blank before your sum row. And then whenever you want to insert a new row, click on your blank row and use "Insert row ABOVE" instead of "insert row below". Your sum formula will automatically adjust.
Example: I want to sum from B1 to B19. I leave row 20 blank. In cell B21, put =SUM(B1:B20). Then if you ever need to insert a new row, click on row 20 and choose "Insert row above". The sum formula automatically changes to =SUM(B1:B21) for you. And of course your sum cell is now B22.
General syntax:
=SUM(INDIRECT(cell_reference_as_string1 &":"& cell_reference_as_string2)
with for example:
cell_reference_as_string1 = ADDRESS(ROW(),COLUMN(),4)
cell_reference_as_string2 = ADDRESS(ROW()-1,COLUMN(),4)
I like how #abernier describes the general solution. So far only alphabet-based A1 notation (A being first column, 1 being first row) are being used. It keeps confusing me, especially when thinking of number of columns left of another column. I like the number-based R1C1 notation much better. To use R1C1 notation for INDIRECT, you need to pass FALSE like so:
=SUM(INDIRECT("R1C"&COLUMN()&":R"&(ROW()-1)&"C"&COLUMN(), FALSE))
I hope you find that helpful, too.
OFFSET() can be used/abused for this purpose. Give it the absolute address of the top left of the range, 0 and 0 for the row/column offsets, and the height/width of the range. Let OFFSET() be the argument to SUM(), SUMIF(), etc.
ROW() and COLUMN() are handy when computing the desired height/width. Be sure to remember to subtract one to exclude the current row/column, or else you're liable to end up with a circular reference. If you have header rows/columns, subtract for them too.
For example, to sum everything from A2 down, excluding the current row, try:
=SUM(OFFSET($A$2,0,0,ROW()-2,1))
To sum everything to the left of the current cell, wherever it may be, try:
=SUM(OFFSET(INDIRECT("RC1",FALSE),0,0,1,COLUMN()-1))
Now let's flip things upside down, to show that this works in the other direction. Suppose you want to sum the B column, starting below the current row, until (and including) row #10. Try this:
=SUM(OFFSET($B$10,ROW()-9,0,10-ROW(),1))
You can avoid negative offsets, while still summing column B:
=SUM(OFFSET(INDIRECT("RC2",FALSE),1,0,10-ROW(),1))
Remove the "2" to instead sum the current column:
=SUM(OFFSET(INDIRECT("RC",FALSE),1,0,10-ROW(),1))
(Credit to Tom Sharpe, who commented above.) INDEX() can be used in a range expression. You might prefer this over OFFSET(), so I'm putting it here. The following sums everything from G1 down to the row above the current:
=SUM(G1:INDEX(G:G,ROW()-1))
Here's how I do it.
This formula does not require you to edit or enter anything about the particular column you would like to sum
=SUM(INDIRECT(CONCATENATE(address(1,column(),4),":",LEFT(address(1,column(),4),1))&ROW()-1))
The answer by #PsychoFish led me in the correct way.
The only issue that I had to rewrite the formula again from each column and each sum. So here is the improved formula, which sums the previous 9 cells on the same column, without hardcoding the column or row numbers:
=SUM(INDIRECT(ADDRESS(ROW()-9,COLUMN(),4)&":"&ADDRESS(ROW()-1,COLUMN(),4)))
The only issue is that I had to rewrite the formulas if someone adds or deletes a row. In this case I should change 9 to 10 or 8 corrspondingly.