Fill previous row of a list automatically - google-sheets

I have a list of items with its transaction details. Columns contain date, item name, buy qty, buy rate, sell qty, sell rate etc.
I have added a column indicating the row in which this item has appeared earlier in the list. If item name is in column B, I can fill the formula
=IFNA(MAX(FILTER(ROW(B:B),A:A<INDIRECT(ADDRESS(ROW(),1)),B:B=INDIRECT(ADDRESS(ROW(),2)))),0)
which returns the row number where the current item has appeared. If the item is currently appearing the first time, the function returns 0. That works fine.
I would like to fill the entire column with the formula so that it fills when new row is added or updates when an item name is changed. How can I achieve it?
I tried QUERY to generate the entire column, but could not get the result. What is in my mind is something like
=FILTER(<function>,B:B<>"")
Can you suggest any methods?

try:
=INDEX(IF(A:A="",,IFNA(VLOOKUP(
B:B&COUNTIFS(B:B, B:B, ROW(B:B), "<="&ROW(B:B))-1, {
B:B&COUNTIFS(B:B, B:B, ROW(B:B), "<="&ROW(B:B)), ROW(B:B)}, 2, 0), 0)))
or:
=INDEX(IFNA(IF(COUNTIFS(B:B, B:B, ROW(B:B), "<="&ROW(B:B))=1, 0,
VLOOKUP(B:B, {B:B, ROW(B:B)}, 2, 0))))

Related

Google sheets- How to process and dynamically update the data like in MS Access queries?

