Creating calculated columns

While we are in Data view, let's add some additional data to our simple, single-table data model:

  1. Click the Modeling tab.
  2. Click on New Column and a new column named Column will appear in your table.
  3. Type this new formula into the formula bar, completely replacing all existing text, and press the Enter key to create the column:
Month = FORMAT([Date],"MMMM")

Here, we use the FORMAT function to create a friendly month name, such as January instead of 1. Within this formula, we refer to our Date column created previously using the column name sandwiched between square brackets. When referring to columns or measures within DAX formulas, these column and measure names must always be surrounded by square brackets. You should now have a new column called Month in your table with values such as January, February, and March for every row in the table.

  1. Repeat the preceding procedure to create new columns as well as create six new columns using the following DAX formulas:
Year = YEAR([Date])
MonthNum = MONTH([Date])
WeekNum = WEEKNUM([Date])
Weekday = FORMAT([Date],"dddd")
WeekdayNum = WEEKDAY([Date],2)
IsWorkDay = IF([WeekdayNum]=5 ,1, 0)

You should now have a total of eight columns in your Calendar table:

Figure 4: Full calendar table with eight columns

These include our original Date column, our text Month column, and the six columns shown in the preceding screenshot. The first five columns—Year, MonthNum, WeekNum, Weekday, and WeekdayNum—all refer to the original Date column and use simple DAX functions that return the year, numeric month, week number of the year, weekday name, and lastly, weekday as a numeric value between 1 for Monday and 7 for Sunday. The last column, IsWorkDay, uses the IF DAX function.

The IF function works in an identical manner to Excel's IF function. The first input parameter is a true/false expression, the second parameter is the result returned if that expression evaluates to true, and the last parameter is the result if that expression evaluates to false. In this case, we are returning 1 for Monday – Friday, and 0 for Saturday and Sunday.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.100.205