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
raghukumarch
Frequent Visitor

DAX Help for Conditional Column

Hi, 

Could you please help me in achieving this either in DAX or PowerQuery.
One of my column in table has values like this.

raghukumarch_0-1660667276824.png

I wanted to create another column in same table by adding "0" in the begining for the values which dose not contain "-" and other value which contains "-" should remains same as it is. 

For example column 'C_SerialNumber' have values like "311211025073516" and "916429-19-11"

I wanted to add '0' in the begining if the value DOES NOT Contain '-'. So the output will be "0311211025073516" and other values remains same.

 

Thanks

RK

1 ACCEPTED SOLUTION
AnkitKukreja
Super User
Super User

Hi! @raghukumarch 

 

Please use this M code.

 

= Table.AddColumn(#"Changed Type", "Custom", each if Text.Contains([Serial Number], "-") then [Serial Number] else "0" & Text.From ( [Serial Number]

) )

 

AnkitKukreja_0-1660675297318.png

 

If my response has successfully addressed your question or concern, I kindly request that you mark this post as resolved. Additionally, if you found my assistance helpful, a thumbs-up would be greatly appreciated.
Thanks,
Ankit Kukreja
www.linkedin.com/in/ankit-kukreja1904

View solution in original post

2 REPLIES 2
bilalrana
Resolver I
Resolver I

Dear @raghukumarch ,

I have implemented your scenario, Here's the few steps to achieve the required output.

Go to Home - > Transform Data 

 

Open the Query which contains the column C_SerialNumber.

Go to - > Add Column - > Custom Column

bilalrana_0-1660733800097.png

 

New Column Name: CheckColumn

Custom column formula: Text.Contains([C_SerialNumber],"-")

Now click on OK.

 

Now again click on  Add Column - > Custom Column

bilalrana_1-1660733800124.png

 

New Column Name: NewC_SerialNumber

Custom column formula: if [CheckColumn] = false then "0" & [C_SerialNumber] else [C_SerialNumber]

Now click on OK.

Now, check on the formula bar, if the type isn't set to text then write it in a way as below screen short and click on the tick.

bilalrana_2-1660733800127.png

 

 

Here's the desire output in a new column.

bilalrana_3-1660733800130.png

 

 

 

Please like the reply and accept it as a solution to help the Power BI Community.

AnkitKukreja
Super User
Super User

Hi! @raghukumarch 

 

Please use this M code.

 

= Table.AddColumn(#"Changed Type", "Custom", each if Text.Contains([Serial Number], "-") then [Serial Number] else "0" & Text.From ( [Serial Number]

) )

 

AnkitKukreja_0-1660675297318.png

 

If my response has successfully addressed your question or concern, I kindly request that you mark this post as resolved. Additionally, if you found my assistance helpful, a thumbs-up would be greatly appreciated.
Thanks,
Ankit Kukreja
www.linkedin.com/in/ankit-kukreja1904

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.

Top Solution Authors
Top Kudoed Authors