Forum Discussion
Remove last 2 characters if number
Hi experts!
I have a column with text and numbers.
Now I would like to remove the last 2 characters, if it is a number.
From "1A" to "A".
But how with dax?
In Power BI, you can achieve this using DAX (Data Analysis Expressions). You can create a new calculated column or measure depending on your requirement. Here's how you can do it using a calculated column:
- Open your Power BI report.
- Go to the table where your column with text and numbers is located.
- Click on "Modeling" at the top.
- Click on "New Column" in the ribbon.
Now, you can use the following DAX expression to create a new column:
NewColumn =
IF(
ISNUMBER(VALUE(LEFT(YourTableName[YourColumnName], LEN(YourTableName[YourColumnName])-2))),
LEFT(YourTableName[YourColumnName], LEN(YourTableName[YourColumnName])-2),
YourTableName[YourColumnName]
)Replace YourTableName with the name of your table and YourColumnName with the name of the column containing the text and numbers.
This DAX expression does the following:
- LEFT(YourTableName[YourColumnName], LEN(YourTableName[YourColumnName])-2): This part takes the left part of the string, excluding the last 2 characters.
- VALUE(): It converts the resulting string into a numeric value.
- ISNUMBER(): Checks if the value obtained is numeric.
- If the last two characters are numeric, it removes them. If not, it keeps the original value.
After you've entered the DAX expression, press Enter. It will create a new column in your table with the desired result.
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.
- Anonymous2 years ago
Hi joshua1990
You can use M function in Power Query.
Here is my trying.
My sample:
1. Split Column by Position
2. add a Custom Column
Text.Select([Value.2], {"a".."z", "A".."Z"})3. Select the two columns that need to be merged
4. remove Value.2 Column
5. Result
Here is the whole M function in Advanced Editor:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WioiIdAmOUorViVYK9vYwNgGzHH28vQwdwUw/72AfIzDL0MjYxM0FwjQ2NDIxhgibGJtCBI1NTN2BQrEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type text}}), #"Split Column by Position" = Table.SplitColumn(#"Changed Type", "Value", Splitter.SplitTextByPositions({0, 2}, true), {"Value.1", "Value.2"}), #"Added Custom" = Table.AddColumn(#"Split Column by Position", "Custom", each Text.Select([Value.2], {"a".."z", "A".."Z"})), #"Merged Columns" = Table.CombineColumns(#"Added Custom",{"Value.1", "Custom"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"), #"Removed Columns" = Table.RemoveColumns(#"Merged Columns",{"Value.2"}) in #"Removed Columns"Best Regards,
Yulia XuIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi,
If you don't wan to make changes in M like Anonymous suggested or use a column. Here is a measure:Measure 20 =var _text = MAX('Table (28)'[Column1])var _base = LEFT(_text,LEN(_text)-2)var _last = RIGHT(_text,1)var _secondLast =LEFT(RIGHT(_text,2),1)var _test1 = IF(ISERROR(INT(_last)),_last,BLANK())var _test2 = IF(ISERROR(INT(_secondLast)),_secondLast,BLANK())RETURN
_base&_test1&_test2
End result:
I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!
My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/
3 Replies
- AnonymousNot applicable
Hi joshua1990
You can use M function in Power Query.
Here is my trying.
My sample:
1. Split Column by Position
2. add a Custom Column
Text.Select([Value.2], {"a".."z", "A".."Z"})3. Select the two columns that need to be merged
4. remove Value.2 Column
5. Result
Here is the whole M function in Advanced Editor:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WioiIdAmOUorViVYK9vYwNgGzHH28vQwdwUw/72AfIzDL0MjYxM0FwjQ2NDIxhgibGJtCBI1NTN2BQrEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type text}}), #"Split Column by Position" = Table.SplitColumn(#"Changed Type", "Value", Splitter.SplitTextByPositions({0, 2}, true), {"Value.1", "Value.2"}), #"Added Custom" = Table.AddColumn(#"Split Column by Position", "Custom", each Text.Select([Value.2], {"a".."z", "A".."Z"})), #"Merged Columns" = Table.CombineColumns(#"Added Custom",{"Value.1", "Custom"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"), #"Removed Columns" = Table.RemoveColumns(#"Merged Columns",{"Value.2"}) in #"Removed Columns"Best Regards,
Yulia XuIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- 123abc
Community Champion
In Power BI, you can achieve this using DAX (Data Analysis Expressions). You can create a new calculated column or measure depending on your requirement. Here's how you can do it using a calculated column:
- Open your Power BI report.
- Go to the table where your column with text and numbers is located.
- Click on "Modeling" at the top.
- Click on "New Column" in the ribbon.
Now, you can use the following DAX expression to create a new column:
NewColumn =
IF(
ISNUMBER(VALUE(LEFT(YourTableName[YourColumnName], LEN(YourTableName[YourColumnName])-2))),
LEFT(YourTableName[YourColumnName], LEN(YourTableName[YourColumnName])-2),
YourTableName[YourColumnName]
)Replace YourTableName with the name of your table and YourColumnName with the name of the column containing the text and numbers.
This DAX expression does the following:
- LEFT(YourTableName[YourColumnName], LEN(YourTableName[YourColumnName])-2): This part takes the left part of the string, excluding the last 2 characters.
- VALUE(): It converts the resulting string into a numeric value.
- ISNUMBER(): Checks if the value obtained is numeric.
- If the last two characters are numeric, it removes them. If not, it keeps the original value.
After you've entered the DAX expression, press Enter. It will create a new column in your table with the desired result.
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.
- ValtteriN
Community Champion
Hi,
If you don't wan to make changes in M like Anonymous suggested or use a column. Here is a measure:Measure 20 =var _text = MAX('Table (28)'[Column1])var _base = LEFT(_text,LEN(_text)-2)var _last = RIGHT(_text,1)var _secondLast =LEFT(RIGHT(_text,2),1)var _test1 = IF(ISERROR(INT(_last)),_last,BLANK())var _test2 = IF(ISERROR(INT(_secondLast)),_secondLast,BLANK())RETURN
_base&_test1&_test2
End result:
I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!
My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/