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
agd50
Helper V
Helper V

M Language Conditional Statements - How To

I'm looking to add a prefix to an existing column but only if it doesn't already have the prefix/conditional.

Any suggestions for how to do this?

2 ACCEPTED SOLUTIONS
BA_Pete
Super User
Super User

Hi @agd50 ,

 

Difficult to give an exact answer without seeing an example of your data, but the basic conditional structure in Power Query M is as follows:

if <this thing is true>
then <do this thing>
else if <this other thing is true>
then <do this other thing>
else <do this escape action>

 

The "<do this thing>" part can be to run a function or to declare a variable etc. Therefore, a new custom QUERY STEP for your purpose might look something like this:

Table.ReplaceValue(
    PreviousStepName,
    each [ColumnToCheck],
    each if Text.StartsWith([ColumnToCheck], "Prefix")
        then [ColumnToCheck]
        else "Prefix " & [ColumnToCheck],
    Replacer.ReplaceText,
    {"ColumnToCheck"}
)

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

v-junyant-msft
Community Support
Community Support

Hi @agd50 ,

I hope this will help you.
Suppose, I want to prefix all rows in column1 with "1. Here I chose to create a new column in order to allow for a clearer comparison.

vjunyantmsft_0-1704954904751.png

Here is the content in the advanced editor:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtRzVIrViVZyApPOYNJQzwVMuyrFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "New", each if Text.Start([Column1], 2) = "1." then [Column1] else "1."&[Column1])
in
    #"Added Custom"

 

The final output is like below:

vjunyantmsft_1-1704954969479.png

Best Regards,
Dino Tao
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-junyant-msft
Community Support
Community Support

Hi @agd50 ,

I hope this will help you.
Suppose, I want to prefix all rows in column1 with "1. Here I chose to create a new column in order to allow for a clearer comparison.

vjunyantmsft_0-1704954904751.png

Here is the content in the advanced editor:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtRzVIrViVZyApPOYNJQzwVMuyrFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "New", each if Text.Start([Column1], 2) = "1." then [Column1] else "1."&[Column1])
in
    #"Added Custom"

 

The final output is like below:

vjunyantmsft_1-1704954969479.png

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

BA_Pete
Super User
Super User

Hi @agd50 ,

 

Difficult to give an exact answer without seeing an example of your data, but the basic conditional structure in Power Query M is as follows:

if <this thing is true>
then <do this thing>
else if <this other thing is true>
then <do this other thing>
else <do this escape action>

 

The "<do this thing>" part can be to run a function or to declare a variable etc. Therefore, a new custom QUERY STEP for your purpose might look something like this:

Table.ReplaceValue(
    PreviousStepName,
    each [ColumnToCheck],
    each if Text.StartsWith([ColumnToCheck], "Prefix")
        then [ColumnToCheck]
        else "Prefix " & [ColumnToCheck],
    Replacer.ReplaceText,
    {"ColumnToCheck"}
)

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




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