07.7TRUE and FALSE

All the conditions you have written in this chapter answer true or false. In DAX, these two answers are written TRUE and FALSE.

07.7.1A comparison returns TRUE or FALSE

A comparison returns neither a number nor a text, but a logical value, TRUE for true or FALSE for false.

A calculated column can keep this answer as it is, without IF:

Big Sale = Sales[Qty] >= 8
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Big Sale = Sales[Qty] >= 8
Table Sales
Sale IDQtyBig Sale
V00110True
V0023False
V0034False
V00410True
V0056False
V0068True
V0073False
V00815True
V0096False
V0103False
V0112False
V0125False
Table: Sales (12 rows)

This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.

On each row, the column holds TRUE or FALSE. It holds TRUE for V001, V004, V006 and V008.

You can then place such a column in a slicer, to filter the big sales in one click.

07.7.2TRUE() and FALSE() in a formula

In a formula, you get these two values with the functions TRUE() and FALSE(). They take no argument, but their parentheses are still required.

These two columns return the same result:

Big Sale = IF( Sales[Qty] >= 8, TRUE(), FALSE() )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Big Sale = Sales[Qty] >= 8
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table

The comparison already returns TRUE or FALSE. The IF adds nothing, and the second form is enough.

07.7.3TRUE() in SWITCH

You saw it with SWITCH, that is where TRUE() is most useful. SWITCH compares TRUE with the answer of each condition, and returns the result of the first one that is TRUE:

Category = SWITCH( TRUE(), Sales[Amount] >= 200000, "Large", Sales[Amount] >= 100000, "Medium", "Small" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
In plain words

“For each sale, I want ‘Large’ if its amount reaches 200,000, ‘Medium’ if it reaches 100,000, otherwise ‘Small,’ testing the thresholds in order, from the first to the last.”

On row V001, the first condition is TRUE, like the first argument. SWITCH returns “Large.”

Practice

Each exercise is solved with the function you just discovered. Two exercises per function, a direct use and a combined use, from easiest to hardest, on data other than the course data. Pick the right formula out of five.

  1. Direct useCalculated columnDifficulty

    A school keeps grades out of 20 in the “Grades” table. Add to the “Grades” table the calculated column “Honors,” which shows “Very good” from 16 up, “Good” from 12 up, otherwise “Pass.”

    Grades
    StudentGrade
    Alma17
    Bilal13
    Chloé9

    Which formula should you write for “Honors”?

  2. Combined useMeasureDifficulty

    A sports club already has two measures, “Signed Up” and “Spots.” The “Signed Up” measure counts the people signed up. The “Spots” measure adds up the spots on offer. Write the “Attendance” measure, which shows “Full” when the people signed up fill all the spots, “Fair” when they fill at least half of them, otherwise “Low.”

Quiz

The questions often rely on the course tables. Keep them in view in their floating window, which you can move and enlarge.

Question 1

You add to Sales this calculated column, with the thresholds in reverse order: SWITCH( TRUE(), Sales[Amount] >= 100000, "Medium", Sales[Amount] >= 200000, "Large", "Small" ). What does row V001 show, whose amount is 250,000?

Question 2

Take the “Category” column of the lesson, which tests 200,000 then 100,000: SWITCH( TRUE(), Sales[Amount] >= 200000, "Large", Sales[Amount] >= 100000, "Medium", "Small" ). How many rows of Sales show “Medium”?

Question 3

You add to Sales the calculated column Sales[Qty] >= 8 && Sales[Price] = 25000, without IF. How many rows are TRUE?

Key takeaways
  • A comparison returns a logical value, TRUE or FALSE.
  • TRUE() and FALSE() are written with their parentheses, even without an argument.
  • In SWITCH( TRUE(), … ), SWITCH returns the result of the first condition that is TRUE.