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.
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?
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:
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'
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:
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 after
Here you ensure that you set it to "has failed".
has 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:
Once more with feeling
That's it!
Here's some results for proof:
no integer
integer
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)
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
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))))))
Mmmm... to trap errors in power automate you need to implement a try-catch action blocks based approach.
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
stampcoin
97
Michael E. Gernaey
70
Super User 2025 Season 1
David_MA
48
Super User 2025 Season 1