
386 Practical PowerPivot & DAX Formulas for Excel 2010
T
his is a short appendix. It’s aimed at those readers who need a brief introduction to
SQL, with some basic syntax examples. We also discuss the reasons for writing
your own queries to import data into PowerPivot, rather than simply importing
complete tables. A few query fundamentals are covered: filtering, sorting, grouping, and
denormalizing data with joins and self-joins. There are also examples of using a stored
procedure and writing SQL queries against Excel.
C
Key concepts SQL, selecting columns, calculated columns, filtering data, sorting
data, grouping data, denormalizing data with joins and self-joins, stored procedures,
querying Excel
Why Write SQL Queries?
Structured Query Language (SQL) is a standard language for extracting data from
a relational database. When you connect to many of the relational data sources in
PowerPivot, you have the option to write your own queries, rather than simply choosing
table names. Among other sources, this would apply to SQL Server, Access, Oracle,
and IBM DB2. All of these products support the SQL language in queries. The SQL
language is generic and widely popular—and there is an agreed standard called ANSI
SQL. Most vendors adhere very closely to this standard, although each dialect of SQL
(for example, T-SQL in SQL Server or PL/SQL in Oracle) may depart slightly from
that standard and incorporate many proprietary extensions. This appendix concentrates
on writing SQL queries for SQL Server (that is, in T-SQL or Transact-SQL dialect).
However, most of the syntax is generic and also relevant for other software, and you
should be able to adapt the queries for your own database vendor (of course, you will
have to change database, table, and column names!).
If you decide to write your own queries, you can reduce the amount of work you
have to do in the PowerPivot window. This has a number of potential benefits. One, for
calculated columns, you may be more familiar with doing this in SQL, rather than in
DAX, and hence, find it easier. Two, you can use stored procedures to import the data.
Three, you can handle self-joins, which are not supported in this release of PowerPivot.
Four, the evaluation and recalculation of some calculated columns may be faster, if
done in SQL rather than DAX. Five, you may be able to prepare data in a way that is
not possible in PowerPivot (for example, outer joins or complex filters). Six, you may
be able to access other external data sources from your queries that are not options in
PowerPivot (for example, you can use DMX inside a T-SQL linked server query to
extract data from a data mining model).