web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to detect if a cha...
Power Automate
Answered

How to detect if a character is an integer, or not.

(0) ShareShare
ReportReport
Posted on by 3,340

I need to check if the first character of a string is a number.  Is there an easy way to do this?  I need to do this in an expression.  Some languages have IfError() to trap an error, or some other way?

 

This is what would work for me (if the error statement was available)

 

 

if(IfError(int(substring(item()?['ProductID'],0,1)),0,1)

 

 

 

What I do NOT want to do (but would work):

 

 

if(equals(substring(item()?['ProductID'],0,1),'0'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'1'),1,
 if(equals(substring(item()?['ProductID'],0,1),'2'),1,
 if(equals(substring(item()?['ProductID'],0,1),'3'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'4'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'5'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'6'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'7'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'8'),1,
 if(equals(substring(item()?['ProductID'],0,1),'9'),1,
 0))))))))))

 

 

 

 

Thanks.

Categories:
I have the same question (0)
  • efialttes Profile Picture
    14,756 on at

    @martinav 

    Mmmm... to trap errors in power automate you need to implement a try-catch action blocks based approach.

    https://poszytek.eu/en/microsoft-en/office-365-en/powerautomate-en/try-catch-pattern-in-microsoft-flow/

     

    I don't believe it is possible to implement it by means of a pure expression based approach, but if someone else has managed to do that, more than happy to learn one more thing

     

    Now, just guessing if a lexicographical order based expression I.e. the first character is greater or equal to character '0' and less or equal to character '9' is could help you to achieve your requirements

     

    Hope this helps

  • martinav Profile Picture
    3,340 on at

    Yea, the action block method is great, but will not work in this case, because this statement is with a Select action.  I was going to be happy with the nested if(), which worked great for one condition.  However, now I have the need to detect if the first SIX characters of a string constitute a number.  That will break the lexicographical order idea.  Which was a good one, btw.  But things have become more complex.  

     

    What I am actually doing, is moving this condition statement to create another data column in a Flow Select action.   This is the statement in powerapps:

     

     

    If(StartsWith(OrderNumOB,"Kel"),2,
     StartsWith(OrderNumOB,"RMA"),3,
     StartsWith(ProductID,"HF"),5,
     StartsWith(ProductID,"DC"),5,
     StartsWith(ProductID,"NW"),5,
     IsMatch(ProductID,Match.Digit) And Mid(ProductID,2,1)="-",5,
     StartsWith(ProductID,"BT"),5,
     StartsWith(ProductID,"K") And IsNumeric(Mid(ProductID,2,1)),4,
     IsMatch(ProductID, Match.Digit & Match.Digit & Match.Digit & Match.Digit & Match.Digit & Match.Digit),4,1)

     

    This is what I have so far  in an expression.  This works perfectly at this point.  However, I need to add the last two lines from above, into the expression.

     

    if(startsWith(item()?['OrderNumOB'],'Kel'),2,
     if(startsWith(item()?['OrderNumOB'],'RMA'),3,
     if(startsWith(item()?['ProductID'],'HF'),5,
     if(startsWith(item()?['ProductID'],'DC'),5,
     if(startsWith(item()?['ProductID'],'NW'),5,
     if(startsWith(item()?['ProductID'],'BT'),5,
     if(and(
     equals(
     if(equals(substring(item()?['ProductID'],0,1),'0'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'1'),1,
     if(equals(substring(item()?['ProductID'],0,1),'2'),1,
     if(equals(substring(item()?['ProductID'],0,1),'3'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'4'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'5'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'6'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'7'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'8'),1,
     if(equals(substring(item()?['ProductID'],0,1),'9'),1,
     0)))))))))),
     1),
     equals(substring(item()?['ProductID'],1,1),'-')),5,
     
    0))))))

     

  • Verified answer
    martinav Profile Picture
    3,340 on at

    I gave up on this.  Ended up changing the View in SQL.  It has proper command statements for such things.

    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'Kel%') THEN 2 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'RMA%') THEN 3 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'DC%') THEN 5 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'NW%') THEN 5 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'BT%') THEN 5 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'DC%') THEN 5 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'HF%') THEN 5 ELSE 
    CASE WHEN (ISNUMERIC(SUBSTRING(dbo.OrderDetail.ProductID, 1, 6)) = 1) THEN 4 ELSE 
    CASE WHEN (ISNUMERIC(LEFT(dbo.OrderDetail.ProductID, 1)) = 1 AND SUBSTRING(dbo.OrderDetail.ProductID, 2, 1) = '-') THEN 5 ELSE 
    CASE WHEN (LEFT(dbo.OrderDetail.ProductID, 1) = 'K' AND ISNUMERIC(SUBSTRING(dbo.OrderDetail.ProductID, 2, 4)) = 1) 
    THEN 4 ELSE 1 END END END END END END END END END END AS OrderType

     

  • eliotcole Profile Picture
    4,363 Moderator on at

    I realise that you have a solution, but without getting into the deeper expression and just looking at your initial question, I initially thought of two methods:

    1. Use OR - Potentially simplify your method, firstly by making it easier to parse (visually) with a variable defining the first character separately 'firstyVAR'. Then using OR to simplify your IF tree (these are set as strings for simplicity's sake): or(equals(variables('firstyVAR'), '0'),equals(variables('firstyVAR'), '1'),equals(variables('firstyVAR'), '2'),equals(variables('firstyVAR'), '3'),equals(variables('firstyVAR'), '4'),equals(variables('firstyVAR'), '5'),equals(variables('firstyVAR'), '6'),equals(variables('firstyVAR'), '7'),equals(variables('firstyVAR'), '8'),equals(variables('firstyVAR'), '9'))
    2. Check the first character against an array, which is easier than you'd think, with a pre-set array of ["1","2","3","4","5","6","7","8","9","0"] and then performing a contains(variables('arrayVAR'), variables('firstyVAR')) expression which will return true or false dependent on the number. It's much cleaner.

     

    However, it was in the processing of that second one, that I realised that there's a super easy way to ascertain this:

     

    Run After

     

    So ... Here is the flow that I used to do it (triggerBody()['text'] is where your initial string would be):
    ignore the comment in 'intYnVAR'ignore the comment in 'intYnVAR'

     

    In order for this to work, everything depends on what happens when defining the integer variable, 'firstyVAR'. So when using the int() conversion function on the first letter of the string:

    int(substring(triggerBody()['text'], 0, 1))

    That is were you will branch:

    1. For success, if the substring managed to convert into an integer, winner winner chicken dinner.
    2. For failure, if it failed, then this is where you process the failure.

    When managing failure states, I always find it simpler to set a boolean, which we can use to move on from there. But you can perform whatever you like, obviously.

     

    The key here is clicking the dot menu on the failure branch and setting the 'Configure run after' value:

    Configure run afterConfigure run after

     

     

     

    Here you ensure that you set it to "has failed".

    has failedhas failed

     

     

     

    Then when the int() runs, if it fails, then the failure branch will run!

     

    You're not done yet, though, as your next entry has to run after EITHER of the previous branches ran. So you need to repeat the action on the unifying connector. You must go in twice to get it right, though:

    1. Once to set it to run after any eventuality in the success branch. CLICK 'Done'
    2. Once more to set it to run after any eventuality in the failure branch. CLICK 'Done'

    Once more with feelingOnce more with feeling

     

     

     

    That's it!

     

    Here's some results for proof:

    no integerno integer

     

    integerinteger

     

     

    NB - Be careful with 'Run after' as it has a knack of removing itself when you amend affecting parts of the flow. So always check your Run Afters if you have them! (or try to get them in a scope)

  • Clifferton Profile Picture
    94 on at

    If the 'firstyVAR' block fails, does the flow show a failure in the run history, or is that failure mitigated by the fact that there's a block set to run after a fail?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 501 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 323 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard