Understanding Data Tables
Understanding Data Tables
Data Analysis involves browsing data over time and making calculations
across time periods. For example, you might have to compare the current
year’s profits with the previous year’s profits. Similarly, you might have to
forecast the growth and profits in the coming years. For these, you need to
use grouping and aggregations over a period of time.
DAX provides several Time Intelligence functions that help you perform most
of such calculations. However, these DAX functions require a Date table for
usage with the other tables in the data model.
You can either import a Date table along with other data from a data source
or you can create a Date table by yourself in the data model.
In this chapter, you will understand different aspects of Date tables. If you
are conversant with Date tables in the Power Pivot data model, you can skip
this chapter and proceed with the subsequent chapters. Otherwise, you can
understand the Date tables in the Power Pivot data model.
For example,
A Date table can have columns such as Date, Fiscal Month, Fiscal Quarter, and
Fiscal Year.
A Date table can have columns such as Date, Month, Quarter, and Year.
If you have to report on a calendar year, you need a Date table with a column –
Date, which contains all the dates from January 1st, 2014 to December 31st, 2016
in a sequence.
If you have to report on a fiscal year, and your fiscal year end is 30thJune, you
need a Date table with a column – Date, which contains all the dates from July
1st, 2013 to June 30th, 2017 in a sequence.
If you have to report on both calendar and fiscal years, then you can have a single
Date table spanning the required range of dates.
Your Date table must contain all of the days for the range of every year in
the given duration. Thus, you will get contiguous dates within that period of
time.
If you regularly refresh your data with new data, you will have the end date
extended by a year or two, so that you do not have to update your Date table
often.
Creating a Date table in Excel and then copying or linking to a new table in Power
Pivot.
Type the first date in the date range that you want to create in the second row in
the same column.
Select the cell, click the fill handle and drag it down to create a column of
contiguous dates in the required date range.
For example, type 1/1/2014, click the fill handle and drag down to fill the
contiguous dates up to 31/12/2016.
Click Table.
Click OK.
This will add the contents of the clipboard to a new table in the data model.
Hence, you can use the same method to create a Date table in an existing
data model also.
Click OK.
This copies the contents of the clipboard to a new table in the data model.
Now, you have a Date table in the data model with a single column of
contiguous dates. The header of the column is Date as you had given in the
Excel table.
For example, you can add columns – Day, Month, Year, and Quarter as follows
−
Day
=DAY('Date'[Date])
Month
=MONTH('Date'[Date])
Year
=YEAR('Date'[Date])
Quarter
The resulting Date table in the data model looks like the following screenshot.
Thus, you can add any number of calculated columns to the Date table. What
is important and is required is that the Date table must have a column of
contiguous dates that spans the duration of time over which you perform
calculations.
Suppose, you want to create a Date table for the calendar year 2017.
Create an Excel table with a column Date, consisting of contiguous dates from
1st January 2017 to 31st December 2017. (Refer to the previous section to know
how to do this.)
Copy the Excel table and paste it into a new table in the data model. (Refer to the
previous section to know how to do this.)
o Day =DAY('Calendar'[Date])
o Month =MONTH('Calendar'[Date])
o Year =YEAR('Calendar'[Date])
For example, for the US, you can get the list of holidays for any required year
from the following link http://www.calendar-365.com/.
Copy the Excel table and paste it into a new table in the data model.
Next, you can add a calculated column of holidays to the Calendar table using
DAX LOOKUPVALUE function.
=LOOKUPVALUE(Holidays[Holiday],Holidays[Date],'Calendar'[Date]
)
You can include the fiscal time periods in the calendar table using the DAX
formulas −
FYE:=3
o Fiscal Year
=IF('Calendar'[Month]<='Calendar'[FYE],'Calendar'[Year],'Calendar'[Year
]+1)
o Fiscal Month
=IF('Calendar'[Month]<='Calendar'[FYE],12-
'Calendar'[FYE]+'Calendar'[Month],'Calendar'[Month]-'Calendar'[FYE] )
o Fiscal Quarter
=INT(('Calendar'[Fiscal Month]+2)/3)