Why won't this DAX formula work? - excel

I'm currently using a DAX formula to try and remove numbers from a string. The string is the first half of a postcode. I've therefore typed the following formulae
=if(Istext(mid([Postcode District],1,1)),(left([postcode district],2)),(left([postcode district],1)))
what this SHOULD do is see if the second character is text and return 2 letters if it is, one if it isn't.
For example, input CA1 should return CA whilst B22 should return B
This simply isn't doing that, and I'm not sure why. all that is being returned is the first two letters whether the second letter is text OR numeric.

The MID function takes a string as an argument and will always return a string (even if it looks like a number), so your IsText is always TRUE.
Try this
=if(IsNumber(mid([Postcode District],1,1)*1),(left([postcode district],1)),(left([postcode district],2)))

ended up using =SUBSTITUTE(substitute(SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE([Postcode District],"1"," "),"2"," "), "3"," "),"4"," "),"5"," "), "6"," "),"7"," "),"8"," "),"9"," "),0,"")
(as its DAX it'll accept this - it's too many nested functions for standard XL)
As it's quite an ugly formulae, I'll be trying other people's answers too, to see if there's a more elegant solution :)

Related

Find specific characters and return the next value in the cell using Excel Formula

I am not sure where to begin with the formula as I have gotten myself so confused with everything. I have a cell the contains "PON " or "PON: " or "PON = " then the actual PON (Example: PON 123467) I want to formula to return 123467 in the cell.
Examples What I want returned
I have PON 123467 for shoes 123467
I have PON: 234567-AB for food 234567-AB
I have PON - 569874-Weird for accessories 569874-Weird
I have PON = DOG-564-987 for dog food DOG-564-987
I am currently using Excel 365
Filterxml() will give you best companion here in this case. Try-
=FILTERXML("<t><s>"&SUBSTITUTE(FILTERXML("<t><s>"&SUBSTITUTE(A1," for","</s><s>")&"</s></t>","//s[1]")," ","</s><s>")&"</s></t>","//s[last()]")
Using FILTERXML, and testing for a substring following PON, you can try:
=FILTERXML("<t><s>"&SUBSTITUTE(TRIM(A1)," ","</s><s>") & "</s></t>","//s[contains(.,'PON')]/following-sibling::*[string-length(.)>2][1]")
Note that FILTERXML solution will cause a PON that is solely numeric, but with a leading zero, to drop the leading zero. Unfortunately, the xPath implementation in that function does not include the string() function
If dropping the leading zero might be a problem, you can add a character to the node that will force the number to be seen as a string. In the modified formula below, I use the unicode zero-width space, but there are others you can use. Note that this will count as a character for the string=length function, so be sure to maintain the >2 parameter:
=FILTERXML("<t><s>"&SUBSTITUTE(TRIM(A1)," ","</s><s>"&UNICHAR(8203)) & "</s></t>","//s[contains(.,'PON')]/following-sibling::*[string-length(.)>2][1]")
Because of the variablity in your data, that sometimes there are extraneous space-separated substrings between PON and your desired extract, the xpath:
locates the substring PON
returns all subsequent siblings that have a string-length of more than two (adjust if necessary)
returns the first sibling that meets that criterion.
You might try this formula.
=TRIM(LEFT(MID(A2,FIND(#{1,2,3,4,5,6,7,8,9},A2),100),FIND(" ",MID(A2,FIND(#{1,2,3,4,5,6,7,8,9},A2),100))))
It extracts the text between the first number and the first space following that number. The size of that extract is limited to 100 characters.

Excel: Formula to combine potentially incomplete address components into an address string with appropriate separators

I need to intake U.S. address data by street, city, state, and ZIP code and output an address in the following format:
1502 Bruce Rd, Oreland, PA 19075
without any superfluous spaces.
Any component of the address can be missing, so each of the four elements binarily exists or doesn't, which results in a total of 4! = 24 permutations of having or not having each of the four elements. Excluding the case where they're all missing, that leaves 23 permutations to deal with.
There seem to be lots of questions about splitting addresses apart, but none about combining them back together, especially when you need to either include or exclude the spacing based on what comes after. What's the best way to handle this? Bonus points for a way that's easily extensible (for example, I won't be surprised if later we have to include a unit field between street and city at some point in the future).
As I was writing up this question, I came up with a couple solutions, where columns A, B, C, and D are street, city, state, and ZIP code, respectively.
First is this one:
=TRIM(
IF(A1="", "", A1 & IF(B1&C1&D1="", "", ", ")) &
IF(B1="", "", B1 & IF( C1&D1="", "", ", ")) &
IF(C1="", "", C1 & IF( D1="", "", " " )) &
IF(D1="", "", D1)
)
which works as follows:
See if the element exists and, if so, include it.
If the element exists, see if anything exists after it and, if so, add the spacer that follows it. (This works because spacers seem be determined by what is before the spacer.)
And the whole thing's wrapped in a TRIM to get rid of unnecessary user-input spaces.
To check if anything after an element exists, I concatenated the following fields and checked to see whether that concatenation was a blank string.
Because of the use of concatenation here, I thought it might be easier to use TEXTJOIN and came up with this:
=TEXTJOIN(" ", TRUE, TEXTJOIN(", ", TRUE, TRIM(A1), TRIM(B1), TRIM(C1)), TRIM(D1))
which works as follows:
The inner TEXTJOIN combines the first three elements (street, city, and state) with the common delimiter of a comma + a space.
Once that's done, the outer TEXTJOIN combines the result of that with the ZIP code using a space as a delimiter.
TRIM used again as above.
These seem to cover all 23 cases and aren't too hard to extend if additional fields need to be added, although I'm definitely open to any better solutions you might have.

Excel get substring from nth position up to the end of string

So How to get substring from nth position up to the end of string?
Input at cell A1 Name: Thomas B.
Expected output: Thomas B.
I know some way to do it but I wonder if there are other elegant ways than them? (some kind of =RIGHT(A1, -6)....)
=MID(A1, 6, 999999) //999999 looks not so good
=MID(A1, 6, LEN(A1) - 5) //must calculate 2 times, first get len, then get substring, seems too much works?
REPLACE
As Dominique already wrote:
'Why don't you just replace the first six characters by an empty string?'
=REPLACE(A1,1,6,"")
I've done some time measuring, but the difference is less than a second at 50000 records (for LEFT, MID, REPLACE & SUSTITUTE). So I'm afraid ELEGANCE is all you're going to get.
A Small Study
I created this study due to the fact that when you say from the n-th character, your n-th character is 7 (your MID-s are wrong), but you want to remove the first n-1 (6) characters. So depending on how you formulate your question, you might have a different approach in RIGHT or MID, and you will remember REPLACE and SUBSTITUTE or you may not.
Small Study Formulas for A1 (*) and B1 (#, ?, *)
Get String From N-th Character to the End, e.g. 7
=RIGHT(A1,LEN(A1)-(B1-1))
=RIGHT(A1,LEN(A1)-B1+1)
=RIGHT(A1,LEN(A1)-6)
=MID(A1,B1,LEN(A1)-(B1-1))
=MID(A1,B1,LEN(A1)-B1+1)
=MID(A1,B1,LEN(A1))
=MID(A1,7,LEN(A1)-6)
=MID(A1,7,LEN(A1))
Remove N First Characters of a String, e.g. 6
=RIGHT(A1,LEN(A1)-B1)
=RIGHT(A1,LEN(A1)-6)
=MID(A1,B1+1,LEN(A1)-B1)
=MID(A1,B1+1,LEN(A1))
=MID(A1,7,LEN(A1)-6)
=MID(A1,7,LEN(A1))
Get String After a Character e.g. " "
=RIGHT(A1,LEN(A1)-(FIND(B1,A1)))
=RIGHT(A1,LEN(A1)-(FIND(" ",A1)))
=MID(A1,FIND(B1,A1)+1,LEN(A1)-FIND(B1,A1))
=MID(A1,FIND(B1,A1)+1,LEN(A1))
=MID(A1,FIND(" ",A1)+1,LEN(A1)-FIND(" ",A1))
=MID(A1,FIND(" ",A1)+1,LEN(A1))
Get String After a String e.g. ": "
=RIGHT(A1,LEN(A1)-(FIND(B1,A1)+LEN(B1))+1)
=RIGHT(A1,LEN(A1)-FIND(B1,A1)-LEN(B1)+1)
=RIGHT(A1,LEN(A1)-FIND(": ",A1)-LEN(": ")+1)
=MID(A1,FIND(B1,A1)+LEN(B1),LEN(A1)-(FIND(B1,A1)+LEN(B1))+1)
=MID(A1,FIND(B1,A1)+LEN(B1),LEN(A1)-FIND(B1,A1)-LEN(B1)+1)
=MID(A1,FIND(B1,A1)+LEN(B1),LEN(A1))
=MID(A1,FIND(": ",A1)+LEN(": "),LEN(A1)-FIND(": ",A1)-LEN(": ")+1)
=MID(A1,FIND(": ",A1)+LEN(": "),LEN(A1))
Back to Remove N First Characters of a String, e.g. 6
=SUBSTITUTE(A1,LEFT(A1,6),"",1)
=REPLACE(A1,1,6,"")
Well, both of your methods already work, but you could also use this one:
=RIGHT(A1,LEN(A1)-6)
(you nearly had this one in your own question)
or this one:
=TRIM(MID(A1,FIND(":",A1)+1,100))
(the FIND() function returns the numeric position of a search string, so is great for doing dynamic substrings)
Why don't you just replace the first six characters by an empty string?
=SUBSTITUTE(A1;LEFT(A1;6);"";1)
Another possibility is that you create a constant with the value 2^31-1 (=2147483647), which is the maximum signed integer value on 32-bit systems, and you give it a nice name, like MaxInt, then your first formula will be efficient and nice looking, too:
=MID(A1, 6, MaxInt)
You can add the Name with Ctrl+F3. If you are interested in fast calculations, giving it as 2147483647 rather than 2^31-1 may have some (very little) advantage.

Excel to find a specific substring within a cell

I am trying to extract a string from a cell and I am running into some trouble. In this example I am looking to extract the string ARM52CVA from
A18031600473 ( FLORENCE - ARM52CVA )
Ive tried this formula and it doesnt seem to work for me
=RIGHT(C3,SEARCH("-",C3))
You would have come closer with either of:
=RIGHT(C3,LEN(C3)- SEARCH("-",C3))
=MID(C3,FIND("-",C3)+2,99)
but they would have left the ending.
If your data is exactly as you show it, with all of the data and spaces as shown, then try:
=INDEX(TRIM(MID(SUBSTITUTE(C3," ",REPT(" ",99)),{1,99,198,297,396},99)),5)
If there is more variability, you'll need to show more data.
Try,
=REPLACE(REPLACE(A1, 1, FIND("- ", A1)+1, ""), FIND(" ", REPLACE(A1, 1, FIND("- ", A1)+1, "")), LEN(A1), "")
The methods above are far more reliable but if the white space is consistent you can also use
=MID(A1,FIND("-",A1,1)+2,(FIND(")",A1,1)-3-FIND("-",A1,1)+1))

Substring in excel

I have a set of data that shown below on excel.
R/V(208,0,32) YR/V(255,156,0) Y/V(255,217,0)
R/S(184,28,16) YR/S(216,128,0) Y/S(209,171,0)
R/B(255,88,80) YR/B(255,168,40) Y/B(255,216,40)
And I want to separate the data in each cell look like this.
R/V 208 0 32
R/S 184 28 16
R/B 255 88 80
what is the function in excel that I can use for this case.
Thank you in advance.
kennytm doesn't provide an example so here's how you do substrings:
=MID(text, start_num, char_num)
Let's say cell A1 is Hello.
=MID(A1, 2, 3)
Would return
ell
Because it says to start at character 2, e, and to return 3 characters.
In Excel, the substring function is called MID function, and indexOf is called FIND for case-sensitive location and SEARCH function for non-case-sensitive location. For the first portion of your text parsing the LEFT function may also be useful.
See all the text functions here: Text Functions (reference).
Full worksheet function reference lists available at:
    Excel functions (by category)
    Excel functions (alphabetical)
Another way you can do this is by using the substitute function. Substitute "(", ")" and "," with spaces.
e.g.
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1, "(", " "), ")", " "), ",", " ")
I believe we can start from basic to achieve desired result.
For example, I had a situation to extract data after "/". The given excel field had a value of 2rko6xyda14gdl7/VEERABABU%20MATCHA%20IN131621.jpg . I simply wanted to extract the text from "I5" cell after slash symbol. So firstly I want to find where "/" symbol is (FIND("/",I5). This gives me the position of "/". Then I should know the length of text, which i can get by LEN(I5).so total length minus the position of "/" . which is LEN(I5)-(FIND("/",I5)) . This will first find the "/" position and then get me the total text that needs to be extracted.
The RIGHT function is RIGHT(I5,12) will simply extract all the values of last 12 digits starting from right most character. So I will replace the above function "LEN(I5)-(FIND("/",I5))" for 12 number in the RIGHT function to get me dynamically the number of characters I need to extract in any given cell and my solution is presented as given below
The approach was
=RIGHT(I5,LEN(I5)-(FIND("/",I5))) will give me out as VEERABABU%20MATCHA%20IN131621.jpg . I think I am clear.
Update on 11/30/2022
With new excel functions, you can use the following in cell C1 for the input in A1:
=TEXTJOIN(" ",,TEXTSPLIT(A1,{"(",",",")"}))
Here is the output:
What about using Replace all?
Just replace All on bracket to space.
And comma to space. And I think you can achieve it.

Resources