This presentations shows how to create a time/date dimension for PowerPivot from the date data in your fact table. I also shows the DAX functions that you can use to add columns to the fact table or a separate dimension table.
3. • Aggregate – A mathematical function that allows you to
summarize values of an attribute
• Dimension - A dimension is essentially a look-up table that
may define a hierarchy or drill-down path such as Year >
Quarter > Month
• Measure – A measure is something that identifies a value
• Fact – A fact is another term for a measure that contains
numeric data that can be grouped along one or more
dimensional hierarchy
• Star Schema – All dimension tables radiate out from a single
fact table
• Snowflake Schema – One fact table may relate to another fact
table before relating to dimension tables. One dimension table
can also have a related dimension table
• A Pivot table or chart is usually based around a single fact
table
6/16/2012 Orlando Code Camp 2012 3
4. • Database must either be in a Star or Snowflake model.
This often means de-normalizing some tables or
‘flattening’ the data model. Essentially you need a central
‘fact’ table from which all supporting dimension data
radiates.
• You must flatten many-to-many relationships into a single
table with only the data that you need.
• Based on date data in your central fact table, build a date
dimension table with the columns needed.
• Create a script to populate the date dimension table with
data.
6/16/2012 Orlando Code Camp 2012 4
8. • You need to calculate the date range needed for your date dimension.
I usually look for the earliest and latest dates in the fact table and
round the dates to January 1st for the earliest date and December 31st
for the latest date.
• DECLARE @Min_Date DATE;
• DECLARE @Max_Date DATE;
• -- Get the smallest and largest date for start and end of date table
• SET @Min_Date = (SELECT
CONVERT(DATE,'01/01/'+STR(YEAR(MIN(VisitDate)))) FROM
dbo.PatientVisits);
• SET @Max_Date = (SELECT
CONVERT(DATE,'12/31/'+STR(YEAR(MAX(VisitDate)))) FROM
dbo.PatientVisits);
6/16/2012 Orlando Code Camp 2012 8
13. • PowerPivot is a data analysis tool
• PowerPivot is a free download from Microsoft
• PowerPivot lets you process millions of rows of data
• PowerPivot uses as much memory as available & multi-cores
• PowerPivot lets you integrate data from multiple sources
• PowerPivot lets you explore, analyze and create analysis
• PowerPivot was originally called: Project ‘Gemini’
6/16/2012 Orlando Code Camp 2012 13
14. • 32-or 64 bit Operating System
• 1 GB of RAM, 2 GB recommended (or more)
• Windows XP with SP3, Vista with SP1, Windows 7
• .NET Framework 3.5 SP1 or better
• Microsoft Office 2010
6/16/2012 Orlando Code Camp 2012 14
15. • Fewer data requests since staff can slice and dice
their own data
• More control over data other people can access
• Users can generate their own reports
• PowerPivot gives SQL Server data access to the
masses
6/16/2012 Orlando Code Camp 2012 15
16. • Use 1 and only 1 header row
• All data in column should be consistently
formatted
• No blank columns
• No blank rows
• Data columns must be 100% filled with data
6/16/2012 Orlando Code Camp 2012 16
17. • Always load the main transaction table first
• This forces the fields to appear at the top of the PowerPivot Field
List.
• PowerPivot’s relationship detection expects the main table to be
first.
• Import lookup tables in their hierarchical order
6/16/2012 Orlando Code Camp 2012 17
18. • Simple Calculations
• Calculated columns within table
• Calculated columns between tables
• Calculated columns to serve as links to other tables
• Calculated columns are calculated for every row in the table.
• Aggregate Calculations
• Calculate unique measures
• Aggregate measures are only calculated for the displayed data in
the Pivot table
6/16/2012 Orlando Code Camp 2012 18
19. • Comprised of 135 functions
• 71 functions are similar to Excel functions
• 69 have the same name – 2 do not
• TEXT FORMAT
• DATEDIF YEARFRAC
• 64 functions are unique to DAX
• Aggregate data functions
• Date related functions
6/16/2012 Orlando Code Camp 2012 19
21. • Get the Year:
=YEAR(Patient_Vaccinations[VisitDate])
• Get Month Number:
=FORMAT(MONTH(Patient_Vaccinations[VisitDate]),"00"
)
• Get name of the month
=FORMAT(Patient_Vaccinations[VisitDate],"MMM")
• Get name of the week day
=FORMAT(WEEKDAY(Patient_Vaccinations[VisitDate]),
"dddd")
6/16/2012 Orlando Code Camp 2012 21
22. • Calculating Quarter of Year
=INT((MONTH(Patient_Vaccinations[VisitDate])-
1)/3)+1
• Calculating Quarter of Year Name
="Qtr " &
INT((MONTH(Patient_Vaccinations[VisitDate])-
1)/3)+1
• Get week number of year
=WEEKNUM(Patient_Vaccinations[VisitDate])
6/16/2012 Orlando Code Camp 2012 22
26. Don’t forget to fill out your
evaluations.
Mike@micmin.org
SharePoint Book Site:
http://sharepointmike.wordpress.com/
6/16/2012 Orlando Code Camp 2012 26
Notas del editor
Run SQL: BuildTexasData
Transaction: Patient_VaccinationsDimensions:DateTableCountyDataRaceTable Regions
Next we need to define the DAX formula for the measure. We learned earlier that all DAX formulas begin with an equal sign. Then as we type additional characters, the tool-tip help displays the possible commands that begin with that letter(s) along with a helpful hint about what the function returns. In this case, as we type ‘di’ for DISTINCT, we learn that this function returns a one column table in which the distinct values appear in a single column. The fact that this function returns not a column or list of values, but a table is something to remember. The reason this is important will become evident shortly. As we press the Tab key to complete the selection of this function, we see the function expects as an input parameter a column. At this point the expression builder box contains =DISTINCT(. If I type the first letter of any of the tables, the tooltip help will display a reference to all the fields in the table. To count distinct visits, I will use the VisitID field in the Patient_Vaccinatinos table as shown in the following figure.If we were to close off the equation at this point and attempt to use it in our pivot table, we would get the error message:ERROR – CALCULATION ABORTED: MdxScript(Sandbox) (8,66) A table of multiple values was supplied where a single value was expected.A measure must return a single value, not a table or even a column. Therefore, we have to wrap this calculation inside of another function that can count the rows in a table. Looking through the available DAX functions, the obvious choice is the function =COUNTROWS() which fortunately accepts a table as input. Similar functions like =COUNT() and =COUNTA() expect to have a column passed to them, not a table. Remember a column is essentially a field in one of our source tables from the PivotTable Window. (By the way, the different between =COUNT() and =COUNTA() is that….)Final Expression is: COUNTROWS(DISTINCT(Patient_Vaccinations[VisitID]))