cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Swordfish
New Member

The second argument to the 'Replace' function must be greater than or equal to 1

Hi all, when enabling Formula-level error management I'm getting the error "The second argument to the 'Replace' function must be greater than or equal to 1"

 

The below formula is what it is referring to which appears to be working as expected. I didn't create the app originally but am maintaining it and I wanted to enable the error management to handle NULL values. Any ideas?

 

Concatenate(

    "Something went wrong with a previous attempt to save the report. Click below to go back to the report and check the following sections for completeness: ",

    Replace(lclMissingListItemNames,Len(lclMissingListItemNames)-1,Len(lclMissingListItemNames),".")

)

 

 

TIA

 

Paul

1 ACCEPTED SOLUTION

Accepted Solutions
CarlosFigueira
Power Apps
Power Apps

As the error message suggests, the second parameter to the Replace function must be at least 1, so if the length of the lclMissingListItemNames variable is less than 2 (or it is blank, in which case it would have a length of 0), then you will see this error. With 'Formula-level error management' disabled you would get a blank value instead of the error. There are a few ways you can solve this:

  • "Capture" the error with the IfError function, and replace it with something else (such as Blank(), which was the previous result):
    • IfError(Replace(lclMissingListItemNames,Len(lclMissingListItemNames)-1,Len(lclMissingListItemNames),"."), Blank())
  • Check if the variable is large enough to do the replacement:
    • If(Len(lclMissingListItemNames) > 1, Replace(lclMissingListItemNames, Len(lclMissingListItemNames)-1, Len(lclMissingListItemNames),"."), Blank())

Notice that you can simplify those expressions with the With function so that you won't need to repeat the same expression multiple times:

  • IfError(With({ l: Len(lclMissingListItemNames) }, Replace(lclMissingListItemNames, l - 1, l, "."), Blank()))
  • With({ l: Len(lclMissingListItemNames) }, If(l > 1, Replace(lclMissingListItemNames, l - 1, l, "."), Blank()))

Hope this helps!

View solution in original post

2 REPLIES 2
CarlosFigueira
Power Apps
Power Apps

As the error message suggests, the second parameter to the Replace function must be at least 1, so if the length of the lclMissingListItemNames variable is less than 2 (or it is blank, in which case it would have a length of 0), then you will see this error. With 'Formula-level error management' disabled you would get a blank value instead of the error. There are a few ways you can solve this:

  • "Capture" the error with the IfError function, and replace it with something else (such as Blank(), which was the previous result):
    • IfError(Replace(lclMissingListItemNames,Len(lclMissingListItemNames)-1,Len(lclMissingListItemNames),"."), Blank())
  • Check if the variable is large enough to do the replacement:
    • If(Len(lclMissingListItemNames) > 1, Replace(lclMissingListItemNames, Len(lclMissingListItemNames)-1, Len(lclMissingListItemNames),"."), Blank())

Notice that you can simplify those expressions with the With function so that you won't need to repeat the same expression multiple times:

  • IfError(With({ l: Len(lclMissingListItemNames) }, Replace(lclMissingListItemNames, l - 1, l, "."), Blank()))
  • With({ l: Len(lclMissingListItemNames) }, If(l > 1, Replace(lclMissingListItemNames, l - 1, l, "."), Blank()))

Hope this helps!

Thanks Carlos solution helps tremendously and works perfectly.

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Carousel_PP_768x460_Wave2 (1).png

2022 Release Wave 2 Plan

Power Platform release plan for the 2022 release wave 2 describes all new features releasing from October 2022 through March 2023.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Top Kudoed Authors
Users online (3,265)