Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
joshua1990
Post Prodigy
Post Prodigy

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?

3 ACCEPTED SOLUTIONS
123abc
Community Champion
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:

  1. Open your Power BI report.
  2. Go to the table where your column with text and numbers is located.
  3. Click on "Modeling" at the top.
  4. 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.

View solution in original post

v-xuxinyi-msft
Community Support
Community Support

Hi @joshua1990 

 

You can use M function in Power Query.

 

Here is my trying.

 

My sample:

vxuxinyimsft_4-1710830411624.png

 

1. Split Column by Position

vxuxinyimsft_0-1710829342544.png

 

vxuxinyimsft_5-1710831449620.png

 

2. add a Custom Column

 

Text.Select([Value.2], {"a".."z", "A".."Z"})

 

vxuxinyimsft_1-1710829902345.png

 

3. Select the two columns that need to be merged

vxuxinyimsft_2-1710830036760.png

 

4. remove Value.2 Column

 

5. Result

vxuxinyimsft_3-1710830351742.png

 

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 Xu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

ValtteriN
Super User
Super User

Hi,
If you don't wan to make changes in M like @v-xuxinyi-msft  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:
ValtteriN_0-1710833196870.png

 

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/





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
ValtteriN
Super User
Super User

Hi,
If you don't wan to make changes in M like @v-xuxinyi-msft  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:
ValtteriN_0-1710833196870.png

 

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/





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




v-xuxinyi-msft
Community Support
Community Support

Hi @joshua1990 

 

You can use M function in Power Query.

 

Here is my trying.

 

My sample:

vxuxinyimsft_4-1710830411624.png

 

1. Split Column by Position

vxuxinyimsft_0-1710829342544.png

 

vxuxinyimsft_5-1710831449620.png

 

2. add a Custom Column

 

Text.Select([Value.2], {"a".."z", "A".."Z"})

 

vxuxinyimsft_1-1710829902345.png

 

3. Select the two columns that need to be merged

vxuxinyimsft_2-1710830036760.png

 

4. remove Value.2 Column

 

5. Result

vxuxinyimsft_3-1710830351742.png

 

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 Xu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

123abc
Community Champion
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:

  1. Open your Power BI report.
  2. Go to the table where your column with text and numbers is located.
  3. Click on "Modeling" at the top.
  4. 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.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.