I have a column of data and a target value and am wanting to apply a rule that will highlight the closest value to that target value from the column of data possible. I have tried a few different forumla and nothing has worked so far. This is what I am currently working with:
Target Number is in I3
Data is in I4:I24
=ABS($I$3-I4)=MIN(ABS($I$3-$I4:$I24))
This is all being done through Google Sheets (not sure if this is relivant but thought it couldn't hurt being included)
First of all, let's change $I4:$I24 to $I$4:$I$24, so that this array is the same for all cells that the formula is applied to.
The - operator only works for numbers, not arrays. So when you subtract an array from a number, it returns just the value minus the first number in the array. The same goes for the ABS function - it can't take an array.
To instead have the - operator and ABS function apply to each element of the array individually, you need to use the ARRAYFORMULA function. To do this, wrap the ABS($I$3-$I$4:$I$24) in ARRAYFORMULA - that is, ARRAYFORMULA(ABS($I$3-$I$4:$I$24)). Now, ABS and - apply to each element of the array individually, and return an array of all the results. This can then be passed into MIN.
Now we get
=ABS($I$3-I4)=MIN(ARRAYFORMULA(ABS($I$3-$I$4:$I$24)))
Try
=ArrayFormula(ABS($I$3-I4)=MIN(ABS($I$3-$I$4:$I$24)))
Related
What I'm trying to do is find the name of the person who is ranked number 1 in the table shown below. I have tried =LOOKUP and =VLOOKUP but I get an error saying that a result can't be found, even though it's obviously there. I assume that I'm either using the wrong function or just not using it right.
I tried =VLOOKUP(1;D2:H19;1) and =LOOKUP(1;D2:H19;1) but neither seems to work.
Answer
The following formula should produce the behaviour you desire:
=INDEX(D2:D,MATCH(1,H2:H,0))
Explanation
=VLOOKUP can only be used to find values to the right of a search key. To find values to the left of a search key, use a combination of =INDEX and =MATCH.
The =MATCH function searches a specified range for a specified value and returns the relative position of the value in that range. In this case, the value to search for is 1, the range to search through is H2:H, and the 0 at the end specifies that the range is not sorted in any way.
The =INDEX function returns the contents of a cell within a range having a specified row and column offset. In this case, the range is D2:D, and the row is whichever row is returned by =MATCH. =INDEX could take a third argument if it was desired to specify a row offset as well, but that is not necessary here.
Functions used:
=INDEX
=MATCH
You sort your ascending order based on rank then return your desired data using INDEX() function. Try-
=INDEX(SORT(D2:H500,5,1),1,1)
=vlookup(1,{H2:H19, D2:D19},2)
Since vlookup only searches the 1st column of the input range, to use it, you need to flip the columns by composing a local array: {H2:H19, D2:D19}.
{} means concatenation. , in it means horizontal concatenation. With that, the rank column is now the 1st column in the input of vlookup and now vlookup works.
With our local array, the 2nd column are the names and therefore index should be 2.
Also note the use of comma to separate function inputs.
your VLOOKUP formula should look like:
=VLOOKUP(1, {H2:H19, D2:D19}, 2, 0)
also try just:
=FILTER(D:D; H:H=1)
or:
=SORTN(D:D; 1; 1; H:H; 1)
You can use query (usefull in case of ex aequo)
=query(D2:H,"select D where H=1",0)
Screenshot with example
I want to define the sum_range in an sumif-formula by using a range returned by an arrayformula but it gives me an error, not acknowledging it as a range. Using an arrayformula for the conditional range works however.
This works:
=sumif(
ARRAYFORMULA({B1;C3;D5}),TRUE,
A7:A9
)
This doesn't:
=sumif(
ARRAYFORMULA({B1;C3;D5}),TRUE,
ARRAYFORMULA({A1;B3;C5})
)
Am I doing something wrong here that I can't see or is it maybe just not possible? Maybe there's another smart way to do it..?
Why not use
=sumif(B1:B3, "TRUE", A1:A3)
or even
=sumproduct(B1:B3, A1:A3)
instead ?
If you really need to use inline arrays this should work
=sumproduct({B1, B2, B3}, {A1, A2, A3})
Bool to Number Casting
SUMPRODUCT will automatically cast Booleans to Numbers, so TRUE=1 and FALSE=0. If it doesn't, then the formula requires this to be a manual operation. You can leverage the N() formula to cast Booleans to Numbers manually.
Reference
N()
SUMPRODUCT()
you don't need any arrayformula to use the sumif as the sumif accepts array ranges:
=sumif(B:B; true; A:A)
ranges:*** ***
the thing is that you may not have a multidimensional array in your sum range column, only a single array, however, in your initial range you can have any multidimensional array:
=sumif(B1:B3;true;{A1:A3}) - error
=sumif({B1:B3};true;A1:A3) - works well
=sumif({B1:B3; B1:B3}; true; A1:A3) - works well as well
I'm learning basic =ARRAYFORMULA usage for a finance spreadsheet:
https://docs.google.com/spreadsheets/d/12cAGuUBzIo0LPbmtqWJZNFgjt94f1ybGoj6x2g0c2Y0/edit?usp=sharing
First, I used =GOOGLEFINANCE at B1 to pull up stock prices for a given date range in Column C
=GOOGLEFINANCE(A1,"price",DATE(2020,1,1),DATE(2020,5,30),"DAILY")
Then, I used simple arithmetic to multiply by number of shares at D2 and dragged the formulas down to get a nice column of values
=C2*20
Then, I used INDEX and COUNTA to pull out the last value of Column D at F3 === Great!
=INDEX(AAPL!D2:D,COUNTA(AAPL!D2:D),1)
Next, I turned my arithmetic formula into an ARRAYFORMULA at G2 === Cool!
=ARRAYFORMULA(C2:C103*20)
Of course, the problem with that ARRAYFORMULA is that I would have to manually change the array name in G2 every time the date range updated.
That is, instead of C2:C103, I would need to change the reference to C2:104 to get the columns to match === Rookie mistake!!!!!
So, I got smart with an ARRAYFORMULA containing a IF(ISBLANK(),...,...) at J2
=ARRAYFORMULA(IF(ISBLANK(B2:B),"",C2:C*20))
Column J stays fully populated with the correct values for any date range === !!!!!!!
But now the =INDEX(AAPL!J2:J,COUNTA(AAPL!J2:J),1) at L3 can't find the last value in Column J
Whaaat???
I've tried everything I can think of
It works if I use =INDEX(AAPL!J2:J104,COUNTA(AAPL!J2:J104),1) but that would defeat the purpose, since the reference J:J104 is going to change as the dates change
WHY???
I get the same results in both cells "L3" and "L5" when using:
=ARRAYFORMULA(IF(ISBLANK(B2:B),,C2:C*20))
Check that you are not returning a blank string ("") in your IF like: "=ARRAYFORMULA(IF(ISBLANK(B2:B),"",C2:C*20))" doing so will fill the cells up to the last row in the sheet with empty strings, thus when you use COUNTA(AAPL!J2:J),1) you get a lot more cells than you would expect, these extra cells are the ones you filled with blank strings in the array formula.
On the contrary when you limit the =INDEX(AAPL!J2:J104,COUNTA(AAPL!J2:J104),1) to cells with numbers only it doesn't mix strings and numbers in the calculation and you are naturally get the expected results.
I have a google sheet which looks like this :
The formula for cell M3 is =COUNTA(UNIQUE(B3:L3)) which outputs the 01/10/00. However the cell B18 is =COUNTA(UNIQUE(B3:B17)) and its output is 15
I wanted to get this count of unique values in the range using the formula but can't figure out the cause of difference of the outputs. Also, the count of unique values in a row should be 11 which is not really reflected in M3 and any changes are not changing the value of the output either.
there is a combo formula for that called COUNTUNIQUE
=COUNTUNIQUE(B3:L3)
The formatting of M3 is probably set to Date and that's converting your number into that date view. Additionally, if you're getting a higher number than you expect in general, double check for trailing spaces that can trick the unique() function.
I watched a tutorial where the author uses an IF statement along with the ARRAYFORMULA function to add a title row to a column of data. Links are given to the docs; however, for an example of how to use ARRAYFORMULA see this answer.
An example can be seen below:
I was able to populate the C column by placing the following formula in C1:
=ARRAYFORMULA(if(row(A:A) = 1, "spent", B:B - A:A))
I'm confused about the syntax. I understand that X:X references the entire X column but I don't understand how it's being used to check if we're at cell A1 in one context and then being used to apply mass formulas in another context.
How does the above line work?
Can you illustrate with some examples?
It sounds to me that the information you learned led you to expect that row(A:A)=1 translates to row A1?
It works a little different than that, the syntax as your using it now, is basically saying if any row in A:A has a value of 1, then write "spent" else subtract B-A
My suggestion:
use a literal array to make your header, then use the if(arrayformula) to only populate rows with values, for aesthetics:
Example:
={"Spent";arrayformula(if(isnumber(A2:A),B2:B-A2:A,))}
Explanation:
The {} allow you to build a literal array, and using a semicolon instead of a comma allows you to stack your cells vertically, following that we check if there is a value in column A, if so, subtract A from B, else leave it blank.
why not just put the column title directly on the first row cell, and start the array formula from the 2nd row, using the A2:A, B2:B syntax?
If something does not have to be in a formula, better put it directly on the cell - simpler for others to understand what's going on, and the formula will be simpler.
If you put the array formula in line 2, and someone sorts the data, then the arrayformula will move. If it is in the header line, this is less likely to happen.
You can also use the IFS function to achieve a similar effect to the array,
=arrayformula(ifs(row(A1:A)=1,"Spent",A1:A="",,True,B1:B-A1:A)
Here the first condition checks the row number, and if it is row ONE, then inserts a Column Header.
The Second condition - A1:A="",, - ensures that blank lines are ignored.
The Third condition True (ELSE) performs the calculation.
This method also allows for different calculations to performed on different rows depending on requirements.