community!
I have a table of 3 columns in Google Sheets: Date, Name, Amount.
I want to process data from it, so for every date in the list, there will be a full list of participants and amounts.
If there is no amount for specific date and person, it will be "0", or just leave it blank.
In addition, would like to count the percent of the sum of the amount for each person till the date in the corresponding row.
And it should be dynamically updated, so if a new row added with new name or date or both, so new participant will be added to every date in the output table and new full list of participants for the new date created.
For better understanding, here is the example of the data and output tables
What I've tried?
In MS access query it could be done not so hard.
Here I tried to create 2 new lists of unique dates and participants and connect them through use of CONCATENATE, ARRAYFORMULA, then SPLIT and some QUERY. All this through use of helping column of text, which should be edited manually for every new row...
The code in helping column:
=concatenate(arrayformula(if(isblank($F$3:$F),,";"&$E3&","&$F$3:$F)))
Then split code:
=query(arrayformula(split(transpose(split(TEXTJOIN(";",true,$G$3:$G),";",true,true)),",",true,true)),"Select Col1,Col2 where Col2 is not null order by Col1 ASC",0)
But here I stuck...
Want to pull the amounts for every corresponding date and participant, but...
FILTER reaches first, not existent in the original table combination, and doesn't proceed.
QUERY doesn't fetch the data fully automatically for all the list in the output table.
VLOOKUP gives only one row or complete mess, when use it recursively, or error...
So, how can I do this (if it's possible at all) in google sheets, and so all the output will be fully automatically updated?
Thank you very much!
Update - based on comments
I could not figure out a way to make this through one formula. If this works, you can have a hidden column (say H here) that totals the amount for the person till that date using the formula =QUERY(FILTER($E$3:$G, $F$3:$F = F3, $E$3:$E <= E3), "select sum(Col3) label sum(Col3) ''", 0) in H3.
Then in I3 you can find the % of total till date using the formula =QUERY(FILTER($E$3:$G, $F$3:$F = F3, $E$3:$E <= E3), "select sum(Col3) label sum(Col3) ''", 0)/SUMIF($E$3:$E, E3, $H$3:$H).
Both of the formulae in H3 and I3 need to be dragged down unlike the one in E3. Perhaps someone will be able to offer a better solution.
Previous answer
You can try something like this:
In E3 you can have the formula =ArrayFormula({(FLATTEN(SPLIT(REPT(FILTER(UNIQUE(A3:A)&"✦", UNIQUE(A3:A)>0), COUNTA(UNIQUE(B3:B))), "✦"))), (TRANSPOSE(SPLIT(REPT(JOIN("", FILTER(UNIQUE(B3:B)&"◼︎", UNIQUE(B3:B)<>"")), (COUNTA(UNIQUE(A3:A)))), "◼︎"))), (IFNA(VLOOKUP({(FLATTEN(SPLIT(REPT(FILTER(UNIQUE(A3:A)&"✦", UNIQUE(A3:A)>0), COUNTA(UNIQUE(B3:B))), "✦")))&(TRANSPOSE(SPLIT(REPT(JOIN("", FILTER(UNIQUE(B3:B)&"◼︎", UNIQUE(B3:B)<>"")), (COUNTA(UNIQUE(A3:A)))), "◼︎")))}, {A3:A&B3:B, C3:C}, 2, 0), 0))}). It is a little long but fills Columns E through G dynamically.
Then in H3 you can have the formula =ArrayFormula(IFERROR(((IFNA(VLOOKUP({(FLATTEN(SPLIT(REPT(FILTER(UNIQUE(A3:A)&"✦", UNIQUE(A3:A)>0), COUNTA(UNIQUE(B3:B))), "✦")))&(TRANSPOSE(SPLIT(REPT(JOIN("", FILTER(UNIQUE(B3:B)&"◼︎", UNIQUE(B3:B)<>"")), 3), "◼︎")))}, {A3:A&B3:B, C3:C}, 2, 0), 0))/(SUMIF((FLATTEN(SPLIT(REPT(FILTER(UNIQUE(A3:A)&"✦", UNIQUE(A3:A)>0), COUNTA(UNIQUE(B3:B))), "✦"))), (FLATTEN(SPLIT(REPT(FILTER(UNIQUE(A3:A)&"✦", UNIQUE(A3:A)>0), COUNTA(UNIQUE(B3:B))), "✦"))), G3:G))), "")). This one is dynamic as well.
I tried with your data and added a row on my own and it works.

Incrementing a column in a group using an Array Formula in Google Sheets

I am having difficulties trying to get an array formula to increment a column and restart the increment whenever a condition has been met.
Here is an example of what I am trying to achieve, but this has been done manually. It would be great if this could work in an array formula as we'll be adding more rows and would rather not drag the formula down.
[Update based on feedback of the original post]
Column A contains a list of names and teams.
Names are already organised into teams.
The aim is to go through each row and provide a name with an index starting from 1.
Then when a new team is found in column A the index starts again from 0.
The aim is to make this an Array Formula to avoid having to manually re-add the formula when more rows are added.
Link to sheet:
https://docs.google.com/spreadsheets/d/1K00LRFNrN99fFXQO1tzp8jjZeCuTxwENXNGuLj0K0ao/edit?usp=sharing
Example
Any thoughts or advice on this would be greatly appreciated.
I've added a new sheet ("Erik Help") to your sample spreadsheet. It contains the following formula in B1:
=ArrayFormula({"ID Index"; IF(A2:A="",, IF( REGEXMATCH(LOWER(A2:A),"team"), 0, ROW(A2:A) - VLOOKUP(ROW(A2:A), FILTER(ROW(A2:A), REGEXMATCH(LOWER(A2:A),"team")), 1, TRUE)))})
The header text is included in the formula. You can change it as you like.
If a cell in A2:A is blank, the corresponding cell in B2:B will be as well.
If REGEXMATCH finds a match with "team" in the lowercase rendering of a cell in A2:A, 0 will be returned for the corresponding cell in B2:B.
Otherwise, VLOOKUP will lookup each remaining row number in a FILTERed array containing only those row numbers where the word "team" appears. (In your sample set, that will be 2, 8, 12). When the exact match is not found (which it will not be for any remaining row), TRUE tells VLOOKUP that, since the search array is in strict ascending order, we want it to "bump back" to the most recent value found. In each case, the returned row number of the most recent row containg "team" is then subtracted from the actual row number to produce the recurring incremental numbering 1, 2, 3, etc.
try:
=INDEX(IFNA(1*IF(REGEXMATCH(A2:A, "Day"), 0, REGEXEXTRACT(A2:A, "(\d+)"))))

How to find second large date in google sheet

I have items in A column and dates in B column I need the second largest date in column D if item's name in Column is same
enter image description here
I tried large with with which gave me 0 as a result
Assuming the item to match is in C2 and the data is in A:B, try
=index(sort(filter(B:B, A:A=C2), 1, 0), 2)
Change range(s) to suit and see if that helps?

Query dated expenses, aggregate and sum by months and by cagetories

I need to extent Google sheets annual budget template.
I'd like to its Expenses sheet to pull and fill data from my detailed "Expense Breakdown" sheet automatically to avoid me filling the data twice.
Here is my table I need to fill in(there are many tables like that)
Here is my detailed expenses list
And here is a test spreadsheet https://docs.google.com/spreadsheets/d/1rk03JI6-JRkaH5eixWJaOU5xwcOl2sVE_fMInOIBZhA/edit?usp=sharing
I tried pivot tables to aggregate dates into month and sum expenses, but for some reason, Google doesn't know how to do it.
So the way I see it, I need to place a formula into each cell in D46 to O53 range, where each cell will query expenses list, aggregate data(sum by category and date) there, then match and pull by date(from D2:O2 cella)and category (in C col). Does that make sense?
Can somebody help me to create a formula for that?
first, you need to fix your logic... every category (column A) needs to have unique sub-category (column C) meaning you cant have sub-category called Other for every category because you would output exact same values of Other sub-category for all categories
then use in D46, D4, D15, D24, etc...
=ARRAYFORMULA(IFNA(VLOOKUP(
INDIRECT(ADDRESS(ROW(), 3)&":"&ADDRESS(IFERROR(MATCH("Monthly totals:",
INDIRECT(ADDRESS(ROW(), 3)&":C"), 0)+ROW()-4, ROWS(A:A)), 3)),
QUERY({'Expenses Breakdown'!B:F; {VALUE(TEXT(SEQUENCE(12, 1, 0, 29), "m/d/yyy")),
SEQUENCE(12, 3, 0, 0)&"♀", SEQUENCE(12, 1, 0, 0)}},
"select Col3,sum(Col5)
where Col3 matches '"&TEXTJOIN("|", 1, "0♀",
INDIRECT(ADDRESS(ROW(), 3)&":"&ADDRESS(IFERROR(MATCH("Monthly totals:",
INDIRECT(ADDRESS(ROW(), 3)&":C"), 0)+ROW()-4, ROWS(A:A)), 3)))&"'
group by Col3
pivot month(Col1)+1"), COLUMN(B:M), 0)))
also, you may want to disable rounding:

Google Spreadsheet search in a row

I have table that looks like this:
Name Item1 Date Item 2 Date
John Orange 2/8/2020 Apple 8/8/2020
Bob Banana 6/8/2020
I'm trying to find a way to query that table so that it returns me every item the person has and on what date they got it. Some of the problems I have is that there may be more items added over time so columns would expand.
I have looked into queries but it always forces me to select a column, not a row number. I'm really not sure how to pull this off. I also looked into HLookup but the date field really messes with it.
I am open to changing the structure of the data in the spreadsheet if there is a way to allow to accomplish what I need.
try:
=ARRAYFORMULA(IFNA(VLOOKUP(B13, A1:10, COLUMN(B1:1), 0)))
if you want it in a more readable format:
={ARRAY_CONSTRAIN(
FLATTEN(FILTER(IFNA(VLOOKUP(B13, A1:10, COLUMN(B1:1), 0)),
MOD(COLUMN(B1:1), 2)=0)), (COLUMNS(B1:1)-1)/2, 1),
FLATTEN(FILTER(IFNA(VLOOKUP(B13, A1:10, COLUMN(C1:1), 0)),
MOD(COLUMN(C1:1)-1, 2)=0))}

Resources