ARRAYFORMULA with PERCENTRANK - google-sheets

I'm looking to use PERCENTRANK in an ARRAYFORMULA for a column in a spreadsheet. I put the following formula in AB1.
={"Daily Total Ranking";arrayformula(if(isblank(A2:A),,PERCENTRANK($AB$2:$AB, AB2)))}
But the result is the same ranking (the one for cell AB2) down the entire column.

You need to put the range of the whole column from AB2 onwards in the second PercentRank parameter so that the function is evaluated for each value in the range AB2:AB where A2:A is not blank, instead of just for AB2:
={"Daily Total Ranking";arrayformula(if(isblank(A2:A),,PERCENTRANK($AB$2:$AB, $AB$2:$AB)))}

Related

ARRAYFORMULA Auto-Increment

I'm trying to understand this auto-increment formula, that my colleague has written. I understand how arrayformula usually works and also countifs.
Formula from the screen: =ARRAYFORMULA(COUNTIFS(ROW(B2:B), "<="&ROW(B2:B)))
I'm stacked about why ROW(B2:B) (1param in COUNTIFS) as a range works fine. It should be a range, not just a number that ROW function returns.
I have been trying to find an answer, read documentation, but nothing helped.
I think that, for example, for 4th line the formula would look like this (if we seperate from ARRAYFORMULA):
COUNTIFS(ROW(B4:B), "<="&ROW(B4:B)),
COUNTIFS(4, "<=4")
I need to understand this code, not other solutions.
The best way to understand how an ARRAYFORMULA works is to write down the equivalent drag-down formula.
The equivalent drag-down formula for:
=ARRAYFORMULA(COUNTIFS(B2:B, B2:B, ROW(B2:B), "<="&ROW(B2:B)))
is
=ARRAYFORMULA(COUNTIFS($B$2:$B, B2, ROW($B$2:$B), "<="&ROW(B2)))
=ARRAYFORMULA(COUNTIFS($B$2:$B, B3, ROW($B$2:$B), "<="&ROW(B3)))
=ARRAYFORMULA(COUNTIFS($B$2:$B, B4, ROW($B$2:$B), "<="&ROW(B4)))
...
(We only need ARRAYFORMULA because ROW($B$2:$B) is an array formula, which means that if you type it in a cell without wrapping it an an array-enabling function it will only evaluate ROW($B$2))
If we recall the COUNTIFS parameters:
=COUNTIFS(criteria_range1, criterion1, criteria_range2, criterion2)
We can see that in the drag-down formula, every parameter that by default expects a range, becomes an absolute reference and every parameter that by default does not expect a range, is just a single value that increments each row.
This is true for any other function: if a function has a parameter that by default expects a range, when we wrap it in ARRAYFORMULA(), that range stays the same throughout the entire computation, which means that every single value in that range is seen by the array formula at any time. What increments, and therefore is only seen by the array formula on that specific row, are the parameters that do not natively expect a range.
This seems like an obvious observation but I'm sure it's the reason many people are confused about how that formula works.
If you understand this concept, then you can also understand how the other variants of the formula work:
=ARRAYFORMULA(COUNTIFS(B2:B, B2:B, ROW(B2:B), "<"&ROW(B2:B)))
=ARRAYFORMULA(COUNTIFS(B2:B, B2:B, ROW(B2:B), ">="&ROW(B2:B)))
=ARRAYFORMULA(COUNTIFS(B2:B, B2:B, ROW(B2:B), ">"&ROW(B2:B)))
every next row is by logic an incrementation of the previous row +1. what this formula does it checks the given row number against row number. for example, for row 4, COUNTIFS checks if ROW(A4) is smaller or equal to ROW(A4). then the evaluation is "yes, row 4 is equal to row 4" and the output is TRUE. what COUNTIF actually does is counting these TRUE outputs up to every row summing all the previous rows. something like:
rows
COUNTIFS processing
output
counting TRUEs
final output
row 1
row 1 equal to row 1?
TRUE
1st TRUE
1
row 2
row 2 equal to row 2?
TRUE
2nd TRUE
2
row 3
row 3 equal to row 3?
TRUE
3rd TRUE
3
row 4
row 4 equal to row 4?
TRUE
4th TRUE
4
row 5
etc
etc
etc
etc

