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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
alexpork
Frequent Visitor

IF conditional m language

I'm currently trying to do a conditional for a column called Item Text. If any rows in that column start with the characters "ELE", then remove the first 24 characters, else do nothing. So far I have this but I'm not sure if im correct syntax wise or what to put in the else statement. 

 

let
Custom1 = if Text.StartsWith([Item Text], "ELE") then Splitter.SplitTextByPositions({24}, {"Item Text"}) else
in
Custom1

2 ACCEPTED SOLUTIONS
Fowmy
Super User
Super User

@alexpork 


Check the following code on a blank query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcvVxLSxPLSqpLM3KSE9LKU6sSC5LysrLzswH85VidTDUgMUcHR0J6nNKAipBNiHYyNDIEJeGWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item Text" = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if Text.StartsWith([Item Text], "ELE") then 
Text.Middle([Item Text], 25, Text.Length([Item Text])) else "")
in
    #"Added Custom"



Fowmy_0-1604218378344.png

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

AlB
Community Champion
Community Champion

Hi @alexpork 

 

Starting from the table where you have the Item text column, create a custom column by:

 1.  Selecting Addcolumn --> Custom column in the menu of the Query editor (https://docs.microsoft.com/en-us/power-bi/create-reports/desktop-add-custom-column)

 2. Enter this code for the column when prompted:

= if Text.StartsWith([Item Text], "ELE") then Text.Middle([Item Text], 24, Text.Length([Item Text])-24 )) else [Item Text]

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

View solution in original post

5 REPLIES 5
AlB
Community Champion
Community Champion

Hi @alexpork 

 

Starting from the table where you have the Item text column, create a custom column by:

 1.  Selecting Addcolumn --> Custom column in the menu of the Query editor (https://docs.microsoft.com/en-us/power-bi/create-reports/desktop-add-custom-column)

 2. Enter this code for the column when prompted:

= if Text.StartsWith([Item Text], "ELE") then Text.Middle([Item Text], 24, Text.Length([Item Text])-24 )) else [Item Text]

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

alexpork
Frequent Visitor

@AlB @Fowmy 

 

Here's a better look into what I have so far based on your reply! I think the answer is close. No syntax errors were detected. However, I receive an error that says the field 'Item Text' of the record wasn't found.

 

I'm thinking that the column names wasn't able to be find somehow but I promoted the headers in the previous step so I'm not sure if this is why the error is occuring!

AlB
Community Champion
Community Champion

@alexpork 

That means that the step test_Sheet doesn't have a column named [Item text].

Why are you adding the custom column to test_Sheet and not to #"Changed Type", the immediately previous step??

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

alexpork
Frequent Visitor

@AlB 

 

I am still new to M language and still adjusting to the syntax. Once I added your suggestion in, that fixed the issue. Thank you so much!

Fowmy
Super User
Super User

@alexpork 


Check the following code on a blank query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcvVxLSxPLSqpLM3KSE9LKU6sSC5LysrLzswH85VidTDUgMUcHR0J6nNKAipBNiHYyNDIEJeGWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item Text" = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each if Text.StartsWith([Item Text], "ELE") then 
Text.Middle([Item Text], 25, Text.Length([Item Text])) else "")
in
    #"Added Custom"



Fowmy_0-1604218378344.png

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.