Excel: Extracting numbers from a string with variable length - excel

I have a column with data that looks like this:
Day
D003
D004
D008
D010
D012
D028
And in the next column, I need to extract just the 3, 4, 10, 12, etc. with NO leading 0's. I tried the following:
=IF(D8="D003 Predose","30",IF(D8="D003 End","31",IF(D8="D003 4hours","34",RIGHT(D8,LEN(D8)-FIND(RIGHT(SUBSTITUTE(D8,"0",""),1),D8)+1))))
Which works for the 4, 5, 8. But for the 12, 17, 21, etc it only gives me the last number. How do I account for the variable length of those strings? I'm really new at Excel and creating formulas "from scratch" so I apologize if the answer is right in front of my nose. If there's a cleaner way to do this, I'm also all ears.

Related

How to get the second largest value in a column

Recently I discovered the LARGE and SMALL worksheet functions, one can use for determining the first, second, third, ... larges of smalles value in an array.
At least, that's what I thought:
When having a look at the array [1, 3, 5, 7, 9] (in one column or row), the LARGE(...;2) gives 7 as expected, but:
When having a look at the array [1, 1, 5, 9, 9], I expect LARGE(...;2) to give 5 but instead I get 9.
Now this makes sense : it seems that the function LARGE(...;2) takes the largest entry in the array (value 9 on the last but one place), deletes this and gives the larges entry of the reduced array (which still contains another 9), but this is not what one might expect intuitively.
In order to get 5 from [1, 1, 5, 9, 9], I would need something like:
=LARGE_OF_UNIQUE_VALUES_OF(...;2))
I didn't find this in LARGE documentation.
Does anybody know an easy way to achieve this?
If you have the new Dynamic Array formulas:
=LARGE(UNIQUE(...),2)
If not use AGGREGATE:
=AGGREGATE(14,7,A1:A5/(MATCH(A1:A5,A1:A5)=ROW(A1:A5)),2)
This is a bit of a hack.
=LARGE(IF(YOUR_DATA=LARGE(YOUR_DATA,1),SMALL(YOUR_DATA,1)-1,YOUR_DATA),1)
The idea is to (a) take any value in your data that is equal to the largest element and set it to less than the smallest element, then (b) find the (new) largest element. It's OK if you want the 2nd largest, but extending to 3rd largest etc. gets progressively uglier.
Hope that helps

Interview question about "largest range" makes no sense

Here's the question. I'm actually dumbfounded. I don't even get the question. What are they on about?
What even is a largest range? What do they mean by largest? What's a range? They say a range is a collection of numbers that come right after each other in the set of real integers. Okay, so 1, 2, 3, 4, stuff like that, right? But then they say the numbers need not be ordered or even adjacent.... but then they're not coming right after each other!! They are contradicting their own previous statement. Now I have no idea what a range is.
Their example doesn't help either. Why is [0, 15, 5, 2, 4, 10, 7] the largest range in that vector?
What is going on?
It's not very clear in the question, but I'm pretty sure the interviewer means a "range" is a set of consecutive numbers (n, n+1).
The range [0,7] is actually [0,1,2,3,4,5,6,7] since all of those appear in the full set.
The actual order doesn't matter.
In the example you were given in the interview, which you list in your question as well, the input array is: [1, 11, 3, 0, 15, 5, 2, 4, 10, 7, 12, 6]. The reason that the "largest range" is identified as [0, 7] is because all the numbers between 0 and 7 are included in that array.
There isn't another range in the input array that has a longer range than 0 to 7. For instance, there is a [10, 12] range in the input array, but that array has a length of 3 that is smaller than the length of [0, 7] range, which is 8.
In this case, the range is understood as a continuous list of integers, the largest range is the list with the most number of integers.
It means
Find the largest continuous range of numbers
For eg. in array [0,1,2,5,6,7,8,9,10]
There are 2 continuous list
[0,1,2] and [5,6,7,8,9,10] but as the larger range is the second one. so the output must be [5,10].
i.e. The largest and smallest of the largest range.

Using result of an Excel array function in a calculation

