Home > Enterprise >  How to understand this array formula
How to understand this array formula

Time:08-24

enter image description here

I tried to understand by breaking up the formula bit by bit in non array version, by this I understood:

​=ARRAYFORMULA(COUNTIFS(ROW(A2:A), "<="&ROW(A2:A)))

(Which gives serial numbers)

I really don't understand anything else, for example in column E:

=ARRAYFORMULA(COUNTIFS(A2:A, A2:A))

Here I don't understand why the blank cells have count 0, does that mean COUNTIFS doesn't count blanks? If yes then when I type ="" at A6 only why value of D6 only turns 950? Why not of other rows which are blank?

enter image description here

And then when the COUNTIFS is branched inside another COUNTIFS I completely failed to understand anything further.

How can I understand this in detail?

CodePudding user response:

let's start with inner COUNTIFS and do this in terms of arrays. if you run:

=ARRAYFORMULA(COUNTIFS(A2:A, A2:A))

it means that you are "counting unique values in A column, only if there is a value in A column". so no value in A column returns 0 and the rest is counted. the first range A2:A stands for a range we search on and the 2nd A2:A range is a criterion - if the cell in A column is empty it returns FALSE eg. 0 and if it's not empty it returns TRUE in form of a count of unique item. see:

enter image description here

and:

enter image description here

now let's take a look at the outer COUNTIFS. the 4th argument of outer COUNTIFS is an array so it overrides the 2nd argument of outer COUNTIFS which is just a single value. so if we run it as is the result is a sum of all ones in K column:

=ARRAYFORMULA(COUNTIFS(COUNTIFS(A2:A, A2:A), 1))

enter image description here

therefore to understand it in its array form we need to alter it a bit by adding the 3rd and 4th arguments to the outer COUNTIFS:

=ARRAYFORMULA(COUNTIFS(COUNTIFS(A2:A, A2:A), 1, 
 ROW(A2:A), "<="&ROW(A2:A))) 

enter image description here

which is essentially the:

=ARRAYFORMULA(COUNTIFS(K2:K, 1, 
 ROW(A2:A), "<="&ROW(A2:A)))

enter image description here

and that translates as "count how many 1's are in K2:K range on every row ROW(A2:A) up to the current row of every row "<="&ROW(A2:A) and loop this row-by-row". in other words, it is the same as you would drag =SUM(K$2:K2) down the column

enter image description here


important note

this formula works only if all values in A column are unique. if they aren't then the formula will brake:

enter image description here

the correct formula for this task would be:

=ARRAYFORMULA(VLOOKUP(VLOOKUP(ROW(A2:A), IF(A2:A<>"", {ROW(A2:A), A2:A}), 2, 1), 
 {UNIQUE(FILTER(A2:A, A2:A<>"")), SEQUENCE(COUNTUNIQUE(A2:A))}, 2, 0))

enter image description here

or if the set is not sorted:

=ARRAYFORMULA(VLOOKUP(VLOOKUP(ROW(A2:A), IF(A2:A<>"", 
 {ROW(A2:A), A2:A&COUNTIFS(A2:A, A2:A, ROW(A2:A), "<="&ROW(A2:A))}), 2, 1), 
 {UNIQUE(FILTER(A2:A&COUNTIFS(A2:A, A2:A, ROW(A2:A), "<="&ROW(A2:A)), 
 A2:A<>"")), SEQUENCE(COUNTA(A2:A))}, 2, 0))

enter image description here


also, keep in mind that a truly empty cell is not equal to a cell with the formula that outputs an empty string =""

enter image description here

  • Related