Forum Discussion

QELucas's avatar
QELucas
New Member
1 year ago
Solved

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 woul...
  • Anonymous's avatar
    Anonymous
    1 year ago

    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.


    Use M code
    In Power Query:
     

    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:

    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.