Get the last non-blank value in a row in Google Sheets - google-sheets

I need to find the last value in row 20 of my Google Sheet spreadsheet. More data is added from time to time.
The formula I use for this is getting too long and I would need an easier way to get the same result without having to edit the formula each time a new column is inserted:
=If(Q20 ="",
If(P20 ="",
If(O20 ="",
If(N20 ="",
If(M20 ="",
If(L20 ="",
If(K20 ="",
If(J20 ="",
If(I20 ="",
If(H20 ="",
If(G20 ="",
If(F20 ="", "",
F20)
,G20)
,H20)
,I20)
,J20)
,K20)
,L20)
,M20)
,N20)
,O20)
,P20)
,Q20)

There are many ways to get the last non-blank value in a column. Try this:
=+sort( transpose(F20:Q20), transpose( column(F20:Q20) * sign(len(F20:Q20)) ), false )

Related

Google sheets get empty cells next to each other

Link: https://docs.google.com/spreadsheets/d/1hCG6jjGSGWMn5kBkDfcoTJkZ2eCC5SRvHWMFTBp_6OE/edit?usp=sharing
I want to return "Yes" or "No" results in column N when there are empty cells next to each other in the range B:M
And column O counts the number of times there are empty cells next to each other.
Thank you.
use in O2:
=INDEX(LEN(SUBSTITUTE(FLATTEN(QUERY(TRANSPOSE(IF(REGEXMATCH(TRIM(
SPLIT(FLATTEN(QUERY(TRANSPOSE(IF(B2:M5="", "×", "¤")),,9^9)),
"¤")), "× ×"), 1, )),,9^9)), " ", )))
use in N2:
=INDEX(IF(O2:O=0, "No", "Yes"))

How to use dynamic array formula to filter and format data

I am facing a problem related to the dynamic array.
I have data in the below format.
And I want to convert to this format.
Here is the sheet link.
I am using this formula to filter Fruits category.
={FILTER(A5:D11,B5:B11="Fruits");SUM( FILTER(D5:D11,B5:B11="Fruits"))}
But it gives this error
In ARRAY_LITERAL, an Array Literal was missing values for one or more rows
NOTE: Data should be pulled dynamically from the formula, as the data may change.
To build the result table without hard coding category names in the formula, use the recently introduced lambda functions, like this:
={
lambda(
data, categories, headers, totalsHeader, blankRow, selectPrice,
reduce(
headers, query(unique(categories), "where Col1 is not null", 0),
lambda(
resultTable, filterKey,
{
resultTable;
lambda(
filterData,
{
filterData;
{ totalsHeader, query(filterData, selectPrice, 0) };
blankRow
}
)(filter(data, categories = filterKey))
}
)
)
)(
B5:D,
B5:B,
B4:D4,
{ "", "Total:" },
{ "", "", "" },
"select sum(Col3) label sum(Col3) '' "
);
{ "", "Grand Total:", sum(D5:D) }
}
See { array expressions }, filter(), query(), reduce() and lambda().
The formula will repeat each category name on several rows. If they get in the way, you can hide them from view by using a conditional formatting custom formula rule.
I did some tests to add all the information in just one formula. It will change the format you want, but it will still divide all the information.
Here is the formula:
={"Fruits:","";QUERY(B5:D,"select C, D where B ='Fruits'");
{"Total:",SUMIF(B5:D,"Fruits",D5:D)};"","";
"Vegetables:","";QUERY(B5:D,"select C, D where B ='Vegetables'");
{"Total:",SUMIF(B5:D,"Vegetables",D5:D);"","";
"condiments:","";QUERY(B5:D,"select C, D where B ='condiments'");
{"Total:",SUMIF(B5:D,"condiments",D5:D)};"","";
"Grand Total:",SUM(D5:D)}}
Note:
I added : and the end of each category in the formula so they will look like Fruits: and the table will look like this:
The formula opens with { to open an array in Google Sheets, and you use , to separate columns to write a row of data, and ; to separate the rows to help you write a column of data. After that, you use } to close the array. For example:
{"1","2";"3","4"}
It will print:
So basically, I organize the data with arrays of the same amounts of columns. The first one with part
= { => To open the array.
"Fruits:",""; => This create a cell with "Fruits:" + an empty cell.
QUERY(B5:D,"select C, D where B ='Fruits'"); => which is
already on an array of 2 columns.
{"Total:",SUMIF(B5:D,"Fruits",D5:D)}; => Creates the "Total" cell + the sum
of values that has Fruits in column B.
"",""; => Which will create an empty row to separate the information
for the next set of arrays.
You do the same pattern for the other categories.
} => to end the initial array.
You can add a "Conditional formatting" that will change the text with : to bold automatically.
Reference:
QUERY function
SUMIF
ARRAYFORMULA
I suggest you read on: https://stackoverflow.com/a/58042211/5632629
the first part of your formula outputs a grid of 4×3 cells
the second part of your formula outputs a single cell
if you want to combine it properly use:
={FILTER(A5:D11, B5:B11="Fruits");
{"","","Totals",SUM(FILTER(D5:D11, B5:B11="Fruits"))}}
or:
={FILTER(B5:D11, B5:B11="Fruits");
{"","Totals",SUM(FILTER(D5:D11, B5:B11="Fruits"))}}

