Sometimes you need to create an ordinal number for use in an email or similar situation. Power Apps does not have any support for this type of number formatting but it can be created quite easily with an expression.
The rules are as follows:
- st is used for numbers ending in 1.
- nd is used for numbers ending in 2
- rd is used for numbers ending in 3
- all other numbers end in th except…
- Numbers ending in 11, 12, or 13 use th.
In Power Automate you can generate an ordinal number with the following expression:
if(endsWith(string(outputs('NumbeToConvert')),'12'),formatNumber(outputs('NumbeToConvert'), '#th'), if(endsWith(string(outputs('NumbeToConvert')),'13'),formatNumber(outputs('NumbeToConvert'), '#th'), if(endsWith(string(outputs('NumbeToConvert')),'1'),formatNumber(outputs('NumbeToConvert'), '#st'), if(endsWith(string(outputs('NumbeToConvert')),'2'),formatNumber(outputs('NumbeToConvert'), '#nd'), if(endsWith(string(outputs('NumbeToConvert')),'3'),formatNumber(outputs('NumbeToConvert'), '#rd'), formatNumber(outputs('NumbeToConvert'), '#th')))))))
Where outputs(‘NumbeToConvert’) is the value you want to replace. An example flow using the above:
The result is obviously 102nd:
The flow simply uses the endswith function to determine the correct ordinal reference.
Hope this helps.
Jay says
This will output 11st instead of 11th because 11 ends with 1 (your 3rd code line and first rule).
I did it like this:
if(and(greater(outputs(‘NumbeToConvert’),3),less(outputs(‘NumbeToConvert’),21)),formatNumber(outputs(‘NumbeToConvert’), ‘#th’),
if(equals(outputs(‘NumbeToConvert’),1),formatNumber(outputs(‘NumbeToConvert’), ‘#st’),
if(equals(outputs(‘NumbeToConvert’),2),formatNumber(outputs(‘NumbeToConvert’), ‘#nd’),
if(equals(outputs(‘NumbeToConvert’),3),formatNumber(outputs(‘NumbeToConvert’), ‘#rd’),
formatNumber(outputs(‘NumbeToConvert’), ‘#th’)))))
Jay says
oops! I mean:
if(and(greater(variables(‘InfringementCount’),3),less(variables(‘InfringementCount’),21)),formatNumber(variables(‘InfringementCount’), ‘#th’),
if(endsWith(string(variables(‘InfringementCount’)),’1′),formatNumber(variables(‘InfringementCount’), ‘#st’),
if(endsWith(string(variables(‘InfringementCount’)),’2′),formatNumber(variables(‘InfringementCount’), ‘#nd’),
if(endsWith(string(variables(‘InfringementCount’)),’3′),formatNumber(variables(‘InfringementCount’), ‘#rd’),
formatNumber(variables(‘InfringementCount’), ‘#th’)))))
jc says
Hi I cant get this code to work