If function and Xlookup - excel

I am currently trying to run an excel function that runs the logic that if the cell that has date X is greater than date 2023-01-01 then run an XLookup function. If it is not then do not run it.
Here's what I tried below:
=IF(table2!N5>DATE(2023,1,1),XLOOKUP(A2,'Table2'!A:A,'Table2'!B:B),"FALSE")
N5 has the date 2022-12-01. The rest is just a standard xlookup function that would run if cell N5 is greater than the date 2023-01-01.
Data for the Xlookup below:
Table 1 : This table provides the lookup value A1.
A B C
1 Position Classification Full Name
2 111111 IT 04 John Smith
Table 2 : This table provides the lookup array being A:A and the return array being B:B
A B C D
1 Employment Position CC Valid From Valid To
2 111111 8888888 2022-10-01 2023-03-31

As posted in comments, I will suggest you use a cell reference, so it becomes more dynamic to use, instead of hard coding within a formula,
• instead of this
=IF(N5>0+"2023-01-01",XLOOKUP(E3,H:H,J:J),"FALSE"))
• Use a cell reference, let's say cell A1 = 2023-01-01
=IF(N5>A1,XLOOKUP(E3,H:H,J:J),"FALSE"))
So the above helps you in changing the cell A1 values whenever you need instead of bothering the formula each time for a change.

I would advise you to use the DATE() function, like this:
= IF(A1>DATE(2023,1,1), ...)

Related

Getting the next lower value with VLOOKUP

I have the following Excel spreadsheet:
A B C D
1 15.000 Product 1 7.500 Product 2
2 5.000 Product 2
3 1.000 Product 3
4 0 Product 4
5
In Cell C1 I type a random number (in this case 7.500). Now I want that in Cell D1 the corresponding Product is shown to the value in Cell C1.
Since 7.500 does not exist in Column A the next lower value should be used.
In this case 5.000 which belongs to Product 2.
I tried to go with the following formulas in Cell D2 but instead of getting Product 2 I always get Product 4 as a result.
=VLOOKUP(F16,$A$1:$B$4,2,TRUE)
=VLOOKUP(F16,$A$1:$B$4,2)
Do you have any idea how to solve this issue?
For unsorted data you can use below formula:
=INDEX(B1:B4,MATCH(LARGE($A$1:$A$4,COUNTIF($A$1:$A$4,">"&C1)+1),A1:A4,0))
See image for reference
i will try it with addition columns, which indicates the difference between given 7.5 and col A like this: =IF($C$1-A1<0,999999,$C$1-A1) and then get the mininum value like =MIN(E1:E4). then get the according value in column B.
If the order is not guaranteed to be ascending then you can use an array formula (use ctrl+shift+enter instead of just enter to create an array formula)
=INDEX(B2:B5,MATCH((C2-MIN(IF((C2-A2:A5)<0,MAX(A2:A5),C2-A2:A5))),A2:A5,0))

Formula returning Column A value for row containing MAX value of a range

Assume I have the following table:
A B C
1 Week 1 Week 2
2 Melissa 114.7 82.8
3 Mike 105.5 122.5
4 Andrew 102.3 87.5
5 Rich 105.3 65.2
The names are in column A, the Week values are in Row 1. (So A1 is blank, B1 = Week 1, and A2 = Melissa.)
I'm trying to build a formula that looks at all the values in a known range (in this example, B2:C5), chooses the highest value of the bunch (here, 122.5) and returns the name of the person from Column A that got that value. If I use this formula, it works for the values in range B2:B5:
=INDEX(A2:A5,MATCH(MAX(B2:B5),B2:B5,0))
That returns Melissa but if I expand the range to include more than just column B's values, I get an #N/A returned:
=INDEX(A2:A5,MATCH(MAX(B2:C5),B2:C5,0))
The weird part (to my simple brain) is that the MATCH portion of the formula works fine, if I just put in this formula, it returns the highest value of 122.5 from C3:
=MAX(B2:C5,B2:C5,0)
So clearly something it going wrong when I'm using either the MATCH or INDEX commands.
Hopefully this makes sense and someone can point out my error?
Try this:
=INDEX(A:A,MAX((B2:C5=MAX(B2:C5))*ROW(B2:C5)))
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
Note: Match can only search one vector at a time. It can be one row or one column or one array. It cannot be two or more rows or columns or a 2D array.
Do it "twice"? Please try:
=INDEX(A2:A5,IFERROR(MATCH(MAX(B2:C5),B2:B5,0),MATCH(MAX(B2:C5),C2:C5,0)))
If you are going to have up to 52/53 weeks to cope with I'd suggest instead inserting a helper column with the MAX for each row. Make that an new (inserted) ColumnA (say =MAX(C2:BC2) etc.) and a simple VLOOKUP should serve, say:
=VLOOKUP(MAX(A:A),A:B,2,0)

Excel: Matching the closest date from range of dates

