Google Spreadsheet Last X Rows - google-sheets

I'm using charts in Google Spreadsheet to plot the last 90 days of data. However, when adding new data it's outside the charts currently selected range of A1:A90. Is there a function I can use to select the last 90 rows of data in a column of a Google Spreadsheet?

You can create a new sheet and use the QUERY function to get the last 90 rows:
If column A is unique and sortable, you simply sort your data in reverse order and take the top 90 rows, and then reverse sort it again:
=SORT(QUERY(Sheet1!A:Z,"order by A desc limit 90"),1,1)
If that is not the case, then you need to calculate the offset by finding the last row and subtracting 90 from it, and then query the data to return 90 rows starting from that offset:
=QUERY(Sheet1!A:Z,"limit 90 offset "&(COUNT(Sheet1!A:A)-90))
You can then use this sheet to generate your chart.

No to my best knowledge, unless you write own google spreadsheet script and use ranges. Script can programatically feed chart with data by
opening Spreadsheet & Sheet
getting data
isolating range, e.g. last 90 rows
feeding with that range the chart component

You can also use the SORTN function to achieve the required result if one or more of your columns are sortable.
In the example below the last 90 rows are listed in descending order with the first sort reverse sorting them back to ascending order:
SORT(SORTN(Sheet1!A:Z,90,0,1,0),1,1)
If you wish to list the last 90 rows after filtering on a particular attribute you can combine the above with a filter function:
SORT(SORTN(FILTER(Sheet1!A:Z,filter to be applied),90,0,1,0),1,1)
If required you can also list the second last 90 records (91 to 180 from last) by applying two SORTN functions as follows:
SORTN(SORTN(Sheet1!A:Z,180,0,1,0),90,0,1,1)

Related

How to dynamically define the Horizontal ending cell of the last Column in a range for reuse in a Google Sheets formula?

I need a formula to dynamically compute the number of empty Columns from the datasets.
Basically I would need the same as the vertical dynamic end row limit but for column.
For example I would need =COUNTA(A1:A) but for colums for example =COUNTA(A1:BZ) but without absolute reference to the column.
I have multiple datasets with a varying number of columns.
Each dataset also has a varying number of empty columns.
For example:
Datasets
# Columns
# Empty Columns
Dataset #1
11
4
Dataset #2
5
1
Dataset #3
17
6
...
...
...
Dataset #n
19
7
I have the followwing formula in Cell A1 as of now:
=SUM(COUNTIF(B2:K,"<1"))
With =COUNTA(B2:B) dragged to column J/ Row J2 as so:
I don't know if there's a way to set the Horizontal ending cell as dynamic and couldn't find another similar question on SO.
Thank you for sharing the solution if you know one.
if lets say you wish to get the dynamic blank column count of the tab Sheet1; try:
=COUNTIF(BYCOL(INDIRECT("Sheet1!1:"&MAX(ROWS(Sheet1!A:A))),LAMBDA(z,COUNTA(z))),0)
Just found some helping insight here:
Google Sheets - Index & Match, Multiple Columns, Merge Data Similar to SQL Join - Part 2
https://youtu.be/2T__Yyn_Bdw?t=600
A1:1 is the horizontal dynamic / open ended reference equivalent of
A1:A or A:A vertical dynamic / open ended reference.
The :1 selecting the whole row without ending column reference.

Can change shape of range with ARRAYFORMULA() in Google Sheets?

