Code never lies, comments sometimes do [Ron Jeffries]

Visitors Map

Follow on Twitter

@ZammaCode
Powered by Blogger.

Tuesday, June 20, 2023

Logical Functions in DAX


  • Logical functions evaluates to TRUE or FALSE.
  • Following are the DAX Logical functions.
  • IF(logical_test, result_true, result_false) 
    • Evaluates a condition and returns one value if it is TRUE, and another value if it is FALSE.

    • The IF logical function can also be nested. 
  • IF.EAGER(logical_test, result_true, result_false)
    • IF.EAGER logical function like IF function evaluates a condition and returns one value if it is TRUE, and another value if it is FALSE.
    • F.EAGER has the same functional behavior as the IF function, but performance may differ due to differences in execution plans as it uses an eager execution plan.
    • The IF function is strictly evaluated while IF.EAGER is eagerly evaluated.
      • For example in IF (2<1, 1+2, 4+1), Power BI will only compute the second part (4+1) since the condition 2<1 is false, and the first part (1+2) will not be computed.
      • While in IF.EAGER (2<1, 1+2, 4+1) both true and false parts will be evaluated.
      • In cases where true and false parts are computed the IF.EAGER provides a better performance as compared to IF function.
  • SWITCH(expression, option1, result1, option2, result2,                  ............, elseResult)
    • Switch logical function evaluates an expression against a list of values and return the one of possible results.
    • By default SWITCH function checks for equality, to achieve functionality of other operators like less than or greater than we can replace expression with TRUE() function.
    • By using TRUE() now Switch will be checking the subsequent options/conditions for true.

  • AND(logical_val_1, logical_val_2)
    • AND logical function returns TRUE if both the arguments are TRUE otherwise returns FALSE.
    • The AND function operates similarly to the && logical operator.
  • OR(logical_val_1, logical_val_2)
    • OR logical function returns TRUE if one of the arguments is TRUE, otherwise returns FALSE.
    • The OR function operates similarly to the || logical operator.
  • NOT(logical_val)
    • NOT logical function inverts FALSE to TRUE or TRUE to FALSE.
  • COALESCE(val1, val2, val3, ........)
    • COALESCE logical function accepts multiple arguments and returns the first non-blank argument. If the first argument is blank, it returns the value from the second argument, and so on. If all arguments are blank, it returns blank.
  • IFERROR(expression, val_if_error) 
    • IFERROR logical function returns val_if_error if error occurs during evaluation of expression, otherwise it returns value of expression. 


0 comments:

Post a Comment