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

How to handle error for "If else statement"?

Hi all,

 

I'm adding a custom column where values are based on conditional statement.

 

 

= Table.AddColumn( #"Added Index", 
        "Custom", 
                 each 
                   if Text.Contains(#"Added Index"{[Index] - 1}[INPUT],"ABC")  then
                     "NEW TEXT"
                   else
                    [INPUT]
                )

 

The answer in new column is correct, but in first row appears "Error" below since is doing {[Index] -1} for first element

 

Expression.Error: The index cannot be negative.
Details:
    Value=[Table]
    Index=-1

 

 

I added a "try otherwise.." before the "if" and output still shows Error for first element. How to handle this error? Thanks

1 ACCEPTED SOLUTION
v-yueyunzh-msft
Community Support
Community Support

Hi , @cgkas 

You just need to update the "Added Index " steps to from 1 to ..., like this:(not start from 0)

vyueyunzhmsft_0-1673315605600.png

 

 

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

4 REPLIES 4
v-yueyunzh-msft
Community Support
Community Support

Hi , @cgkas 

You just need to update the "Added Index " steps to from 1 to ..., like this:(not start from 0)

vyueyunzhmsft_0-1673315605600.png

 

 

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

 

 

Thank you @v-yueyunzh-msft. Only changing from 0 to 1, removed the error.

Greg_Deckler
Super User
Super User

@cgkas You could do a Replace errors step and plug in whatever you did for your otherwise.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks for your answer. I was wondering if is possible to handle the error withour adding extra step.

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.