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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
QELucas
New Member

Adding Text to data column based on text length

Hello,

I'm still fairly new to Power BI and I'm trying to add a new column thats based on the Tracking column.

Basically I'm not sure how to properly code the following:

If the length is =6 I would like to add "A1S1"

If the length is =8 add "S1"

If the length is =10 keep as is

 

I normally do this in an offline Excel doc which I use a simple If/Then and Concat but with Power BI having a different syntax I'm not sure how to proceed.

 

I should also mention that the excel doc is pulled from our ERP system which makes it not possible to add this formula to the excel doc.

 

Example Excel Doc.JPG

Thank you!

Lucas

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi,Greg_Deckler .thanks for your concern about this issue.

Your answer is excellent!
And I would like to share some additional solutions below.
Hello,@QELucas .I am glad to help you.
I hope you find my test below helpful, I have uploaded my test file so you can download it and see the steps in detail.
Here is my test data.

vjtianmsft_0-1729562729635.png
Use M code
In Power Query:
 

vjtianmsft_1-1729562749578.png

if Text.Length([Tracking])=6 then [Tracking] &"A1S1"
else if Text.Length([Tracking])=8 then [Tracking] &"S1"
else if Text.Length([Tracking])=10 then [Tracking]
else [Tracking]&"Error"

Use DAX code
In Power BI

use IF function or SWITCH function:

Create a calculate column:

vjtianmsft_2-1729562857207.png

vjtianmsft_3-1729562864231.png

C_RFT Tracking = 
VAR LengthOfTarcking = LEN([Tracking])
RETURN
    IF(
        LengthOfTarcking = 6, 
        [Tracking] & "A1S1",
        IF(
            LengthOfTarcking = 8,
            [Tracking] & "S1",
            IF(
                LengthOfTarcking = 10,
                [Tracking],
                [Tracking] & "Error"
            )
        )
    )

 

C_switch Tracking = 
VAR LengthOfTarcking = LEN([Tracking])
RETURN
SWITCH(TRUE(),
LengthOfTarcking =6,  [Tracking] & "A1S1",
LengthOfTarcking = 8,[Tracking] & "S1",
LengthOfTarcking = 10,[Tracking],
[Tracking] & "Error"
)

 

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.

Best Regards,

Carson Jian,

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

3 REPLIES 3
Anonymous
Not applicable

Hi,Greg_Deckler .thanks for your concern about this issue.

Your answer is excellent!
And I would like to share some additional solutions below.
Hello,@QELucas .I am glad to help you.
I hope you find my test below helpful, I have uploaded my test file so you can download it and see the steps in detail.
Here is my test data.

vjtianmsft_0-1729562729635.png
Use M code
In Power Query:
 

vjtianmsft_1-1729562749578.png

if Text.Length([Tracking])=6 then [Tracking] &"A1S1"
else if Text.Length([Tracking])=8 then [Tracking] &"S1"
else if Text.Length([Tracking])=10 then [Tracking]
else [Tracking]&"Error"

Use DAX code
In Power BI

use IF function or SWITCH function:

Create a calculate column:

vjtianmsft_2-1729562857207.png

vjtianmsft_3-1729562864231.png

C_RFT Tracking = 
VAR LengthOfTarcking = LEN([Tracking])
RETURN
    IF(
        LengthOfTarcking = 6, 
        [Tracking] & "A1S1",
        IF(
            LengthOfTarcking = 8,
            [Tracking] & "S1",
            IF(
                LengthOfTarcking = 10,
                [Tracking],
                [Tracking] & "Error"
            )
        )
    )

 

C_switch Tracking = 
VAR LengthOfTarcking = LEN([Tracking])
RETURN
SWITCH(TRUE(),
LengthOfTarcking =6,  [Tracking] & "A1S1",
LengthOfTarcking = 8,[Tracking] & "S1",
LengthOfTarcking = 10,[Tracking],
[Tracking] & "Error"
)

 

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.

Best Regards,

Carson Jian,

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

Greg_Deckler
Community Champion
Community Champion

@QELucas Why is this posted like 6 or 7 times? 

 

Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Apologies for the multiple posts, not sure what happened there. 

 

I was able to use the Excel to DAX Translation - Microsoft Fabric Community with great success and my column is populating correcty.

 

Thank you for the resource! 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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