Sum every n columns starting at the 1st column in the range

In Excel/Google Sheets I have found how to sum every N columns on websites such as https://exceljet.net/formula/fixed-value-every-n-columns, but the problem is, from what I can see is that it starts at N column each time. I need something that starts from column 1 and then counts every N columns. like the following:
I need to do this with a formula and not a script.
With Google-Sheets, try:
Formula in M2:
=SUM(QUERY(TRANSPOSE(A2:J2),"Skipping "&L2))
Or, a single dynamic array formula (without dragging):
=INDEX(MMULT(A2:J4*(MOD(COLUMN(A2:J4),L2:L4)=1),SEQUENCE(10,1,1,0)))
Or, more dynamic:
=INDEX(MMULT(A2:J4*(MOD(COLUMN(A2:J4),L2:L4)=1),SEQUENCE(COLUMNS(A2:J4),1,1,0)))
Note: The latter would also work in Excel with slight modifications.
Google sheets formula:
=SUM(FILTER(A2:J2, MOD(A2:J2, L2)=1))
then drag to other cells
or use this array version:
Array version:
=INDEX(TRANSPOSE(MMULT(A2:J4,TRANSPOSE(COLUMN(A2:J4)^0 *
N(MOD(COLUMN(A2:J4), L2:L4)=1)))), ,1)
If you want the cells that were added to be automatically highlighted.
Conditional formatting used on A2:J:
=MOD(COLUMN(), $L2)=1
In M2:
=SUMPRODUCT(A2:J2,N(MOD(SEQUENCE(,COLUMNS(A2:J2),0),L2)=0))
and copied down.
Try this formula on column M:
=SUM((sumif(ArrayFormula(mod((COLUMN(B2:J2)-COLUMN(B2)+1),L2)),0,B2:J2))+A2)
Here's the result on Column M.
Just to break down the code sumif(ArrayFormula(mod((COLUMN(B2:J2)-COLUMN(B2)+1),L2)),0,B2:J2) does the actual calculation with the number of intervals set on Column L but take note that I started at the 2nd column so the range here does not include the first column. The result from this is at the Column O highlighted red as you can see in the screenshot.
At the Column M is the actual solution where I only added the first column by using SUM on top of the previous formula.
I hope my explanation is clear.
Just copy/drag the formula down to each row and it should work.
Reference: How to Sum Every Nth Row or Column in Google Sheets Using SUMIF

Array formula with filter and index values depending on countif