My intention is to convert a single line of data into rows consist of a specific number of columns in Google Sheets.
For example, starting with the raw data:
A
B
C
D
E
F
1
id1
attr1-1
attr2-1
id2
attr2-1
attr2-2
And the expected result is:
(by dividing columns by three)
A
B
C
1
id1
attr1-1
attr1-2
2
id2
attr2-1
attr2-2
I already know that it's possible a bit manually, like:
=ARRAYFORMULA({A1:C1;D1:F1})
But I have to start over with it every time the target range is moved OR the subset size needs to be changed (in the case above it was three)!
So I guess there will be a much more graceful way (i.e. formula does not require manual update) to do the same thing and suspect ARRAYFORMULA() is the key.
Any help will be appreciated!
I added a new sheet ("Erik Help") where I reduced your manually entered parameters from two to one (leaving only # of columns to be entered in A2).
The formula that reshapes the grid:
=ArrayFormula(IFERROR(VLOOKUP(SEQUENCE(ROUNDUP(COUNTA(7:7)/A2),A2),{SEQUENCE(COUNTA(7:7),1),FLATTEN(FILTER(7:7,7:7<>""))},2,FALSE)))
SEQUENCE is used to shape the grid according to whatever is entered in A2. Rows would be the count of items in Row 7 divided by the number in A2 (rounded to the nearest whole number); and the columns would just be whatever number is entered in A2.
Example: If there are 11 items in Row 7 and you want 4 columns, ROUNDUP(11/4)=3 rows to the SEQUENCE and your requested 4 columns.
Then, each of those numbers in the grid is VLOOKUP'ed in a virtual array consisting of a vertical SEQUENCE of ordered numbers matching the number of data pieces in Row 7 (in Column 1) and a FLATTENed (vertical) version of the Row-7 data pieces themselves (in Column 2). Matches are filled into the original SEQUENCE grid, while non-matches are left blank by IFERROR
Though it's a bit messy, managed to get it done thanks to SEQUENCE() function anyway.
It constructs a grid by accepting number of rows/columns input, and that was exactly I was looking for.
For reference set up a sheet with the sample data here:
https://docs.google.com/spreadsheets/d/1p972tYlsPvC6nM39qLNjYRZZWGZYsUnGaA7kXyfJ8F4/edit#gid=0
Use a custom formula
Although you already solved this. If you are doing this kind of thing a lot, it could be beneficial to look into Apps Script and custom formulas.
In this case you could use something like:
function transposeSingleRow(range, size) {
// initialize new range
let newRange = []
// initialize counter to keep track
let count = 0;
// start while loop to go through row (range[0])
while (count < range[0].length){
// add a slice of the original range to the new range
newRange.push(
range[0].slice(count, count + size)
);
// increment counter
count += size;
}
return newRange;
}
Which works like this:
The nice thing about the formula here is that you select the range, and then you put in a number to represent its throw, or how many elements make up a complete row. So if instead of 3 attributes you had 4, instead of calling:
=transposeSingleRow(A7:L7, 3)
you could do:
=transposeSingleRow(A7:L7, 4)
Additionally, if you want this conversion to be permanent and not dependent on formula recalculation. Making it in run fully in Apps Script without using formulas would be neccesary.
Reference
Apps Script
Custom Functions

Google Sheets - Using ArrayFormula with Conditional and Embedded Formula

This may be far more simple than I think. What I'm trying to do is use the ArrayFormula to copy an existing formula to all the rows in a particular column.
To begin with, I have several columns along the lines of:
What I'm doing is adding the first three numbers and multiplying them by the fourth. I also have an IF condition in which, if the fourth column is blank, I leave the value in column 5 blank.
Now, I'm trying to convert this to an ArrayFormula to repeat this for all the rows in column 5.
So I went from:
=IF(ISBLANK(E2)=TRUE,,SUM((B2+C2+D2)*E2))
to this:
=ArrayFormula(IF(ISBLANK(E2:E)=TRUE,,SUM((B2+C2+D2)*E2)))
But what this does is, when I add a new row, ALL of the values in column 5 are set to the same value. Here is an example:
So, my first thought was to set the range on the SUM formula and change it to:
=ArrayFormula(IF(ISBLANK(E2:E)=TRUE,,SUM((B2:B+C2:C+D2:C)*E2:E)))
But that just makes all the values to sum of all of what the individual values should be...so, in my example, it works out to 435 (60 + 135 + 240).
What am I doing wrong here? The values in column 5 should be different in each row (e.g., row 2 should be 135 and row 3 should be 240).
Thanks!
use:
=ARRAYFORMULA(IF(ISBLANK(E2:E)=TRUE,, (B2:B+C2:C+D2:D)*E2:E))
SUM is not supported under AF

Match 3 cells pair with other 3 cells pair. Google Sheets

I'm using Google Sheets for our production price calculation and we are getting new orders with different data every week.
I have all the price calculations sorted out but sometimes there are the same data in orders that already been in the past and I have to manually search for it and use the same price if it exists.
As you can see in the example above, when I enter in the selected cell data "100", I have to check if it already exists in cells above (all three cell in the same row) and if it does enter its price on the cell on the right("=" sign), if it doesn't it could say "new" or be left empty.
I looked at the INDEX and MATCH functions but they don't seem to do the trick.
Do you have any suggestions? Of course the formula should be easily copied to every next cell down when new data and orders come in.
Approach
In this case it's useful to have an index for your table. I created a simple one that concatenates the 3 values you have with the & operator. You can see in the table below for the complete formula. Just drag it down to the whole table to make it automatic.
For the price calculation then I am using a VLOOKUP. It will search for the index generated by the three values in the new row and get the corresponding Price if ti exists. If not, it will print NEW. That's just a placeholder of course, you can edit it as you want. I put the first cell of your table as absolute reference in the VLOOKUP formula so you can drag it down and it will always get its upper (already filled) portion.
Table
INDEX X Y Z Price
11010030 110 100 30 1
500300100 500 300 100 2.3
12030010 120 300 10 1.2
500300100 500 300 100 2.3
12030010 120 300 10 1.2
11010030 110 100 30 1
3004510 300 45 10 NEW
11010030 110 100 30 1
=B10&C10&D10 =IFERROR(VLOOKUP(A10, $A$2:I9,5,0), "NEW")
Based on the correct initial thought by Alessandro, please use the following single arrayformula in cell E2
=ArrayFormula(IF(LEN(A2:A)>0,
IF(LEN(D2:D)>0,
VLOOKUP(A2:A&B2:B&C2:C, {A2:A&B2:B&C2:C,D2:D},2,0),"new"),""))
The formula works as a helper column, only showing you what price to use in column D (if it previously exists) or lets you know that you need to calculate a new one.
Functions used:
VLOOKUP
ArrayFormula
LEN
IF

Google Sheets Custom Color for top 10 values when values are not sorted and contain formulas

I'm having a problem getting Google Sheets to color the top 10 values in multiple columns.
I have columns that contain the following stock information:
Account,Ticker,Symbol,YTD,1 Yr,5 Yr,10 Yr,Risk,Return,Star Rating
Example:
T. Rowe Price U.S. Large-Cap Core Fund,TRULX,2.2,3.93,12.3,12.67,2,5,5
I would like to color the top 10 values for the 1 Yr, 5 Yr, 10 Yr columns.
I thought the function large(L:L,10) would do the job but it seems that the function needs to be sorted by the field that its coloring and it doesn't seem to work if the values in the column are formulas. All the data in these fields but the ticker are formulas.
Assume that data column is column A.
Here's step by step solution:
select your range, like: A2:A500
go menu Format → Conditional formatting...
Choose 'Format cells if...' = Custom formula is
Paste this formula: =$A2>=large($A$2:$A,10)
To check if the formula works right, you may use this formula, paste it into empty column:
=QUERY(sort(A2:A,1,0),"limit 10")
This formula will give you the list of top 10 values from column A. Change A to your column.

Resources