I have a use case wherein I get an amount in the form of a string and I need to compare it to an integer value. The string has the following conditions:
1. Its first three letters denote the currency
2. The string can consist of commas
For e.g. EUR 540,000 denotes 540000 Euros.
One way I can think of doing this is to:
1 Take a substring from the 4rth character using ?substring
2 Eliminate the commas using ?replace
3 Convert to a integer using ?number
4 and then compare
Is there a cleaner way to achieve this?
You can encapsulate that into a #function or a TemplateMethodModelEx. But really, the data-model (template context) you are using is rather strange. Especially if the template is supposed to do math, you should get the numbers (like BigDecimal-s or Integer-s), not those strings. So I would try to clean up the data-model before passing it to the template. That's the clean solution.
Related
I'm trying to add numbers that have the unit prefixes appended at the end (mili, micro, nano, pico, etc). For example, we have these two columns below:
Obviously doing something like =A2+A3+A4 and =B2+B3+B4 would not work. How would you resolve this? Thanks
Assuming you don't have excel version constraints, per the tags listed in your question. Put all the suffixes as delimiters inside {} delimited by a comma as follow in TEXTSPLIT, then define the conversion rules in XLOOKUP. We use SUBSTITUTE(col, nums, "") as input of XLOOKUP to extract the unit of measure.
=BYCOL(A2:B4, LAMBDA(col, LET(nums, 1*TEXTSPLIT(col,{"ms","us"},,1),
units, XLOOKUP(SUBSTITUTE(col, nums, ""), {"us";"ms"},{1;1000}),
SUM(nums * units))))
The above formula converts the result to a common unit of microseconds (us), i.e. to the lower unit, so milliseconds get converted by multiplying by 1000. If the unit of measure was not found it returns #N/A, it can be customized by adding a fourth parameter to XLOOKUP. If you want the result in milliseconds, then replace: {1;1000} with {0.001;1} or VSTACK(10^-3;1) for example.
If you would like to have everything in seconds, you can use the trick of using power combined with the XMATCH index position, to generate the multiplier. I took the idea from this question: How to convert K, M, B formatted strings to plain numbers?, check the answer from #pgSystemTester (for Gsheet, but it can be adapted to Excel). I included nanoseconds too.
=BYCOL(A2:B4,LAMBDA(col,LET(nums,1*TEXTSPLIT(col,{"ms","us"},,1),
units, 1000^(-IFERROR(XMATCH(RIGHT(col,2), {"ms";"us";"ns"}),0)),
SUM(nums * units))))
Under this approach, seconds is the output unit, because it is not part of the XMATCH lookup_array input argument, the multiplier will be 1 (as a result of 1000^0), so no units or seconds (s) will be treated the same way.
Notes:
In my initial version I used INDEX, but as #P.b pointed out in the comments, it is not really necessary to remove the second empty column, instead, we can use the ignore_empty input argument from TEXTSPLIT. Thanks
You can use TEXTBEFORE instead of TEXTSPLIT, as follows: TEXTBEFORE(A2:A4,{"ms","us"})
How can i create a thousand separator for every number which is in my string?
So for example this string:
string = "123456,78+1234"
should be displayed as:
TextView = "123.456,78+1.234"
And the string should be editable, so the thousand separator should adapt when i remove or add a digit.
I have already read all the posts I could find about it, but I could never find an up-to-date working answer. So I would be really grateful for your help!
Your question contains two sub-questions:
A. You want to add thousand separators to a string which contains a group of numbers.
B. You want it to change.
And the answers are:
A: In your example there's , as a delimiter, so you need to split the string using this delimiter to an array of strings.
Then iterate over them and have your dots added to every 3nth index of their characters; you can also use String.format("%,d", substr.toLong()).
Lastly, append all of the strings back together with , as the separator.
B: This one can be done in different ways. You may store the original string somewhere and observe it, so when it changes it goes to the function which does A, and use the function result the way you like (which I suppose is to be set in a TextView).
what is the easiest way with an Excel formula to extract certain details from a cell? So for example, if this is in cell A1 column=""HMI_LOCATE"" px=""CLASS"" position=""99"" validation=""ROOM"" then I'm trying to extract just the data the falls in between the double "" after the px= so in this example, I need to extract just the letters CLASS and nothing else, what is the easiest way to extract that data, the part I'm trying to extract won't always be 5 characters long it could be much longer or shorter.
Do you want to achieve this?
With o365 you can use this formula
=FILTERXML("<t><s>"&SUBSTITUTE(A1,CHAR(34)&CHAR(34),"</s><s>")&"</s></t>","//s[position() mod 2 = 0]")
or for older EXCEL-versions
=IFERROR(INDEX(FILTERXML("<t><s>"&SUBSTITUTE($A$1,CHAR(34)&CHAR(34),"</s><s>")&"</s></t>","//s"),ROW(A1)*2),"-")
This splits the string at the quotation marks (CHAR(34)) and builds an array of elements. Then every second element is put out.
For tons of other possibilities have a look at this awesome guide by JvdV.
EDIT:
To get the element after px= no matter where it is, you can use
=LET(list,
FILTERXML("<t><s>"&SUBSTITUTE($A$1,CHAR(34)&CHAR(34),"</s><s>")&"</s></t>","//s"),
INDEX(list,MATCH("px=",list,0)+1)
)
The LET-function lets you assign functions to variables which then can be used for further calculations.
I have a string of letters and numbers, where if the second two characters of the string equal a certain value, then a location value should be shown in the corresponding column.
I have used the MID function to essentially extract the characters of the string that I want to use MID(A2,2,2) but now I can't figure out how to compare what is returned to a list of options that those two characters could be without typing in each option in an extremely long formula.
Here are possible strings that are situated in a column:
3PH356969
MSFFACEBUS
MBH0007398
MBH0007402
I am extracting the second two characters of these, to compare to a list similar to this
PH
SF
BH
PG
HR
These values then correspond to location (below), which would optimally be returned:
Philadelphia
Bay Area
Birmingham
Western PA
Hartford
I can write =IF(MID(A2,2,2)="PH","Philadelphia",else...) but then the else-ifs will go on for 76 more 2-character strings to compare against. I'm hoping there is a more optimal way for this.
Expected results should be the location corresponding to the string, or just "error" displayed.
Basically we need to use a lookup/reference table, but instead of a much more common VLOOKUP function we can use a much faster INDEX + MATCH combo.
Formula in B1:
=INDEX($E$1:$E$6,MATCH(MID(A1,2,2),$D$1:$D$6;0))
I would use a VLOOKUP, personally. Although it would require a separate lookup table, just feed your MID result as the VLOOKUP key. Then you could easily add/remove locations, and there will be an #N/A error if the key's not there.
If you don't want a separate lookup table, you may try it this way:
=IFERROR(INDEX({"Philadelphia","Bay Area","Birmingham","Western PA","Hartford"},MATCH(MID(A2,2,2),{"PH","SF","BH","PG","HR"},0)),"Not found")
I have a free-form text custom CRM field that is displayed as time that I would like to convert to a number in order to do basic arithmetic operations. The field is called {actualwork} and displays like this, 3:00. I want to divide it by a decimal number field called {custeven10} and display it in percent.
By using TO_NUMBER and SUBSTR I can convert the text to a number but the number of hours can be higher than a single digit so I don't know how to use the SUBSTR command to split my field. Right now I'm using this command but it only uses the first digit of the string.
TO_NUMBER(SUBSTR{actualwork},1,1))/{custevent10}
Does anyone know how I could separate the characters before and after the ":" into a two strings? Thank you for your time.
I used this formula to transform the text into number and separate the minutes and the hours:
(TO_NUMBER(SUBSTR({actualwork}, 1, LENGTH({actualwork})-3))+TO_NUMBER(SUBSTR({actualwork}, LENGTH({actualwork})-1,2))/60)/{custevent10}