I'm trying to display the header of the column that its cell that contains a date, is the closest (out of 3 other cells with dates, B2:D2) to the a specific cell (A2)
So here's my sheet:
A B C D
1 | T1 | T2 | T3 | T4 |
2 | 5/1/2013 | 5/31/2013 | 2/5/2013 | 3/2/2013 |
A2 = a date.
B2:D2 = 3 dates, can also be blank
I've tried using the following array formula that I found online:
=IF(COUNT($B2:$D2)>0,INDEX($B$1:$D$1,MATCH(MIN(ABS($B2:$D2-$A2)),ABS($B2:$D2-$A2),0)),"")
But it sometimes when there is only one or two dates, it gives me the wrong header. I would also like to avoid using array formulas, but couldn't convert it to a regular one.
Would highly appreciate assistance on this matter
Can you give an instance were the array formula you are using isn't working ? It worked for the cases I tried.
You can avoid using the array formula if you are prepared to have another row with the difference in the dates and your date cell A2.
Push your rows down one row, so the headings are on row 2 and the dates on row 3 and enter the following formula across row 1, starting at cell B1
=ABS(B3-$A$3)
Drag this across and enter the following HLOOKUP to get your header row
=HLOOKUP(MIN(B1:D1),B1:D3,2,FALSE)
if you can't move your rows down for some reason you can use a combination of MATCH and INDEX (which is slower) . If you were to put the difference function =ABS(B2-$A$2) across row 7 the formula would be
=INDEX(B1:D1,1,MATCH(MIN(B7:D7),B7:D7,0))
Okay so the answer was fairly simple.
I went back to the sheet to investigate the scenarios in which this happens.
It seems that when A2 is blank (= no date to make the comparison) it just gives out the only date available from B2:D2.
By simply adding another condition to the IF statemented that would work only if A2 has a value resolved the issue. So we're checking if both A2 and B2:D2 has values in them (with B2:D2 needs to be at least one:
=IF(AND(COUNT($B2:$D2)>0,$A2>0),......)
and here is the full formula
=IF(AND(COUNT($B2:$D2)>0,$A2>0),INDEX($B$1:$D$1,MATCH(MIN(ABS($B2:$D2-$A2)),ABS($B2:$D2-$A2),0)),"")
Thanks for making me go back to the investigate! :D
For a non-array alternative, you could try:
=LOOKUP(2,1/FREQUENCY(0,ABS(B2:D2-A2)),B$1:D$1)
in place of the INDEX(...) part of the formula.

defining variables in excel formulas?

i have a simple table data like:
date | Jenna | Tom | Robin
01/01/12 2 5 8
02/01/12 3 4 7
(date columns starts from the first day of the year and goes all the way down to he next year.)
I have a formula getting the min. and max. values (for example for Jenna) for the month January.
I have to change the row numbers every month to get the right result.
Is it possible for me to define variables in two cells and use them in that formula without using VBA.
For example;
I will put two values in H1 and H2 cells, which are 28 and 58 respectively. And i will define E1 as start and E2 as end. And write down this formula:
=min(Cstart:Cend)
=max(Cstart:Cend)
=average(Cstart:Cend)
The first formula means to get the min value between the C28 and C58.
That way i will not have to change every formula manually on the page. I will just change the values of 2 cells and that will be enough.
I hope, I have explained.
Sure. You have to use the INDIRECT() function. INDIRECT transforms a string to a range reference.
=MIN(INDIRECT("C" & $H$1 &":C" & $H$2))

Return value of last match

I need a formula to return the value of Data for the last match of "Text". Row number is also acceptable. Macro is NOT acceptable. Name column is unsorted and cannot be sorted!
Only column "Name" is used as lookup value. I would rather use a/multiple helper column(s) instead of an array formula.
Row Name Data
1 Joe 10
2 Tom 20
3 Eva 30
4 Adam 40
5 Tom 21
LARGE only works with numbers, and VLOOKUP only returns the first match. LOOKUP only works sometimes, so its out too.
So if I wanted the last match for "Tom" then it should return "21".
Array formulas could be avoided with a helper column.
Suppose to have in F1 the name to match (i.e. Tom)
In the helper column row C2 enter
=IF(A2<>$F$1,0,row())
Then copy the formulas along your data.
Now the column C contains 0 for the unmatched names and the row number for the matched ones. Maxing the column yield the row of the solution.
Now the result is simple a matter of using the correct offset with the function offset:
=OFFSET(B1,max(C:C)-1,0)
PS: my copy of excel is in italian, so I can't test this english translaction of the formulas.
I think it's the easiest way to make it.
=LOOKUP("Tom";A2:B7)
Create a column with an array formula (enter it with Ctrl+Shift+Enter):
=VLOOKUP(MAX(IF($B$2:$B$6=B2, $A$2:A$6, 0)), $A$2:$C$6, 3, FALSE)
To make sure you did it right, click on the cell, and the formula should be shown encased in curly brackets ({}).
Note: This assumes that "Row" is in A1.
I have come up with a solution, but it requires that numbers in Data are concurrent, like so
Name Data
Joe 1
Tom 1
Eva 1
Adam 1
Tom 2
Tom 3
Eva 2
But thats okay, since that my data looks like that anyway. So if Name is used before then it must be the old highest +1 aka concurrent.
Name is A1 and Data is B1, and this formula goes into C2:
FLOOR(SQRT(2*SUMIF(A2:A7,A2,B2:B7)),1)

Resources