Forum Discussion
Adding Text to data column based on text length
- Anonymous1 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 BIuse 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.
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.
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!