I am attempting to count instances of a particular value in Excel, from the last instance of a prior value.
Assume a vertical list starting in cell A1: 1, 2, 3, 4, 5, 4, 5, 3, 4, 5, 2, 3, 4, 3, 4, 2, 3, 4, 5
I can use an array function in, say B14 (A14 value: 3), of {=MAX(ROW($1:14)*(A$1:A14=A14-1)) to give me the row number of the last instance of a "2" (row 10).
I can then have, in C15, a function =COUNTIF(OFFSET(A14,0,0,B14-ROW(A14),1):A14,A14), which will count the instances of 3's since the last 2.
The question is: how do I integrate that array function directly into the final formula, so as not to have to waste a column with the interim calculation?
Edit
The list of numbers represents a level of indentation, so the end result will be a compound of these calculations with different offset checking to provide section numbering: 1; 1.1; 1.1.1, 1.2, 1.2.1, 1.2.2, etc
I want a single function that can calculate this entire depth level, without having to waste several columns identifying how many rows above the previous indent layer was defined.
Try in cell B14 this formula array:
{=COUNTIF(OFFSET($A14,0,0,
MAX(ROW($1:14)*($A$1:$A14=$A14-1))
-ROW($A14),1):$A14,$A14)}

Formula logic for extracting specific cell data

For extracting First £ figure and not the second one from sample data
(24M UNLTD+INS 30GB £347+£30 S6)
Following array formula has been used in Stackoverflow questions.
{=MID(A1,FIND("£",A1),MIN(IF(ISERROR(MID(MID(A1,FIND("£",A1)+1,999),ROW($1:$999),1)+0),ROW($1:$999),999)))}
I attempted an analysis of the formula as represented in the image below. I am not able to grasp the logic of part
MIN(IF(ISERROR(MID(MID(A1,FIND("£",A1)+1,999),ROW($1:$999),1)+0),ROW($1:$999),999))
As to how it leads to a figure of 4. Request that this part of formula be elaborated to clarify the role of various constituents of this formula.
Try the following as a standard (non-array) formula,
=--REPLACE(REPLACE(A2, 1, FIND("£", A2), ""), FIND("+", REPLACE(A2, 1, FIND("£", A2), "")), LEN(A2), "")
First the inside REPLACE(A2, 1, FIND("£", A2), "") erases everything up to the first £ symbol, then the same logic is applied to erase everything in that modified text from the first + to the end. The -- converts text-that-looks-like-a-number to an actual number.
The array formula you provided uses a more convoluted logic.
FIND("£", A2) + 1 finds the starting point of the first number after the first £ symbol. e.g. The first £ is the 20th character so it returns 21.
MID(A2, FIND("£",A2)+1, 999) extracts the text following that first £ symbol. The text might look like it starts with a number but it is text masquerading as a number. e.g. 347+£30 S6
In an array formula, ROW($1:$999) processes as a sequence of numbers from 1 to 999, incrementing by 1 for each cycle of calculation.
MID(MID(A1, FIND("£", A1) + 1, 999), ROW($1:$999), 1) + 0) returns an array of text values, each one 1 character long and 1 position deeper into the text than the previous one. e.g. 3, 4, 7, +, £, etc.
+0 is used to try and convert each of these pieces of text to a number. The IFERROR function returns TRUE if a piece of text cannot be converted to a true number. The first one that cannot be turned into a true number is the 4th e.g. +
The IF catches the TRUE on the fourth position and returns 4 from the second ROW($1:$999). It has returned 999 for positions 1, 2 and 3. e.g. 999, 999, 999, 4, 5, etc.
The MIN catches the smallest of these numbers returned as an array. This is where the 4 comes from. e.g. 999, 999, 999, 4, 5, 999, ...
You can see this yourself by changing all of the 999's to 9 then using the Evaluate Formula command. The reason changing to 9 is important is so that the returned arrays of number look like 1, 2, 3, 4, 5, 6, 7, 8, 9 which does not obfuscate the results quite a badly as 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 998, 999.
        This shows the formula evaluated several steps into the process. Note the 4 being returned to the MIN function.

Sort Part Numbers by First Character in Excel

I have an excel document with about 12,000 lines of part numbers with pricing information. I think the most intuitive way to sort them would be as follows:
1, 12, 15, 100, 10003, 2, 2002, 20005000, 3, 30, 333, 4, 5, 6, 700000, 800000.
All the numbers that begin with 1 are in the same spot, same with all 2 prefixes, etc. My problem is, excel's default sorting method sorts it like THIS:
1, 20, 30, 40, 100, 150, 200, 250, 500, 1000, 1500, 2000, 10000.
So it sorts them in regular ascending order, which I think makes it more difficult to find your part number, especially as they get super long as some of them do.
I don't have a lot of resources outside of excel at my workspace, so I would like to stick to using that. My knowledge of excel also isn't too impressive, so please treat me like an idiot!
To make thing easier to describe, let's assume that your list of part numbers is in column A, and that you have a blank column B.
In cell B2 enter this formula:
=TEXT(A1,"#")
Copy that down as far as your list goes.
Now, selecting both columns, Custom Sort by column B.
That's it.
Create a new column called SortOrder with the following formula:
=VALUE(LEFT(A2,1))
Then go to Home tab > Sort & Filter > Custom Sort.... Sort first by SortOrder, then by Value:
With data in column A like:
In B1 enter:
=CHAR(1) & A1
and copy down. Then sort cols A & B by B to get:
Is this "alphabetic-style" sort closer to what you need??

Resources