Is excel how to get the max number with condition? - excel

In Excel, I use the Col A as the id number for my data, and the ID number begin at the year. Such as 2018xxxxxxx. I want to get the Max id number for a year.
In python, use a simple condition check, then I can get the result. Such as.
col_A_max = max([x for x in range(A) if str(x) == '2016'])
I try to rewrite it in Excel but it did not work.
=max(if(A:A, left(A:A, 4) = "2016", A:A))

Try this array formula:
=max(if(left(A:A, 4) = "2016", A:A))
Use Ctrl, Shift and Enter to confirm. (The first 6 characters will never be "2018".)
This might depend on whether you are using dates.

MAX Array Formula
The following are array formulas. After they are entered (copied) to the formula bar, hold LEFT CONTROL + SHIFT and press ENTER. (Enter them without the curly braces ({})).
{=MAX((LEFT(A:A,4)="2016")*A:A)}
{=MAX((LEFT(A$1:A$20,4)="2016")*A$1:A$20)}
The following image demonstrates how this formula will work if the values in the A column are either numbers or numbers formatted as text, while the other formulas will not. This principle cannot be applied to MIN.

Related

Look up value by the lowest value

I am trying to match specific name (the names may be repeated several times) and to look up the name which has the lowest value in column D
Here's a snapshot to illustrate the issue
I would like for example to lookup the name Name1 and return the one who has the lowest value. In this case I need to return the row number 4 as this one has only 12 in value
I tried such a formula but didn't work for me
=MATCH("Name2",$A$2:$A$9,MATCH(MIN($D$2:$D$9),$D$2:$D$9,0))
You may use:
=MATCH(1,(A1:A9="Name1")*(D1:D9=MIN(IF(A1:A9="Name1",D1:D9))),0)
Note: It's an array entered formula using CtrlShiftEnter.
If you have Office 365, you can use the new XMatch function with an array returned by an IF statement. Like this formula, which assumes that the text "Name1" is in cell F1.
=XMATCH(0,IF(A1:A9=F1,D1:D9,NA()),1,1)
edit after comment: If you can't use Xmatch, then the data needs to be sorted by column D values from large to small. Then you can use Match() with a -1 as the last argument to look up the next value larger than 0. XMatch can do that with unsorted data, but Match needs sorted data for that.
Also, if you don't use Office 365, the formula needs to be confirmed with Ctrl + Shift + Enter, because it is an array formula. If you enter it in Office 365 Excel, that's not needed, but if people edit the workbook with an older version of Excel and edit the formula, they need to use Ctrl + Shift + Enter.

Find Length=2 cell in a given range

I have these two rows with an image path.
In Columns D-H i have only one cell that his length is 2.
I need to find it and do formulas based on it, and I would prefer not writing 5 times "IF", any quick way to find it?
It's unclear what you're asking but it seems like you're just trying to figure out which column has a string with a length of 2 characters.
If this is the case use this formula (assuming your data starts in row 2 of the columns you mentioned):
=match(2,len(D2:H2),0) However, when you write this hit CTRL + SHIFT + ENTER
Which will give you {=match(2,len(D2:H2),0)} this is an array formula you must carry down. This will give you the relative column of the string with 2 as its length. Relative meaning, if the criteria is met in column D, it will return 1 (instead of 4).
If you want the value, just use an Index match like so, using the same CTRL + SHIFT + ENTER I mentioned earlier.
{=index(D2:H2,match(2,len(D2:H2),0))}
Here's a non-vba, non-CTE/Array formula way to do this:
=SUMPRODUCT((LEN(D1:H1)=2)*COLUMN(D1:H1))
Will spit out the number of the column that has a length of 2. If more than one column fulfills this criteria then you will get back garbage. So don't do that.
You can pop that into Index() to get the value that was hit:
=INDEX(A1:H1, 1, SUMPRODUCT((LEN(D1:H1)=2)*COLUMN(D1:H1)))

Excel find lower distinct value in list

I'm trying to find in a list the lowest unique value.
I tried to find out a way on google, but nothing seem to work like I want.
What i have :
John;5
Leon;7
Mark;5
Bob;3
Peter;3
Louis:4
Desired result: "4" because it's the lower unique value.
Suppose I add in the original list:
Alex;4
The new result is about to be "7" because it's the new lowest unique value.
my excel sheet :
Assuming your data is setup so that names are in column A and values are in column B so that it looks like this:
In cell D2 (or wherever you want the result), use this array formula (Note that array formulas must be confirmed with CTRLSHIFTENTER and not just ENTER):
=MIN(IF(COUNTIF(B2:B20,B2:B20)=1,B2:B20))
You'll know you've entered it as an array formula correctly because you'll see it surrounded by curly braces {=formula}in the formula bar. Do NOT add the curly braces manually.
You'll also notice that I have extra rows in there than just the used rows. Normally I'd suggest using a dynamic named range, but this works for now. So when you add the new line of Alex; 4, you get this:
And you can see the formula now has the new correct value of 7.
With data in columns A and B, in C1 enter:
=COUNTIF(B:B,B1)
and copy down. Then in another cell enter the array formula:
=MIN(IF(C:C=1,B:B))
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.
To avoid speed problems, make the limits on the ranges as small as possible:
=MIN(IF(C1:C6=1,B1:B6))

