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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
veronica_zappp
Frequent Visitor

Create a column with new text based on character length of another column

Hello,

 

I'm having trouble finding an efficient way of setting the following command, and as new Power BI user with limited M and DAX experience, not sure how to best go about this.

 

In short, I'd like to create a new column of string values, based on length of string in another column.

More specifically, in my original column (LocationTag), I have acronyms for locations.

The locations are either 3 or 4+ length character acronyms.

 

I'd like to have a new column with information based on the length of the string in my original column.

  • If the location is 3 characters, its string in the new column will be "Home"
  • If the location is 4+ characters, its string in the new column will be "Away".
  • There are never less than 3 character tags

 

LocationTagNew column
ABCDAway
BCDEFAway
OSSHome
OMAHome

 

Manual replace values in the GUI is not an option as I have hundreds of different types of location tags. I'd also like to avoid searching/typing specific text and assigning it a new text (in the new column). I need to create the new column based on length of text string in the original column (LocationTag).... does Replace.Value based on character string length exist?

 

My thinking is something related to this:

if string is 3 characters in LocationTag 

then, "Home"

if string is equal to or more than 4 characters

then, "Away"

 

Thank you very much for your help!

1 ACCEPTED SOLUTION
v-henryk-mstf
Community Support
Community Support

Hi @veronica_zappp ,

 

The function used is the same as the one provided by @rsbin , which is to return the length of a given string. Need a judgment condition has been added to this.

result =
IF (
    LEN ( 'Table'[LocationTag] ) < 3,
    "error",
    IF (
        LEN ( 'Table'[LocationTag] ) = 3,
        "home",
        IF ( LEN ( 'Table'[LocationTag] ) > 3, "away" )
    )
)

vhenrykmstf_0-1661247426402.png

 

If the problem is still not resolved, please provide detailed error information and let me know immediately. Looking forward to your reply.


Best Regards,
Henry


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

2 REPLIES 2
v-henryk-mstf
Community Support
Community Support

Hi @veronica_zappp ,

 

The function used is the same as the one provided by @rsbin , which is to return the length of a given string. Need a judgment condition has been added to this.

result =
IF (
    LEN ( 'Table'[LocationTag] ) < 3,
    "error",
    IF (
        LEN ( 'Table'[LocationTag] ) = 3,
        "home",
        IF ( LEN ( 'Table'[LocationTag] ) > 3, "away" )
    )
)

vhenrykmstf_0-1661247426402.png

 

If the problem is still not resolved, please provide detailed error information and let me know immediately. Looking forward to your reply.


Best Regards,
Henry


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

rsbin
Super User
Super User

@veronica_zappp ,

Using DAX, create a new Calculated Column.  You are on the right track with your IF statement, but 

I now prefer using the SWITCH function instead of the usual IF Statement.

You just need to be made aware of the LEN function.

NewColumn = SWITCH(
                TRUE(),
                LEN(YourTable[LocationTag]) = 3, "Home",
                "Away")

 This is my source for the comprehensive list of DAX functions:
https://docs.microsoft.com/en-us/dax/

Hope this helps.

Regards,

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.