Code Best Practices Checker | DAX

Power Automate Expression Refactor

This document analyzes and refactors a Power Automate expression, transforming a deeply nested conditional structure into a cleaner, more maintainable format by utilizing variables and array membership checks for improved performance and


Empty image or helper icon

Prompt

If(And(equals(triggerOutputs()?['body/Gen'], 'F2'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'F3'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'F4'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B2'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B3'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B4'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B5'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B4'), Equals(variables('MIR162'), '1')), true, false))))))))

Answer

Language/Technology Identification

The provided code snippet appears to be written in Power Automate (previously known as Microsoft Flow) Expressions, typically used in conditional logic within workflows.

Code Assessment

The current implementation is highly nested and difficult to read, making it prone to errors and challenging to maintain. The nested structure doesn’t scale well, and the repeated calls to triggerOutputs()?['body/Gen'] and variables('MIR162') can lead to performance issues.

Code Refactoring

Original Code

If(And(equals(triggerOutputs()?['body/Gen'], 'F2'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'F3'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'F4'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B2'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B3'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B4'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B5'), Equals(variables('MIR162'), '1')), true, If(And(equals(triggerOutputs()?['body/Gen'], 'B4'), Equals(variables('MIR162'), '1')), true, false))))))))

Refactored Code

@{ // Store values for easier referencing $genValue = triggerOutputs()?['body/Gen']; $mirValue = variables('MIR162');

// Define the accepted values
$acceptedValues = ['F2', 'F3', 'F4', 'B2', 'B3', 'B4', 'B5'];

// Check if conditions are met
If(And(contains($acceptedValues, $genValue), Equals($mirValue, '1')), true, false)

}

Documentation of Changes

  1. Storage of Repeated Values:

    • Original: The values triggerOutputs()?['body/Gen'] and variables('MIR162') were repeatedly called.
    • Refactored: Created variables $genValue and $mirValue to store these outputs and reduce function calls. This improves performance and reduces redundancy.
  2. Use of Array and contains function:

    • Original: Multiple nested If and And statements checked for multiple values.
    • Refactored: Introduced an array $acceptedValues containing all valid conditions, and used contains to check membership within this array. This approach enhances readability and lessens the complexity of conditions, allowing easier updates in the future.
  3. Simplified Boolean Evaluation:

    • Original: The final outcome had unnecessary complexity with multiple true statements.
    • Refactored: The outcome now directly checks if either conditions are satisfied and returns true or false accordingly, simplifying the entire logical structure.

Conclusion

The refactored code significantly improves readability, maintainability, and performance by eliminating redundant function calls and complex conditional nesting. Such restructuring is fundamental in aligning with best practices in workflow logic development within Power Automate. Adopting these practices contributes to a cleaner and more efficient workflow implementation, allowing for easier adjustments and lower chances of introducing errors in complex mutually exclusive conditions.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document analyzes and refactors a Power Automate expression, transforming a deeply nested conditional structure into a cleaner, more maintainable format by utilizing variables and array membership checks for improved performance and readability.