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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
pmc086
Frequent Visitor

If statement - Expression.Error: We cannot convert the value to type text

Trying to do what I figured was a simple nested if statement in power query. Use the 'add custom column' function to build this quickly.

 

The resultant M formula is:

 

= Table.AddColumn(#"Reordered Columns", "Grade", each if [Grade Original] = "Prep" then 0 else if Text.Start([Grade Original],1) = "7" then 7 else -1)

 

Annotation 2019-10-24 103207.png

 

As you can see in the above, the first evaluation of the if statement works, as does the else if. It's falling over at the else statement which I thought would be pretty simple (I actually want to return the [Original Grade] column as the else but simplified it to debug.

 

It doesn't matter what value I try and put in the 'else' statement (tried a string, number, null), it throws the same error (below). Can't for the life of me work out what the issue is but it would be great if someone could provide some insight.

 

Annotation 2019-10-24 103208.png

 

If it matters, I'm using power query in Excel and have the latest office 365 release. I've tried searching for similar errors but can't find anything that works.

 

Thanks in advance.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

The datatype for the rows that are failing are numbers so the expression Text.Start([Grade Original],1) is returning the error. Either change the column type to text prior to adding the column or modify the formula as show below:

 

= Table.AddColumn(#"Reordered Columns", "Grade", each if [Grade Original] = "Prep" then 0 else if Text.Start(Text.From([Grade Original]),1) = "7" then 7 else -1)

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

The datatype for the rows that are failing are numbers so the expression Text.Start([Grade Original],1) is returning the error. Either change the column type to text prior to adding the column or modify the formula as show below:

 

= Table.AddColumn(#"Reordered Columns", "Grade", each if [Grade Original] = "Prep" then 0 else if Text.Start(Text.From([Grade Original]),1) = "7" then 7 else -1)

Thank you! Yes that was the solution. I hadn't even looked at that part of the formula as it was evaluating correctly for the condition I wanted. Makes total sense now that I have been enlightened.

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

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.