Explore business scenarios for Power Fx functions

Completed

After learning about the fundamentals of building Power Fx functions, you can explore some real-world applications. In this unit, you explore practical business scenarios and observe how Power Fx functions can help you streamline logic, improve efficiency, and enhance the user experience.

Example: Apply discount codes at checkout

In this example, you work for a company that sells event tickets, and they want to offer discount codes during checkout. The company needs to ensure that the discount is valid according to these criteria:

  • The code hasn't expired
  • The event is eligible for discounts
  • The user hasn't already used this code

Additionally, you want real-time validation so that the system applies the discount instantly before the order is complete.

To do so, you need to set up the necessary tables first:

  • Discount Code - Stores codes, expiration dates, and discount percentages.
  • Event - Contains a validation flag to determine eligible events.
  • User/Purchases - Tracks purchase history, including applied discount codes.

When building your function, use the following parameters:

  • Input parameters:

    • DiscountCode (string)
    • EventID (string)
    • UserLogin (string)
    • Amount (decimal)
  • Output parameter: TotalAmount (decimal)

The following example shows how your Power Fx function might appear:

{TotalAmount:
If(LookUp(Discount Codes, ‘Code'=DiscountCode).'Expiration Date'>Today() And LookUp(Events, ‘Event ID' = EventID).'Validation'=true And ForAll(LookUp(Purchases, ‘User'=UserLogin).'Discount Code Applied'<>DiscountCode, (Amount-Amount*.20), Amount)
}

With this function, you build a flexible and reusable solution that works for any discount code or event in the dataset. Moreover, you can use it across multiple apps, whether it's a mobile app or a desktop app, by passing in the required parameters and using the returned value.

Example: Automatically apply free shipping

In this common e-commerce scenario, your company wants to offer free shipping when an order reaches a specific amount. You can create a function that checks the order subtotal and then determines whether to apply the discount or not.

To do so, you need the following tables:

  • Discount Code - Stores codes and expiration dates.
  • Items - Contains product details, including cost.

You also need the following parameters:

  • Input parameter - Subtotal (decimal)

  • Output Parameter - CodeStatus (Boolean)

The function needs to run whenever someone adds an item to the cart to evaluate whether the order qualifies for free shipping or not.

The Power Fx function for this scenario might resemble the following example:

{CodeStatus:
If(LookUp('Discount Codes','Discount Code'="FREEDEL").'Exp Date'>Now() And Subtotal>25, true, false)
}

This approach ensures that, when a user reaches the free shipping threshold, the system automatically applies the discount without the need for extra steps and with no interruption to the user.

Screenshot of the free shipping example.

Example: Reward frequent travelers

For this example, you work for a company that wants to create a travel booking app to reward loyal customers. Instead of applying the discount in real time, you might want to issue it on special occasions, such as birthdays or anniversaries.

In this case, you use a Power Automate flow in conjunction with your Power Fx function. Your function needs to validate if a customer qualifies based on their history.

To do so, you need the following tables:

  • Discount Code - Stores codes, expiration dates, and discount percentages.
  • Customer - Tracks VIP status and purchase history.

You also need these parameters:

  • Input parameter - CustomerNumber (string)

  • Output parameter - CodeStatus (Boolean)

The Power Fx function for this scenario might resemble the following example:

{CodeStatus:
If(LookUp(Customers,'Customer Number'=CustomerNumber).'Paid on time'=true And LookUp(Customers,'Customer Number'=CustomerNumber).'Purchased YTD'>25000, true, false)
}

Next, you can create a scheduled Power Automate flow to check for customer birthdays and then send them a discount if they qualify. This approach adds a layer of automation and personalization to the customer experience.

Screenshot of creating a scheduled Power Automate flow.

By structuring your logic with Power Fx functions, you can create flexible, reusable solutions that simplify processes across multiple apps and scenarios. Whether validating discount codes, applying promotions in real time, or integrating with Power Automate for scheduled actions, these functions can help you build smarter, more efficient apps.