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
NPandelos
New Member

Help Using If Then

Good morning,

I'm new to using power query. I'm trying to create an if then statement in that will look at the first character in the column and if it matches a set of values it returns the cell value, if not it dropps off the last three digits of the cell. The code is giving me an error at the "then [Root.1]" I dont know why. Below is an example and the code.

      From             -->           To

3-000795-00       -->     3-000795

5-009870-01       -->     5-009870

AP-TESTPART      -->    AP-TESTPART

 

if (Text.StartsWith([Root.1],"A", "C", "F", "I", "L", "P", "R","S")
then [Root.1]
else Text.End([Root.1], Text.Length([Root.1])-3))

1 ACCEPTED SOLUTION
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Use this

= if Text.Contains("ACFILPRS",Text.Start([Root.1],1)) then [Root.1] 
else if Text.Start(Text.End([Root.1],3),1) = "-" then Text.RemoveRange([Root.1],Text.Length([Root.1])-3,3) 
else [Root.1]

Actually, seeing your construction, first if can be totally removed. Do you have any data which will violate this?

= if Text.Start(Text.End([Root.1],3),1) = "-" then Text.RemoveRange([Root.1],Text.Length([Root.1])-3,3) 
else [Root.1]

 

View solution in original post

4 REPLIES 4
NPandelos
New Member

Awesome that worked. Thanks for your help.

Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Use this

= if Text.Contains("ACFILPRS",Text.Start([Root.1],1)) then [Root.1]
else Text.RemoveRange([Root.1],Text.Length([Root.1])-3,3)

That worked good except that after I ran it i found another variable I need to get written in. I'm working with some rather large data sets. 90% of the remaining data that is a number value has a - 3 characters in from the end. I thought I could add a second if statement but it didnt work. Here is the code I thought would work.

 

if Text.Contains("ACFILPRS",Text.Start([Root.1],1)) then [Root.1] else
if Text.Contains("-",Text.End([Root.1]),3) then
Text.RemoveRange([Root.1],Text.Length([Root.1])-3,3) else
[Root.1] 

Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Use this

= if Text.Contains("ACFILPRS",Text.Start([Root.1],1)) then [Root.1] 
else if Text.Start(Text.End([Root.1],3),1) = "-" then Text.RemoveRange([Root.1],Text.Length([Root.1])-3,3) 
else [Root.1]

Actually, seeing your construction, first if can be totally removed. Do you have any data which will violate this?

= if Text.Start(Text.End([Root.1],3),1) = "-" then Text.RemoveRange([Root.1],Text.Length([Root.1])-3,3) 
else [Root.1]

 

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.