Add a column with a specific value when making google sheet "=QUERY"

I am sorting an array made up of 2 queries.
I want to add an additional row per query with a prefilled value.
Full query:
=SORT({
QUERY(Materials!A4:K,"SELECT A,D,K,J where B='"&Home!C9&"'") ;
QUERY(Labour!A4:K,"SELECT A,D,H,G where B='"&Home!C9&"'")
}, 1, 1)
Example of currently returned columns/values:
QUERY(Materials!A4:K,"SELECT A,D,K,J where B='"&Home!C9&"'")
date1, supplier1, notes1, amount1
date2, supplier1, notes2, amount2
date3, supplier2, notes3, amount4
QUERY(Labour!A4:K,"SELECT A,D,H,G where B='"&Home!C9&"'")
date1, joblocation1, notes1, amount1
date2, joblocation2, notes2, amount2
What I would like the final output to be:
Name1, Materials, supplier1, notes1, amount1
Name2, Materials, supplier1, notes2, amount2
Name3, Materials, supplier2, notes3, amount4
date1, Labour, notes1, amount1
date2, Labour, notes2, amount2
.. thus as per the title, "adding a column with a specific value when making a google sheet query"
To Add an additional untitled column, here is one possible solution:
QUERY(Materials!A4:K,"SELECT A,D,K,J where B='"&Home!C9&"'")
To
QUERY(Materials!A4:K,"SELECT A,'NewColValue',D,K,J where B='"&Home!C9&"' label 'NewColValue' ''")

Easier Way to VLOOKUP to the left w/ IMPORTRANGE?

I'm using a formula in Google Sheets that combines vlookup and importrange to look to the left in a separate spreadsheet. Here's the formula...
={
"Staff Name";
arrayformula(
iferror(
VLOOKUP(
A2:A,
{
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!D2:D"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!C2:C")
},
2,
0
),
""
)
)
}
But, let's say I need to look 6 columns to the left, instead of just 2. Do I need to do an importrange for every single column like the above formula? Or is there an easier way?
In other words, will my formula need to change to look like this below?
={
"Staff Name";
arrayformula(
iferror(
VLOOKUP(
A2:A,
{
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!H2:H"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!G2:G"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!F2:F"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!E2:E"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!D2:D"),
IMPORTRANGE("1Ri5k_WNei8X9XJVWxrblSuEKS3680YcH3LobflvmzuALcsY","Directory!C2:C")
},
6,
0
),
""
)
)
}
Thanks for your help!

How can I append or add rows below the last row. (in worksheet)

It is possible to append a row in a table using the method below:
POST /workbook/worksheets/{id|name}/tables/{id|name}/rows/add
How can I do the same but in a worksheet?
Now it's only possible to Update with a specific range:
PATCH /workbook/worksheets/{id|name}/range(address='<address>')
I can't type in the address like "range(address='A(lastRow):C(lastRow)')"
First, get the last row number using an Excel function(Match) before appending the row.
POST /me/drive/root:/demo.xlsx:/workbook/functions/match
BODY
{
"lookupvalue": "",
"lookuparray": {
"Address": "sheet1!A:A"
},
"matchtype": -1
}
Specify the last row number as the range and update the value.

Resources