Remove Zeros Before Decimal- PowerBI - powerbi

Numbers under 1 are currently being represented with a leading zero before the decimal point (example: 0.50). Because I'm working with baseball statistics (which almost never have the zero before the decimal) I would like to remove that. I want to keep the number before the decimal if its greater than 1 though. How would I do that?
For instance if I'm working with this measure. Is there something I can add to that?
AVG = SUM(Batter[H])/sum(Batter[AB])
Thanks. I appreciate the help.
Here is some sample data
Name AB H
Gleyber Torres 546 152
Brett Gardner 491 123
Aaron Judge 378 103
Adam Ottavino 0 0
Aroldis Chapman 0 0

The NAN error is occurring because you are dividing by 0. You should add an IF condition to avoid that:
AVG = IF(sum(Batter[AB])=0,BLANK(),SUM(Batter[H])/sum(Batter[AB]))
To tackle the formatting issue you can use the FORMAT function as mentioned by Andrey:
AVG = IF(sum(Batter[AB])=0,BLANK(),FORMAT(SUM(Batter[H])/sum(Batter[AB]),"###.0#"))
Hope this helps.

Unfortunately, it isn't directly possible. However, in the last step (the visualization of the data), you can convert the decimal number to text and format it as you want. For example, your measure could be like this:
AVG = FORMAT(SUM(Batter[H])/SUM(Batter[AB]), "#,###.00")
This will give you 2 decimal places (0 means that there will be a digit displayed at this position), but the digits before the decimal are optional (# means it will show a digit, but will omit the leading zeros) or here are some examples:

Related

Extract Number from String Field, Including Decimal (Postgres 9.5)

Suppose I have a field:
product_strength
10MG/ML
0.25MG
25MG
0.125MG
How do I extract just the "numeric" part and then cast to numeric? I can get this far: regexp_replace(product_strength, '(\D|!\.)','','g')::numeric AS result_numeric
But the problem with this is that it doesn't actually account for the decimal. In other words, this returns
product_strength result_numeric
10MG/ML 10
0.25MG 25
25MG 25
0.125MG 125
But I would want to return
product_strength result_numeric
10MG/ML 10
0.25MG 0.25
25MG 25
0.125MG 0.125
I would use regexp_matches for this:
select (regexp_matches(product_strength, '[0-9]+\.?[0-9]*'))[1]::numeric
from the_table
regexp_matches() returns an array of all matched strings, that's why the [1] is needed.
Try this regex to match the numbers;
\d+\.?\d*
Edit: as "Boolean_Type" says, if you need negative numbers too, you could add in an optional negative sign, and use;
\-?\d+\.?\d*

Regex for price, requiring decimal point and 2 decimal places

I am trying to validate a price field in Javascript.
The value can only be numbers, must have 1 decimal point, and must have 2 decimal places after it. Only 7 digits can be in front of the decimal point. Like: 1000000.00
Accepted:
123.00
1.01
0.01
4576.23
1234567.00
1.00
Not accepted:
0.00 (Cannot be free)
0.1 (not 2 decimal places)
1.0 (not 2 decimal places)
01.01 (Cannot start with 0)
12345678.00 (too many digits)
123 (no decimal point and 2 places)
-123.12 (negative, and unacceptable character)
123.123 (too many places)
I am unsure how to approach this problem and any help would be appreciated. A simple guide on how to do write my own regex would be helpful too as English is not my strong point. Thanks in advance.
Here's what I tried on my own: /^[0-9]+.[0-9]{2}$/
But I am unsure how to approach the 0 and length problem.
This regular expression will solve your problem. I have checked it against all the options you have given in question.
^(0(?!\.00)|[1-9]\d{0,6})\.\d{2}$
If you don't know how to test a regular expression against a string in JavaScript you can check below link.
http://www.w3schools.com/jsref/jsref_regexp_test.asp
Try this pattern ^(?!^0\.00$)(([1-9][\d]{0,6})|([0]))\.[\d]{2}$
It excludes 0.00 and negative numbers and any spaces before or after the number (negative cases)
Hope I covered all possibilities
You can check for test cases here
The following regex pattern matches lines with 1 up to 7 digits followed by a "." and 2 more digits excluding those starting with 0 (or a letter)
^[1-9]\d{0,6}\.\d{2}$

Regular expression for price validation

Need regular expression which have:
Maximum 8 digits before decimal(.) point
Maximum 4 digits after decimal point
Decimal point is optional
Maximum valid decimal is 8 digits before decimal and 4 digits after decimal
So 99999999.9999
The regular rexpression I have tried ^\d{0,8}[.]?\d{1,4}$ is failing for 123456789
and more than this. means it is taking more than 8 digits if decimal point is not available.
Tested here : http://regexpal.com/
Many many thanks in advance!
^\d{0,8}(\.\d{1,4})?$
You can make the entire decimal optional
You can try this:
^\d{1,8}(?:\.\d{1,4})?$
or
^[1-9]\d{0,7}(?:\.\d{1,4})?$
If you don't want to have a zero as first digit.
You can allow this if you want: (.1234)
^[1-9]\d{0,7}(?:\.\d{1,4})?|\.\d{1,4}$
Any of the above did not work for me.
Only this works for me
^([0-9]{0,2}((.)[0-9]{0,2}))$
This regex is working for most cases even negative prices,
(\-?\d+\.?\d{0,2})
Tested with the following,
9
9.97
37.97
132.97
-125.55
12.2
1000.00
10000.00
100000.00
1000000.00
401395011
If there is a price of $9.97, £9.97 or €9.97 it will validate 9.97 removing the symbol.
1-(\$+.[1-9])
2-(\£+.[1-9])
You can use this expression for complete price digits.
I'm using this:
^[1-9]\d{0,7}(\.\d{1-4})$
^ = the start of the string
[1-9] = at least the string has to begin with 1 number between 1 and 9
\d{0,7} = optional or max 7 times d (digit: a number between 0 and 9)
() = create a group like a substring
. = need a .
\d{1-4} = digit repited max 4 time
$ end of the string
For price validation we can not allow inputs with leading repeating zeros like 0012 etc.
My solution check for any cases. Also it allows maximum 2 decimal point after the dot.
^(?:0\.[0-9]{1,2}|[1-9]{1}[0-9]*(\.[0-9]{1,2})?|0)$

Regex Numerical Range 1 - 1 million

I'm looking for a expression range for monetary purposes. It needs to be 1 - 1 million and allow commas and periods. I don't need a min/max of (, and .) for correct formatting but I would like the digits after a period to be a min/max of 2 for actual cent values. Thanks
In Range:
640 or 5,000.35 or 999,000
Not in Range:
01 or 1,000,000.01 or 333,567.678
What I would suggest is :
Use something like that to verify that the input has a specific format :
(here's a demo - http://regexr.com?30l28)
(1[\.,])?([0-9]{1,3}[\.,])?([0-9]{1,3})([\.,][0-9]{1,2})
And then test the value range :
is value<1.000.000?
My regex is by no means 100% complete, but it DOES verify your general number format though.
This should do it:
^(1(\.\d{2})?|[1-9]\d{0,2}(,?\d{3})?(\.\d{2})?)|1((,000){0,2}|(000){0,2})(\.00)?$
But it would probably easier if you normalize the value first (e. g. remove any character except digits and the .) and then parse it.

RegEx for value Range from 1 - 365

What is the RegEx for value Range from 1- 365
Try this:
^(?:[1-9]\d?|[12]\d{2}|3[0-5]\d|36[0-5])$
The start anchor ^ and end anchor
$ are to match the whole input and
not just part of it.
(? ) is for grouping.
| is for alternation
[1-9]\d? matches 1 to 99
[12]\d{2} matches 100 to 299
3[0-5]\d matches 300 to 359
36[0-5] matches 360 to 365
You would have to list the possible combinations 1-9, 10-99, 100-299, 300-359, 360-365:
^([1-9]\d?|[12]\d\d|3[0-5]\d|36[0-5])$
Not really a good fit for regex, but if you insist:
^(?:36[0-5]|3[0-5][0-9]|[12][0-9][0-9]|[1-9][0-9]|[1-9])$
This is not allowing leading zeroes. If you wish to allow those, let me know.
The expression above can be shortened a little to
^(?:36[0-5]|3[0-5]\d|[12]\d{2}|[1-9]\d?)$
but I find the first solution to be a bit more readable. YMMV.
A general solution for matching the numbers from 1 to XYZ
^(?!0)(?!\d{4}$)(?![X+1-9]\d{2}$)(?!X[Y+1-9]\d$)(?!XY[Z+1-9]$)\d+$
Notes:
If any of X, Y or Z are 9 that will make X+1 etc. be 10. If that happens the regex part that would require using the 10 should be left out.
This can be extended to numbers with more or less digits following the same principles.
It does not allow left-padding 0es.
Applied to your case:
^(?!0)(?!\d{4}$)(?![4-9]\d{2}$)(?!3[7-9]\d$)(?!36[6-9]$)\d+$
Lets explain:
(?!0\d*) - does not start with 0
(?!\d{4}$) - does not have 4 digits, i.e. between 1000 and infinity
(?![4-9]\d{2}$) - it's not between 400 and 999
(?!3[7-9]\d$) - it's not between 370 and 399
(?!36[6-9]$) - it's not between 366 and 369
Test it.
^36[0-5]|(3[0-5]|[12]?[0-9])[0-9]$
^3(6[0-5]|[0-5]\d)|[12]\d\d|[1-9]\d|[1-9]$
Or if numbers like 05 can not be in input:
^3(6[0-5]|[0-5]\d)|[12]?\d?\d$
P.S.: Anyway no need of regex here. Use ToInt(), <=, >=
It really depends on your regex engine since they may not all be PCRE-style. I usually work to the lowest common denominator unless I know it will be targeting a minimum engine.
To that end, I'd just use something like:
^[1-9]|[1-9][0-9]|[1-2][0-9]{2}|3[0-5][0-9]|36[0-5]$
This will take care of (in order):
1-9.
10-99.
100-299.
300-359.
360-365.
However, unless you're absolutely required to use just a regex, I wouldn't. It's like trying to kill a fly with a thermo-nuclear warhead.
Just use the much simpler ^[0-9]{1,3}$ then use whatever language features you have to convert it to an integer and check it's between 1 and 365 inclusive:
def isValidDayOtherThanLeapYear (s):
if not s.matches ("^[0-9]{1,3}$"):
return false
n = s.toInteger()
if n < 1 or n > 365:
return false
return true
Your code will be more readable that way and I tend to rethink the use of regular expressions the second they start looking like they may be hard to read six months down the track.
This worked for me...
^[1-3][0-6]?[0-5]?$