Max IF While Filtering dates?

How can I find the max date that is less than today in a range that is mixed with both ACTUAL and PROJECTED Dates:
as you see here I have a row of dates. Some dates are actual, others are projected. I want to open the spreadsheet and say "What is the latest PROJECTED date that is less than today?"
If you want to do it in a single formula, you will need to use an Array formula. Array formulas calculate something multiple times, once for each cell in a range, and provide you with an array of responses. To solve part 1 of what you're asking, the array formula would look like this (Assuming your columns end at H, and are on row 2 only):
=MAX(if(A1:H1="PROJECTED",A2:H2,""))
When this is typed into the formula bar, confirm with CTRL + SHIFT + ENTER, instead of just ENTER. It will look like this afterwards (do not type the {} yourself):
{=MAX(if(A1:H1="PROJECTED",A2:H2,""))}
This looks at each cell from A1:H1. Where it says "PROJECTED", it then gives the value in A2:H2 for that column [otherwise it gives ""]. To find which date is the highest, we wrap it in the MAX function.
But we're not done, because you have other criteria. Normally you could use the AND function for this, but AND functions take array results and collapse them into a single value. So we need to use the natural TRUE / FALSE function of the IF statement instead, like so:
=MAX(if(A1:H1="PROJECTED",if(A2:H2<today(),A2:H2,""),""))
This checks where in row 1 = "PROJECTED", while at the same time that column in row 2 is less than the value of today's date. It then provides you with that date. It takes the highest date shown. Remember to confirm with CTRL + SHIFT + ENTER, instead of just ENTER.

COUNTIFS formula to count unique records where range contains dates within specified year criteria

I've been Googling for hours trying to create a COUNTIFS formula that will:
count the unique records in a table range
that have dates that fall within 4 years after
a year in a referenced cell
This is what I have so far: =COUNTIFS(dte_degr_conferred2,"<="&YEAR('Student Success and Progress'!$G$1)+4) but it is not correct and I've tried several variations
Please note: dte_degr_conferred2 column contains dates, 'Student Success and Progress'!$G$1 contains a 4-digit year
Many Thanks
Perhaps you mean this array formula**:
=SUM(IF(FREQUENCY(IF(dte_degr_conferred2<>"",IF(YEAR(dte_degr_conferred2)='Student Success and Progress'!G1,dte_degr_conferred2)),dte_degr_conferred2),1))
Regards
**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).
Trying to work out exactly what you need (you might be overcomplicating). Here is an example of using the COUNTIFS formula to compare dates. This formula was entered into cell E6 with the setup as below:
=COUNTIFS($B$2:$B$6,"<="&E3+4,$B$2:$B$6,">="&E3)
I have an even more simplistic interpretation:
=COUNTIF(dte_degr_conferred2,">="&DATE('Student Success and Progress'!$G$1+4,1,1))
The "count unique" requirement makes this an interesting question.
Here's what I've come up with:
=SUM(
IF(
FREQUENCY(
IF((YEAR(ddc)>=ssp!$G$1) + (YEAR(ddc)<ssp!$G$1+4) = 2, ddc, ""),
IF((YEAR(ddc)>=ssp!$G$1) + (YEAR(ddc)<ssp!$G$1+4) = 2, ddc, "")
)>0,
1
)
)
... where ddc is dte_degr_conferred2 and ssp is 'Student Success and Progress'. (When posting to SO, it's helpful to reduce a question to its essentials, because that makes it easier to respond to, as well as making it more universally useful.)
Enter as an array formula: Ctrl + Shift + Enter
Example
In this example, there are three distinct dates that match the criteria: 05/12/2014, 09/12/2014, and 05/26/2012.
How it works:
When creating array formulas, it can be useful to work out the calculations separately before joining them in an array. I've done so here:
Column C is =(YEAR(A1)>=$B$1) + (YEAR(A1)<$B$1+4) (copied down). In Excel, TRUE is 1 and FALSE is 0, so you can add boolean values together. If A1 is between B1 and B1+4, both operands are TRUE, and TRUE + TRUE = 2.
Column D is =IF(C1=2,A1,"") (copied down). This grabs the data that matches the criteria. (I formatted as numbers to highlight the fact that Excel stores dates as numbers.)
Column E is =SUM(IF(FREQUENCY(D1:D10,D1:D10)>0,1)). For an explanation of this method, see http://office.microsoft.com/en-us/excel-help/count-occurrences-of-values-or-unique-values-in-a-data-range-HP003056118.aspx
My solution at top combines all this into a single array formula.

Resources