Chapter 12: A Few Ideas: PowerPivot and DAX Solutions 349
Parsing Dates
The Northwind sample has an OrderDate column in the Orders table. This has a data
type of date and is simply a date. Users like to slice, filter, and view years and quarters,
but these are not present as separate columns. Splitting a date into constituent parts is
called parsing a date. There are a number of ways of doing this. One, you can do it in
PowerPivot using DAX. Two, you can parse the date by writing a query for data import
into your model. Three, you can create the columns in the source data. Neither of these
is that difficult, but establishing fiscal years and quarters is more of a challenge. If you
do have fiscal periods, it might also be a good idea to concatenate FY, and also CY to
calendar periods, so dates in a pivot table are unambiguous. Let’s consider calendar
years and calendar quarters first—month names are the same for calendar and fiscal, but
month numbers may or may not be, depending upon your accounting conventions.
If you want to parse dates within the PowerPivot model, use DAX formulas. Here
are a few formulas to get you started. The first one is a calculated column to return the
calendar year—it’s called Year in some of the examples in this chapter. The second one
returns the month as a number—this column is called Month and is used by the next
three formulas, as well as later in the chapter. The month name (called MonthName
later in the chapter) is the result of the third example—it’s also easy to return the full
name, rather than an abbreviation. Formulas four and five are two alternative methods
of returning the calendar quarter (called Quarter).
=YEAR(Orders[OrderDate])
=MONTH(Orders[OrderDate])
=IF(Orders[Month]=1,"Jan",
IF(Orders[Month]=2,"Feb",
IF(Orders[Month]=3,"Mar",
IF(Orders[Month]=4,"Apr",
IF(Orders[Month]=5,"May",
IF(Orders[Month]=6,"Jun",
IF(Orders[Month]=7,"Jul",
IF(Orders[Month]=8,"Aug",
IF(Orders[Month]=9,"Sep",
IF(Orders[Month]=10,"Oct",
IF(Orders[Month]=11,"Nov",
"Dec")))))))))))
=IF(Orders[Month]<4,"Q1",
IF(Orders[Month]<7,"Q2",
IF(Orders[Month]<10,"Q3","Q4")))
=CONCATENATE("Q",ROUNDUP(Orders[Month]/3,0))