Extract part of text in Excel with condition - excel

I know this questions seems redundant but cannot find an answer. I have a text string such as:
A-B-C-D-E-F
I would like to extract only the first 4 items separated by "-". Result should be
A-B-C-D
Alternatively, I am also looking to extract only the 3rd item:
C
I have been trying the MID and SEARCH functions with no success.
Any help would be well appreciated.

Using TEXTSPLIT (As of this writing only available to Office 365 Insider Beta Channel)
=TEXTJOIN("-",TRUE,INDEX(TEXTSPLIT(A1,"-"),1,{1,2,3,4}))
Change the {1,2,3,4} to 3 for the third.
If one does not have TEXTSPLIT but has TEXTJOIN and is on a PC then:
=TEXTJOIN("-",TRUE,INDEX(FILTERXML("<a><b>"&SUBSTITUTE(A1,"-","</b><b>")&"</b></a>","//b"),1,{1,2,3,4}))
Without TEXTJOIN it will require vba. Like this UDF that mimics TEXTJOIN: MS Excel - Concat with a delimiter

So, this is what I have tried,
• Formula used in cell B1
=TEXTJOIN("-",,TAKE(TEXTSPLIT(A1,"-"),,4))
• Formula used in cell D1
=TEXTJOIN("-",,DROP(TEXTSPLIT(A1,"-"),,-2))
• Formula used in cell C1
=TEXTJOIN("-",,INDEX(TEXTSPLIT(A1,"-"),,3))
Note: Formulas shown above works for O365 Users, Insiders Beta Channel users only!
However, if you have access to Excel 2019, then you can use either TEXTJOIN() or CONCAT()
• Formula used in cell B9
=SUBSTITUTE(CONCAT("-"&INDEX(FILTERXML("<t><s>"&SUBSTITUTE(A9,"-","</s><s>")&"</s></t>","//s"),ROW(A1:A4))),"-","",1)
• Formula used in cell C9
=SUBSTITUTE(CONCAT("-"&INDEX(FILTERXML("<t><s>"&SUBSTITUTE(A9,"-","</s><s>")&"</s></t>","//s"),3)),"-","",1)
Since OP has mentioned in comments, that OP is using O365 in MAC, hence here is an update.
• Formula used in cell B1
=TEXTJOIN("-",,TRIM(MID(SUBSTITUTE(A1,"-",REPT(" ",100)),COLUMN(A1:D1)*99-98,99)))
• Formula used in cell C1
=TRIM(MID(SUBSTITUTE(A1,"-",REPT(" ",100)),COLUMN(C1)*99-98,99))
Or,
• Formula used in cell D1
=TRIM(MID(SUBSTITUTE(A1,"-",REPT(" ",100)),200,100))

For Excel MAC and earlier versions:
Extract first four:
=LEFT(A1,FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),4))-1)
Extract the third:
=MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),2))+1, FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),3))-FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),2))-1)

Easier to understand (now and in the future) if done in small steps.
Assuming source data is in A2:
in B2: =FIND("-",$A2)
in C2: =FIND("-",$A2,B2+1)
in D2, E2: copy across from C2
Then in F2: =LEFT(A2,E2-1)
You can put all of that into one formula, but that's more error-prone.

Related

Get the latest date in two different tables and return the corresponding value

I'm trying to get the latest date in two different tables and return the corresponding value in excel.
This is what I would want it to be like:
I'm new at excel so I've just been looking all over the internet for a solution but cant find anything.
Posted an an Answer, as confirmed by OP its working for them:
Try either of the one:
• Formula used in cell A6
=#CHOOSECOLS(SORT(VSTACK(A1:B4,D1:E4),1,-1),2)
Or,
• Formula used in cell B6
=INDEX(SORT(VSTACK(A1:B4,D1:E4),1,-1),1,2)
• Formula used in cell D6
=TAKE(SORT(VSTACK(A1:B4,D1:E4),1,-1),1,-1)

need to count only numbers after underscore symbol in excel

