05.1The calculated column

At times you will want one more piece of information on each row of a table, like a category or a calculation made row by row. That is what the calculated column is for.

05.1.1Adding a calculated column to a table

You can add a column to a physical table as well as to a calculated table. That is the calculated column.

At first, all the columns of a calculated table come from its formula. For Calendar, CALENDARAUTO created the Date column. Once the table exists, you can add other columns to it in two ways:

  • In DAX, by changing its formula, for example with ADDCOLUMNS.
  • Through the Power BI interface, with the New column button. In that case, the formula of the table does not change.

To find this button, in Table view, first choose the table in the Data pane. Then click New column, in the Table tools tab of the ribbon. In Report view, the same button sits in the Modeling tab. A right-click on the table, in the Data pane, also offers New column.

A physical table, like Sales, has no DAX formula to change. To add a calculated column to it in DAX, you therefore go through the New column button.

05.1.2Why create a calculated column?

Several reasons can lead you to create a calculated column. Take the calculated table Calendar. You are going to add several columns to it, needed to analyze the sales properly.

Data needs landmarks in time to make sense. So you are going to create a calculated column for each of these periods, the year, the quarter, the month and the week. With the New column button, you write:

Year = YEAR( Calendar[Date] )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Quarter = QUARTER( Calendar[Date] )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Month = MONTH( Calendar[Date] )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Week = WEEKNUM( Calendar[Date], 2 )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table

In WEEKNUM, the 2 makes the week start on Monday. Each date thus gets its year, its quarter, its month and its week:

Week = WEEKNUM( Calendar[Date], 2 )
Table Calendar
DateYearQuarterMonthWeek
1/1/20242024111
1/2/20242024111
1/3/20242024111
1/4/20242024111
1/5/20242024111
1/6/20242024111
1/7/20242024111
1/8/20242024112
1/9/20242024112
1/10/20242024112
1/11/20242024112
1/12/20242024112
1/13/20242024112
1/14/20242024112
1/15/20242024113
1/16/20242024113
1/17/20242024113
1/18/20242024113
1/19/20242024113
1/20/20242024113
1/21/20242024113
1/22/20242024114
1/23/20242024114
1/24/20242024114
Table: Calendar (731 rows)

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

See easier-to-read landmarks, with FORMAT

The functions above return numbers. January is month 1, the first week of the year is week 1, and so on. In a visual, these numbers are not very handy, and they say little. Yet, if you add columns to a calendar, it is precisely to find the periods more easily.

FORMAT writes the date the way you read it, following a pattern placed between quotation marks. The letters of the pattern are:

  • d for day
  • m for month
  • y for year

The more letters, the longer the result. "mmmm" gives January, "mmm" gives Jan, and "mm" gives 01.

Getting the month in full, from January to December (1/15/2024 gives “January”):

