Do you need to convert Yes or No values to true and false values in Power Automate?
You may often encounter situations where you need to convert Yes or No values to their respective Boolean equivalents, true or false.
This conversion can be crucial for maintaining data consistency when dealing with multiple data sources.
You can utilize expressions or condition actions to perform the transformation. This allows you to map the incoming Yes or No value to its corresponding true or false value.
This article will guide you through the process of transforming Yes or No values into true or false Booleans.
Convert Yes No to Boolean with the If Function
You can easily convert Yes or No text values to boolean values true or false using the if function. This function can be used in expressions to compare a given value to the required text values and then return the corresponding boolean value.
The if function works in a simple way; you can use the syntax if(condition, true, false)
where the condition
checks for a Yes value.
if(equals(outputs('YN_Value'),'Yes'),true,false)
For example, you can use the above expression to convert a given text value to a boolean.
The equals function compares the value from a Compose action named YN Value to see if it’s equal to Yes. If they match, the result is true
. Otherwise, it returns false
.
📝 Note: Keep in mind that the comparison is case-sensitive. To make it case-insensitive, consider using the toLower function so that it does not matter if the input is yes, YES, or Yes.
if(equals(toLower(outputs('YN_Value')),'yes'),true,false)
The above expression demonstrates this approach. It converts your value to lowercase and then compares that to a lowercase yes value.
Both these approaches only check for a Yes value and assume any other value will be a No.
This might not be the case and you might also want to check for a No value. This will require a nested if expression.
if(
equals(toLower(outputs('YN_Value')),'yes'),
true,
if(
equals(toLower(outputs('YN_Value')),'no'),
false,
'Other'
)
)
The above expression will check your value for a Yes, and return true if this is the case. Otherwise, it will check if the value is No and return false if this is the case. If the value is neither Yes or No, it will return the text Other.
Convert Yes No to Boolean with the Bool Function
You can convert Yes or No string values to their corresponding boolean values without the if function.
The equals function will actually return a true or false value. So this can be used to convert the Yes/No to true/false. Then you can use the bool function to ensure this is boolean value.
bool(equals(outputs('YN_Value'),'Yes'))
The above expression will return true when the value in your Compose action is Yes, and false otherwise.
bool(equals(toLower(outputs('YN_Value')),'yes'))
In some scenarios, you may want to be more flexible with the input strings and not limit them to exact case matches. To make this conversion case-insensitive, use the toLower function as above.
By converting both the input string and the comparison string (‘yes’ in this case) to lowercase using the toLower function, you can ensure that the comparison is case-insensitive.
Convert Yes No to Boolean with a Condition Action
You can also use the Condition action to avoid using expressions to convert Yes or No values to their corresponding boolean true or false values.
Here is a summary of the steps needed to convert Yes/No to boolean with a Condition action.
- Add an Initialize variable action to your flow. This will be used to set a value to either true or false depending on the Condition action.
- Give your variable a Name such as ynValue. This will be used to refer to the variable later in the flow.
- Set the variable as String with the Type dropdown menu.
- Add a Condition action to your flow.
- Add the value to the left side of the condition that you want to convert to a true/false. In this example, this value is from a Compose action.
- Select the is equal to comparison option from the dropdown menu.
- Enter a
@{false}
expression on the right side of the condition.
Now you will be able to set the variable to a true or false value within the Condtion If yes and If no branch.
- Add a Set variable action in the If yes branch of the Condition action.
- Select the Name of your variable. In this case, it’s the ynValue variable.
- Add a
@{true}
expression to the Value input of your Set variable action. - Add a Set variable action in the If no branch of the Condition action.
- Select the Name of your variable as the ynValue variable.
- Add a
@{false}
expression to the Value input of your Set variable action.
With these steps, you can easily convert Yes or No text values to their corresponding boolean true or false values using a Condition action.
Conclusions
Converting Yes/No text values to Boolean (true/false) values can be achieved using several methods.
The Condition action is a user-friendly way to include conditional statements in your flow. It enables you to define a condition based on your Yes/No values. Then variables can be set based on the condition to obtain a true or false value.
Expressions involving the if, bool, and equals functions can also be used to convert yes/no values.
When working with expression, you can also include the toLower function to create a case-insensitive solution.
Have you used any of the methods to convert yes and no values to boolean? Do you have any other tips? Let me know in the comments!
0 Comments