I need an array formula only in column Date_2 with results like on screenshot and that will
insert last day of month depending on Date_0 (if bunch of Color&Fruit&Meal doesn't repeat in table)
insert first minimum date of column Date_1 (if bunch of Color&Fruit&Meal repeats first time) - 1
insert second minimum date of column Date_1 (if bunch of Color&Fruit&Meal repeats second time) - 1
and so on...
Is is possible to solve it with array formula?
I've tried but I can't..
=ArrayFormula(IF(A2:A="","",IF(COUNTIF(B2:B&C2:C&D2:D,B2:B&C2:C&D2:D)>1,INDEX(FILTER(B2:E,E2:E<>""),1,4),EOMONTH(A2:A,0))))
Google Sheets
I'm not quite sure what you need for Date_1 but try this arrayformula in cell F2 for Date_2:
=ARRAYFORMULA({"Date_2";if(IF(B2:B&""&C2:C&""&D2:D<>"",if(A2:A<>"",COUNTIFS(B2:B&"|"&C2:C&"|"&D2:D,B2:B&"|"&C2:C&"|"&D2:D,ROW(A2:A),"<="&ROW(A2:A)),),)=1,eomonth(A2:A,0),)})
I've added a duplicate sheet ("Erik Help") with the following formula in F1:
=ArrayFormula({"Date_2";IF(A2:A="",,IFERROR(VLOOKUP(B2:B&C2:C&D2:D&TEXT(COUNTIFS(B2:B&C2:C&D2:D,B2:B&C2:C&D2:D,ROW(A2:A),"<="&ROW(A2:A))+1,"000"),{B2:B&C2:C&D2:D&TEXT(COUNTIFS(B2:B&C2:C&D2:D,B2:B&C2:C&D2:D,ROW(A2:A),"<="&ROW(A2:A)),"000"),E2:E},2,FALSE)-1,EOMONTH(A2:A,0)))})
This formula creates the header (which you can change within the formula) and all results for Column F.
To lookup the "next instance of the group if there is one," I just wrote the formula to VLOOKUP that grouping plus a text rendering of the COUNTIFS-as-of-that-row-plus-1 for that grouping within a virtual array of each-grouping-plus-unique-count-thus-far in one column and the E2:E data in the next column. For instance, for Row 2, the formula VLOOKUPs redapplepie002
(002 being the text rendition of 001, which is the count of redapplepie as of row 2).

Calculate Total row Averages correctly if table columns contain empty cells

I am using the following formula to calculate a Total row that calculates averages for percentages in the columns. However, since the data contains some empty values, the averages calculated is not correct.
={"TOTAL",arrayformula(transpose(query(transpose(query({(B16:M19)},"Select " & TEXTJOIN(",",true,arrayformula(if(filter($B$15:15,$B$15:15<>false),"Sum(Col" & Column($B$15:$M$15) - Column(B$15) + 1 & ")/Count(Col" & Column($B$15:$M$15) - Column(B$15) + 1 & ")"))))),"select Col2 format Col2 '0.00%'",)*100%))}
Ocassionally, I also get errors in my production spreadsheet viz.,
Unable to parse query string for Function QUERY parameter 2: AVG_SUM_ONLY_NUMERIC
or
Unable to parse query string for Function QUERY parameter 2: TypeError: Cannot read property 'get' of undefined
These go away if i coerce the range like this:
(B16:M19)+0 .... or.... N(B16:M19)
But this results in the average calculated incorrectly as the average considers the 0's that get replaced or the empty cells now. How can this be avoided and the values calculated correctly?
P.S: i had to hardcode the range as i don't know how to make this formula more dynamic. If anyone has an idea, please do share with me.
Here is the sample sheet. FTR Table
I came up with this formula, for your total values. See B23 in your Tables sheet:
=IFERROR(SUM(B16:B19)/COUNT(FILTER(B16:B19,B16:B19>0)),0)
If this tests out okay for you, I can try to see if it can be developed as an arrayformula.
It just sums each column in the table, and divides that by the count of values in the table that are greater than zero, so it excludes both blanks and zeroes in the table.

How to find a value from a range of cells and place at a specific in google sheet

Suppose I have only one value anywhere at the cell range C2:Z2, I want that value at B2. What can I do?
I need this solution for all the rows bellow this also. The value might at C:C column at one row, at H:H column at another, that means it is dispersed at the range but there will be only one value at the range in a row.
Place this formula in B2:
=ARRAYFORMULA(TRIM(TRANSPOSE(QUERY(TRANSPOSE(C2:Z), , COLUMNS(C2:Z)))))
The formula above works for any type of values.
If your values are numbers then a simpler formula could be used (MMULT does row wise sum here):
=MMULT(
ARRAYFORMULA(--(C2:Z)),
SEQUENCE(COLUMNS(C2:Z), 1, 1, 0)
)
You can use FILTER()
=FILTER(C2:Z2, NOT(ISBLANK(C2:Z2)))
Reference:
FILTER()
if there will be only one value in the row then a simple sum function will do the job for you. put this in B2 ..
=sum(C2:Z2)

Resources