I need to check if numbers are there after underscore symbol and put that numbers count as shown below.
I am very new to excel, please how to write a formula for this.
Thank you so much in advance
Perhaps you can try something like this. Not sure about your Excel Version.
• Formula used in cell B1
=REPLACE(A1,FIND("#",SUBSTITUTE(A1,"_","#",5)),255,
"_\d{"&LEN(-LOOKUP(0,-RIGHT(A1,ROW($ZY$1:INDEX($Z:$Z,LEN(A1))))))&"}")
If you have access to MS365 then you could try as below,
• Formula used in cell C1
=LET(x,TEXTSPLIT(A1,"_"),
c,LEN(TAKE(x,,-1)),
TEXTJOIN("_",,DROP(x,,-1),"\d{"&c&"}"))
I tried made it short in MS365 version
• Formula used in cell D1
=TEXTBEFORE(A1,"_",-1)&"_\d{"&LEN(TAKE(TEXTSPLIT(A1,"_"),,-1))&"}"
With One Spill Array Formula.
• Formula used in cell D1
=MAP(A1:A3,LAMBDA(m,
TEXTBEFORE(m,"_",-1)&"_\d{"&LEN(TAKE(TEXTSPLIT(m,"_"),,-1))&"}"))
I make the assumption that the characters "cm_" are always before the number:
=len(mid(A1,find("cm_",A1,1)+3,30))
The len() counts the number of characters,
mid() gets the numbers occurring after the text defined in find()
Then:
LEFT(A1,FIND("cm_",A1,1)+2)&"\d{"&LEN(MID(A1,FIND("cm_",A1,1)+3,30))&"}"
My output is here in cell D2..
=REPLACE(C2,MATCH(TRUE,ISNUMBER(--MID(C2,SEQUENCE(LEN(C2)),1)),0),LEN(C2),"\d{"&SUMPRODUCT((ISNUMBER(--MID(C2,SEQUENCE(LEN(C2)),1))*1))&"}")
if your excel doesn't support for Sequence function then you can use ROW(INDIRECT("1:"&LEN(C2)) instead..

Excel formula to lookup a table from another table with comma separated output

I am trying to get a formula that looks up information from another table and populates the output with comma separated values. As shown below, I want to populate column D in Table 2 with information from Table 1. The desired output is in column E. I came up with this formula but it's only pulling one city per person.
TEXTJOIN(", ",,INDEX('Table 1'!B:B,MATCH(FILTERXML(""&SUBSTITUTE(C3,",","")&"","//m"),'Table 1'!A:A,0)))
For you reference, you can see the screenshot below,
Solution as posted by Scott Craner Sir using FILTERXML()
• Formula used in cell G8
=TEXTJOIN(", ",,FILTER($B$3:$B$9,ISNUMBER(MATCH($A$3:$A$9,FILTERXML("<m><b>"&SUBSTITUTE(F3,", ","</b><b>")&"</b></m>","//b"),0)),""))
Note: Depending on one's Excel version this may needs to be keyed with CTRL+SHIFT+ENTER instead of Enter when exiting edit mode.
Perhaps, if you are using MS365 then you can use ARRAYTOTEXT() with TEXTSPLIT() as well,
• Formula used in cell G3
=ARRAYTOTEXT(FILTER($B$3:$B$9,ISNUMBER(MATCH($A$3:$A$9,TEXTSPLIT(F3,,", ",1),0)),""))
Kindly change the cell range/sheet preferences as per your suit.
Edit
Thanks! It appears to be working now. However, there is one down side that I am hoping you can help with. If there is only one person listed, it's returning a #calc error. This formula only appear to be working when there are multiple staff present. Any idea on how to fix this?
• Formula used in cell G8
=TEXTJOIN(", ",,FILTER(B:B,ISNUMBER(MATCH(A:A,FILTERXML("<m><b>"&SUBSTITUTE(F8&", ",", ","</b><b>")&"</b></m>","//b"),0))))
Or,
• Formula used in cell G3
=ARRAYTOTEXT(FILTER($B:$B,ISNUMBER(MATCH($A:$A,TEXTSPLIT(F3&", ",,", ",1),0)),""))
Try:
Formula in D3:
=TEXTJOIN(", ",,IF(ISNUMBER(XMATCH("*, "&A$3:A$9&", *",", "&C3&", ",2)),B$3:B$9,""))
Note I concatenated leading/trailing comma/space's to prevent possible false positives.
Also, in case you wish to avoid duplicates, just nest UNIQUE() in there:
=TEXTJOIN(", ",,UNIQUE(IF(ISNUMBER(XMATCH("*, "&A$3:A$9&", *",", "&C3&", ",2)),B$3:B$9,"")))

How to convert a table to a list with row & column header

Is there formala in excel or google spreed sheet can convert a table to a list like this?
from this table:
to this list:
You may try any one of the following approaches, as per your Excel version.
• If you are using Excel 2010/2013/2016/2019/2021/MS365, then this one works for all
Formula used in cell A6
=INDEX($A$2:$A$4,INT((ROW()-6)/COLUMNS($B$1:$D$1))+1)
Formula used in cell B6
=INDEX($B$1:$D$1,MOD((ROW()-6),ROWS($A$2:$A$4))+1)
Formula used in cell C6
=INDEX($B$2:$D$4,MATCH(A6,$A$2:$A$4,0),MATCH(B6,$B$1:$D$1,0))
Note: You may need to change the (ROW()-6) depending on the ROW() from where you are entering the formula for transformation. Example, like if you start from cell or row 3 then it will be (ROW()-3) so on so forth accordingly as per your suit.
• If you are using MS365 and when writing if you have enabled the Office Insiders Beta Channel Version then, using LAMBDA() HSTACK() TEXTSPLIT() DROP() TAKE() TOCOL() & TEXTJOIN() can achieve it as well.
=LAMBDA(array,
HSTACK(TEXTSPLIT(TEXTJOIN(".",1,
DROP(TAKE(array,,1)&"-"&TAKE(array,1),1,1)),"-","."),
TOCOL(DROP(array,1,1))))(A1:D4)
• Also Note : You can use a custom, reusable formula with a friendly name by placing the above formula in the Name Manager - with Define Name as UNPIVOT.
So from Formulas tab, Click on Define Name --> Enter the name as UNPIVOT and place the above formula as in refers to
=LAMBDA(array,
HSTACK(TEXTSPLIT(TEXTJOIN(".",1,
DROP(TAKE(array,,1)&"-"&TAKE(array,1),1,1)),"-","."),
TOCOL(DROP(array,1,1))))
The syntax is
=UNPIVOT(array)
Where array is A1:D4
Closing Note: You can also achieve the above transformation using Power Query as well.

