Exit

Exit

How to Use the Power Fx Exit  Function in Power Apps

What is the Exit Function?

The Exit function in Power Fx allows you to exit a loop or a function and return a value. This can be useful in situations where you want to exit out of a loop early if a certain condition is met, or if you want to return a specific value from a function.

How Does the Exit Function Work?

The Exit function takes one argument, which is the value you want to return. Here’s the syntax for the Exit function:


Exit(value)


When the Exit function is called, it immediately exits the loop or function and returns the specified value. If the Exit function is called outside of a loop or function, it will return an error.

How to Use the Exit Function in Power Apps

Now that you know what the Exit function is and how it works, let’s look at some examples of how you can use it in your Power Apps.

Example 1: Using Exit in a ForAll Loop

Suppose you have a collection of numbers and you want to find the first number that is greater than 10. You can use the ForAll function to loop through the collection and the Exit function to exit the loop as soon as the first number greater than 10 is found.

Here’s an example of how you can use the Exit function in a ForAll loop:


ClearCollect(numbers, [1, 5, 15, 8, 12])

ForAll(numbers, If(Value ❱ 10, Exit(Value)))


In this example, the ForAll function loops through the numbers collection and checks if each number is greater than 10. If a number greater than 10 is found, the Exit function is called with the value of that number, causing the loop to exit early and returning the first number greater than 10.

Example 2: Using Exit in a Function

Suppose you have a function that calculates the average of a collection of numbers. You want to exit the function early if the collection is empty to avoid a divide by zero error. You can use the Exit function to exit the function early and return a default value when the collection is empty.

Here’s an example of how you can use the Exit function in a function:


Function CalculateAverage(numbers)

If(CountRows(numbers) = 0, Exit(0))

Sum(numbers)/CountRows(numbers)

End Function


In this example, the CalculateAverage function checks if the numbers collection is empty using the CountRows function. If the collection is empty, the Exit function is called with a default value of 0, causing the function to exit early and return the default value. Otherwise, the function calculates the average of the numbers collection and returns the result.

The Exit function in Power Fx is a powerful tool that allows you to exit loops and functions early and return specific values. By using the Exit function in your Power Apps, you can make your code more efficient and avoid unnecessary processing. Hopefully, this article has given you a better understanding of what the Exit function is, how it works, and how you can use it in your Power Apps.

Power Apps Training Courses by G Com Solutions (0800 998 9248)