Exercise - Build and test a Power Fx function

Completed

In this exercise, you create a Power Fx function and test it. Additionally, you learn how to configure a function to accept parameters and return an output. The following video takes you through the steps for this exercise. The detailed instructions are listed in the following exercise.

Scenario

Recall the previous example where a commerce company determines if a customer's order qualifies for local delivery based on the delivery address's US state. Your first task is to define those states in your Power Fx function as New York, New Jersey, and Connecticut. Then, you add a new requirement: orders qualify for local shipping only if the total is over $25.

Create a function

To create a function, follow these steps:

  1. Open the Functions section of make.powerapps.com (you might need to find Functions in the More menu) and verify your environment.

  2. Select + New function from the command bar or select the Create a function button.

    Screenshot of creating a function.

  3. Enter the following properties:

    • Display name - Local Delivery Discount
    • Description - This function validates an order's total and shipping address for local delivery based on whether it's in NY, NJ, or CT and the order is more than $25.
  4. Add a New input parameter with the name OrderTotal and a decimal data type.

  5. Add a New input parameter with the name USState and a string data type.

  6. Add a New output result with the name LocalDelivery and a Boolean data type.

  7. Enter this Power Fx expression in the Formula field:

     {LocalDelivery:
     If((USState="NY" Or USState="CT" Or USState="NJ") And OrderTotal>25, true, false)
     }
     
  8. Select Save.

Test the function

To test the function, follow these steps:

  1. Select the function that you created in the functions list and then select Test in the command bar in the upper part of the screen.

  2. Enter 100 in the OrderTotal field and NY in the USState parameter field.

  3. Select Play. The OData response in the Response field shows the result of the function (LocalDelivery) as true.

    Screenshot of the OData response in the Response field.

  4. Experiment with entering different order totals and two letter state codes and then watch the responses processed in real time.