Excel if else formula fails due to wrong syntex

I have different class name in excel column in character form. I want to convert it to numerical form and add in column C
If B2==N, make it 1
If B2==O, make it 0
If B2==A, make it 2
If B2==~, make it 9
I have written the following excel formula. I have added my formula and error message in image. Can you correct me where I am making mistake
=IF(B2='N',1,if(b2='O',0,if(b2='A',2,if(b2='~',9))))
Update
After adding the formula as per details answer
Personally I would do something like this, using IFS function:
=IFS(B2="N",1,B2="O",0,B2="A",2,B2="~",9)
Within Excel formulas, double quotes are used to denote the start and end of a text string. Numeric should not be within double quotes.
That said, we can use a formula like IF function but we can also avoid using double quotes by creating a table of reference, refer the image below, for alternative solutions - 7 APPROACHES
• Formula used in cell C2
=VLOOKUP($B2,$I$2:$J$5,2,1)
Without Table Of Reference the above formula can be written as
=VLOOKUP($B2,{"~",9;"A",2;"N",1;"O",0},2,1)
• Formula used in cell D2
=LOOKUP($B2,$I$2:$J$5)
Without Table Of Reference the above formula can be written as
=LOOKUP($B2,{"~",9;"A",2;"N",1;"O",0})
• Formula used in cell E2 --> "Correction To Your Own Formula"
=IF(B2="N",1,IF(B2="O",0,IF(B2="A",2,IF(B2="~",9))))
• Formula used in cell F2
=IFS(B2="~",9,B2="A",2,B2="N",1,B2="O",0)
• Formula used in cell G2
=SWITCH($B2,"~",9,"A",2,"N",1,"O",0)
Note the formulas used in cell F2 & G2 you need to have either Excel 2019, Excel 2021 Or O365 hence its better to use a Table Of Reference to avoid using Double Quotes.
So, if there is a space before each characters then the formula, shall change refer the image below and & the formulas,
• Formula used in cell C2
=VLOOKUP(TRIM($B2),$I$2:$J$5,2,1)
• Formula used in cell D2
=LOOKUP(TRIM($B2),$I$2:$J$5)
• Formula used in cell E2
=IF(TRIM($B2)="N",1,IF(TRIM($B2)="O",0,IF(TRIM($B2)="A",2,IF(TRIM($B2)="~",9))))
• Formula used in cell F2
=IFS(TRIM($B2)="~",9,TRIM($B2)="A",2,TRIM($B2)="N",1,TRIM($B2)="O",0)
• Formula used in cell G2
=SWITCH(TRIM($B2),"~",9,"A",2,"N",1,"O",0)
And if those are not spaces and actually are non printable characters then just change the TRIM to SUBSTITUTE Function
C2
=VLOOKUP(SUBSTITUTE($B2,CHAR(160),""),$I$2:$J$5,2,1)
D2
=LOOKUP(SUBSTITUTE($B2,CHAR(160),""),$I$2:$J$5)
E2
=IF(SUBSTITUTE($B2,CHAR(160),"")="N",1,IF(SUBSTITUTE($B2,CHAR(160),"")="O",0,IF(SUBSTITUTE($B2,CHAR(160),"")="A",2,IF(SUBSTITUTE($B2,CHAR(160),"")="~",9))))
F2
=IFS(SUBSTITUTE($B2,CHAR(160),"")="~",9,SUBSTITUTE($B2,CHAR(160),"")="A",2,SUBSTITUTE($B2,CHAR(160),"")="N",1,SUBSTITUTE($B2,CHAR(160),"")="O",0)
G2
=SWITCH(SUBSTITUTE($B2,CHAR(160),""),"~",9,"A",2,"N",1,"O",0)
In Excel, double quotes is used to enclose character values not single quote. Hence, your formula will become
=IF(B2="N",1,IF(B2="O",0,IF(B2="A",2,IF(B2="~",9,""))))
Additionally, I have added an else clause with blanks in last IF.
Also, if you are on newer versions of Excel, then you can also use SWITCH which is easier and shorter
=SWITCH(B2,"N",1,"O",0,"A",2,"~",9,"")

Resources