Tuesday, 20 October 2020

How to do Proper Case in flow

06:24 Posted by Benitez Here , , , , , No comments
Earlier this year I faced a challenge with flow in Power Automate - I needed to make a value that was in uppercase and format it as proper case.

What is Proper Case? 

Proper Case is 

...any text that is written with each of the first letters of every word being capitalized.

For example

Hello World Today

where in bold is the first letter of the word displayed as uppercase

What's supported today with flow in Power Automate?

Proper Case is a function supported today in Canvas apps.

However with flows in Power Automate, the function does not yet exist. You can vote for the function to be made available from this Power Automate Idea.

Don't worry, in this #WTF episode I share with you how to do Proper Case with flow in Power Automate using some actions until the function is supported in flow. My brain came up with the idea to first make the value all in lowercase and then make the first letter of each word as as uppercase.

Let's flow

The following is what my flow in Power Automate looks like.

The trigger

For the purpose of learning in this WTF episode I have used the trigger of Manually trigger a flow with a text input. The text input will be where a value in uppercase will be entered and the output therefore will be used in the next action.

Compose - Split into array

The value to be transformed into Proper Case will be in uppercase - HELLO WORLD TODAY as an example. As mentioned earlier, the value will be transformed to lowercase in order to make the first letter uppercase. The first action is Compose and I am referencing a couple of functions
  1. Using the tolower function transforms the value from uppercase to lowercase - note this can be skipped if your value is already as lowercase
  2. Then by using an additional function of split where we split by space, an array will be formed so that we can loop through the words to make the first letter as uppercase.
The following is my expression used in the compose action.

split(tolower(triggerBody()['text']), ' ')

The following is the output of the compose action where the words in the string value is all in lowercase.

Apply to each

Since the output from the previous action is an array the apply to each action can be used to loop through the words.

Compose action - Uppercase first letter

In this action I am referencing multiple functions in a single expression. In my vlog I broke it down to explain the purpose of the different functions in the expression. What I did forget to mention though in my vlog is that I came across this expression from a Power Automate community forum post. Credit to the author of the post and not to me for this part.

The breakdown is in the following screenshot.

How to make the first letter as uppercase (#1)

To make the first letter as uppercase, two functions are used.
  1. toupper will make the value as uppercase
  2. by wrapping it with first function, only the first character in the value will be capitalized
The following is my expression where I'm referencing the item from the Apply to each action.

first(toupper(item()))

The output is the following where the h from hello is capitalized.

How to identify the length of the word (#2)

The purpose of this function is to know how many characters make up the word for the next two functions.

The following is my expression where I'm referencing the item from the Apply to each action.

length(item())

The output is the following where 5 is the number of characters in hello.

How to identify the difference of characters from the first letter of the word (#3)

The purpose of this function is to be able subtract the first letter, which is 1, from 5 since it is the number of characters in hello. This will make sense in the next two functions.

The following is my expression where I'm referencing the item from the Apply to each action.

sub(length(item()), 1)

The output is the following where 4 is the remaining difference when you subtract 1 from 5 (which is the number of characters in hello).

How to retrieve the remaining characters (#4)

A substring function is then wrapped around the previous expression so that from position 1 of the string value, h, the remaining characters in the string is retrieved.

The following is my expression where I'm referencing the previous two functions (2 & 3).

substring(item(), 1, sub(length(item()), 1))

The output is the following where ello is the characters retrieved.

Wrapping it altogether

Using the concat function will then combine all of the functions together to form the single expression.

The following is my expression.

concat(first(toupper(item())), substring(item(), 1, sub(length(item()), 1)))

The output is the following where the uppercase letter H is combined with ello so that the value is in Proper Case.

Join Array

I'm going to pause here as there's something I need to share and explain, Pieter's method.

Pieter's method

Pieter's method was something I didn't know of until my friend John Liu showed me. In the past whenever I've had to reference an array downstream in a flow where the array is formed from an apply to each, I've used the Initialize Variable and Set Variable actions.


Pieter's method removes the need for these two actions by referencing the output in the Apply to each in a compose action outside of the Apply to each action. In my scenario I can reference the output from the Compose action.

If you try reference the output through Dynamic Content, you won't see it.


What I did was create another Compose action within the apply to each action and referenced the output from the previous compose action.


Next step is to grab the expression by clicking on the ellipsis of the action and select peek code.


From here I copied the expression and used it in my compose action outside of the array.


Now that I know what the expression is of the output, I can use it in my join function for the Compose action. Since the capitalized words are in the form of an array, we need to 'join' them back together. This is where the join function is handy. 

The following is my expression

join(outputs('Uppercase_first_letter'), ' ')

This will result in the following as the output, hooray!

Flow in action

Time to see the flow in action 😃

Manually trigger the flow using the test feature and enter HELLO WORLD TODAY in the text input. The end result will be the words as Proper Case. Ta da!

Summary

Proper Case is a function that is not supported today but in this #WTF episode I outline what can be done in the meantime. The method I share is one way of achieving it.

Till next time 😊 #LetsAutomate

0 comments:

Post a Comment