Month Name = FORMAT( Calendar[Date], "mmmm" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table

Getting the month followed by the year, from January 2024 to December 2025 (1/15/2024 gives “January 2024”):

Month and Year = FORMAT( Calendar[Date], "mmmm yyyy" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table

Getting the day of the week, from Monday to Sunday (1/15/2024 gives “Monday”):

Day = FORMAT( Calendar[Date], "dddd" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table

Getting the quarter in the form Q1, Q2, Q3 or Q4 (1/15/2024 gives “Q1”):

Quarter Name = "Q" & QUARTER( Calendar[Date] )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table

In a Power BI file set to English, you get:

Quarter Name = "Q" & QUARTER( Calendar[Date] )
Table Calendar
DateMonth NameMonth and YearDayQuarter Name
1/1/2024JanuaryJanuary 2024MondayQ1
1/2/2024JanuaryJanuary 2024TuesdayQ1
1/3/2024JanuaryJanuary 2024WednesdayQ1
1/4/2024JanuaryJanuary 2024ThursdayQ1
1/5/2024JanuaryFriday
1/6/2024JanuarySaturday
1/7/2024JanuarySunday
1/8/2024JanuaryMonday
1/9/2024JanuaryTuesday
1/10/2024JanuaryWednesday
1/11/2024JanuaryThursday
1/12/2024JanuaryFriday
1/13/2024JanuarySaturday
1/14/2024JanuarySunday
1/15/2024JanuaryMonday
1/16/2024JanuaryTuesday
1/17/2024JanuaryWednesday
1/18/2024JanuaryThursday
1/19/2024JanuaryFriday
1/20/2024JanuarySaturday
1/21/2024JanuarySunday
1/22/2024JanuaryMonday
1/23/2024JanuaryTuesday
1/24/2024JanuaryWednesday
Table: Calendar (731 rows)

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

Watch out!

These columns are text. In a visual, Power BI therefore sorts them in alphabetical order, April before January. That is where the numbers come in. The Month column gives 1 for January, 2 for February, and so on. So sort the Month Name column by the Month column, with the Sort by column button of the Column tools tab. January then comes back to the top.

Placed on the rows of a “Table” visual, next to the “Revenue” measure, the “Year” column groups the sales by year:

Table visual, The “Revenue” measure by year
YearRevenue
2024750,000
2025950,000
Total1,700,000

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

The 5 sales of 2024 come to 750,000, the 7 of 2025 to 950,000. In a slicer, the same “Year” column lets you filter the whole report on 2024 or on 2025.

Practice

Each exercise is solved with one of the functions shown here, on data other than the course data. Pick the right formula out of five.

  1. Direct useCalculated columnDifficulty

    A sports club records the sign-up date of each member in the “Members” table. Add to the “Members” table the calculated column “Year,” the year of sign-up.

    Members
    NameSignup
    Inès3/14/2024
    Hugo9/2/2025
    Jade1/20/2026

    Which formula should you write for “Year”?

  2. Direct useCalculated columnDifficulty

    A bakery records the date of each order in the “Orders” table. Add to the “Orders” table the calculated column “Quarter,” the number of the quarter of the order.

    Orders
    OrderDate
    12/14/2026
    25/3/2026
    311/21/2026
  3. Direct useCalculated columnDifficulty

    A weather station keeps its readings in the “Readings” table. Add to the “Readings” table the calculated column “Month,” the number of the month of each reading.

    Readings
    DayDegrees
    1/5/2026-2
    7/18/202631
    12/30/20261
  4. Direct useCalculated columnDifficulty

    A carrier records the date of each delivery in the “Deliveries” table. For this carrier, the week starts on Monday. Add to the “Deliveries” table the calculated column “Week,” the number of the week of the delivery.

    Deliveries
    ParcelDate
    C-2011/2/2026
    C-2021/12/2026
    C-2033/15/2026
  5. Direct useCalculated columnDifficulty

    A hotel records the arrival date of each guest in the “Bookings” table. Add to the “Bookings” table the calculated column “Day,” the day of the week in full, like “Monday.”

    Bookings
    GuestArrival
    Alma1/2/2026
    Bruno1/12/2026
    Célia3/15/2026

05.1.3A formula that needs a row

Let us go back to the “Year” column of Calendar:

Year = YEAR( Calendar[Date] )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Year = YEAR( Calendar[Date] )
Table Calendar
DateYear
1/1/20242024
1/2/20242024
1/3/20242024
1/4/20242024
1/5/20242024
1/6/20242024
1/7/20242024
1/8/20242024
1/9/20242024
1/10/20242024
1/11/20242024
1/12/20242024
1/13/20242024
1/14/20242024
1/15/20242024
1/16/20242024
1/17/20242024
1/18/20242024
1/19/20242024
1/20/20242024
1/21/20242024
1/22/20242024
1/23/20242024
1/24/20242024
Table: Calendar (731 rows)

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

The formula is calculated for each row of Calendar. On each row, Calendar[Date] refers to a single date, the one on that row, and YEAR draws the year from it.

Written as a measure, the same formula is rejected:

rejected in a measureYear = YEAR( Calendar[Date] )

Power BI shows an error. A measure has no current row, except inside a function that goes through a table row by row, like SUMX, covered later. Calendar[Date] there refers to all 731 dates at once, and YEAR does not know which one to take. If a formula works as a column and not as a measure, this is very often the reason, and not a mistake on your part.

A physical table can get calculated columns too. In Customers, this column puts together the name and the city of each customer:

Customer and City = Customers[Name] & " (" & Customers[City] & ")"
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Customer and City = Customers[Name] & " (" & Customers[City] & ")"
Table Customers
Customer IDNameCityCustomer and City
C001OrvaneNew YorkOrvane (New York)
C002KumoraNew YorkKumora (New York)
C003TalvioNew YorkTalvio (New York)
C004RibeliaChicagoRibelia (Chicago)
C005MovaraChicagoMovara (Chicago)
C006NorthelTorontoNorthel (Toronto)
C007Sam LeeTorontoSam Lee (Toronto)
Table: Customers (7 rows)

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

Written as a measure, it would be rejected for the same reason. Customers[Name] would refer to all 7 names at once.

In a calculated column, the formula always has a current row. A column name there gives the value on that row.

In a calculated column, you do not need to write the table name in front of a column of that same table:

Year = YEAR( [Date] )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table

Power BI understands that this is the Date column of Calendar, the table where you are adding the column. This formula returns the same result as YEAR( Calendar[Date] ).

In the same way, suppose Sales does not have its Amount column yet. Let us add it, for the amount of each sale:

Amount = Sales[Qty] * Sales[Price]
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table

On each row, Sales[Qty] gives the quantity of the sale, and Sales[Price] its price. The formula multiplies them.

Without the table name, you write:

Amount = [Qty] * [Price]
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table

Power BI understands that these are the Qty and Price columns of Sales, the table where you are adding the column. The result is the same.

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

With the New measure button, you write Total Amount = Sales[Qty] * Sales[Price]. What happens?

Question 2

You add to Sales the calculated column Amount = [Qty] * [Price]. What does it hold on row V012, 5 cars at 10,000?

Question 3

Monday, January 1, 2024 opens week 1. What does Week = WEEKNUM( Calendar[Date], 2 ) return on the row of Sunday, 1/14/2024?

Key takeaways
  • A calculated column can be added with the New column button, to a physical table as well as to a calculated table.
  • In a calculated column, a column written on its own gives the value of the row. Written in a measure, it is rejected, except inside a function that goes through a table row by row, like SUMX, covered later.