Tải bản đầy đủ (.pdf) (16 trang)

quickstart learn dax basics in 30 minutes

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.04 MB, 16 trang )



QuickStart: Learn DAX Basics in 30
Minutes
Owen Duncan








Summary: This QuickStart is for users new to PowerPivot or tabular model projects
authored in SQL Server Data Tools. It is meant to give you a quick and easy introduction
on how you can use Data Analysis Expressions (DAX) to solve a number of basic data
modeling and analytical problems.
Category: Quick Step-By-Step
Applies to: PowerPivot for Excel (all versions), SQL Server 2008 R2, SQL Server 2012
Source: TechNet Wiki (link to source content
)
E-book publication date: May 2012
13 pages





Copyright © 2012 by Microsoft Corporation
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means


without the written permission of the publisher.


Microsoft and the trademarks listed at
are trademarks of the
Microsoft group of companies. All other marks are property of their respective owners.

The example companies, organizations, products, domain names, email addresses, logos, people, places, and events
depicted herein are fictitious. No association with any real company, organization, product, domain name, email address,
logo, person, place, or event is intended or should be inferred.

This book expresses the author’s views and opinions. The information contained in this book is provided without any
express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will
be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.

Qui
c
Min

This Quick
S
meant to
g
number of
you can c
o
understan
d
What is D
A

DAX is a c
o
and return
model.
Why is D
A
It’s easy to
PivotChart
s
critical sal
e
inventory
d
important
c
data. Whe
n
line. This i
s
Prereq
You might
understan
d
you get st
a
We are go
i
with funda
Help. You
s

PowerPivo
t
help, whic
h
Example
W
The best
w
yourself. T
h
workbook
download
e
Note:
If you are
w
c
kSta
r
utes

S
tart is for use
r
g
ive you a quic
k
basic data mo
o
mplete, and a

d
ing of the mo
A
X?
o
llection of fun
one or more
v
A
X so importa
n
create a Powe
s
that display i
m
e
s data across
s
d
ata from seve
r
c
apabilities as
w
n
you get the i
n
s
Business Intel
uisites

already be fa
m
d
ing DAX, but
e
a
rted creating
D
i
ng to focus sp
mental conce
p
s
hould also be
t
, you should f
i
h
is usually inst
W
orkbook
w
ay to learn DA
X
h
e examples a
n
from http://go
e
d onto your c

o
w
orking in the
A
r
t:Le
a

r
s new to Pow
e
k
and easy intr
o
deling and an
a
few quizzes to
s
t basic funda
m
ctions, operat
o
v
alues. Stated
m
n
t?
rPivot workbo
o
m

portant infor
s
everal produc
t
r
al tables in di
f
w
ell. Learning
n
formation yo
u
ligence, and D
A
m
iliar with crea
t
e
ven if you ha
v
D
AX formulas
a
ecifically on u
n
p
ts of both calc
familiar with t
h
i

rst step throu
g
alled on your
c
X
is to create s
n
d tasks here u
.microsoft.co
m
o
mputer, open
A
nalysis Servic
a
rn
D
e
rPivot or tabu
l
o
duction on h
o
a
lytical proble
m
test what you’
m
ental concep
t

o
rs, and consta
m
ore simply, D
A
o
k and import
mation withou
t
categories an
f
ferent data so
u
how to create
e
u
need, you ca
n
A
X will help yo
t
ing formulas i
n
v
e no experien
c
a
nd solving re
a
n

derstanding
D
ulated column
h
e PowerPivot
g
h the Create
Y
c
omputer whe
n
ome basic for
m
se the Contos
o
m
/fwlink/?LinkI
D
it, and then o
p
es tabular mo
d
D
AXB
a
l
ar model proj
e
o
w you can us

e
m
s. This topic i
n
ve learned. Aft
t
s in DAX.
nts that can b
e
A
X helps you c
some data int
o
t using any D
A
d for different
u
rces? DAX for
e
ffective DAX
f
n
begin to solv
u get there.
n
Microsoft Ex
c
c
e with Excel f
o

a
l-world BI pro
b
D
AX formulas u
s and measure
for Excel auth
o
Y
our First Pow
e
n
the PowerPiv
o
m
ulas, use it wi
o
Sample DAX
D
=237472&clc
i
p
en the Power
P
d
el project aut
h
a
sics
i

e
cts authored i
n
e
Data Analysis
n
cludes conce
p
er completing
e
used in a for
m
reate new info
o
it. You can e
v
A
X formulas. B
u
date ranges?
O
mulas provide
f
ormulas will h
e
e real busines
s
c
el. That knowl
o

rmulas, the co
b
lems right aw
a
sed in calculati
s, both of whi
c
o
ring environ
m
e
rPivot Workb
o
o
t for Excel ad
d
th some actua
l
Formulas.xlsx
w
i
d=0x409. Onc
e
P
ivot window.
h
oring environ
m
i
n30


n
SQL Server
D
Expressions (
D
p
tual informati
o
this topic, you
m
ula, or expres
rmation from
d
v
en create Pivo
t
u
t, what if you
n
O
r, you need c
o
this capability
e
lp you get th
e
s
problems tha
t

edge will be h
e
ncepts describ
a
y.
ons. You shou
l
c
h are describe
d
m
ent and tools.
o
ok tutorial incl
d
-in is installe
d
l
data, and see
w
orkbook. You
e
you have th
e
m
ent in SQL S
e

D
ata Tools. It is

D
AX) to solve a
o
n, a series of
t
should have a
sion, to calcul
a
d
ata already in
t
Tables or
n
eed to analyz
e
o
mbine import
a
and many oth
e
e
most out of y
t
affect your b
o
e
lpful in
ed here will h
e
l

d already be f
a
d
in PowerPiv
o
If you are new
uded in Powe
r
d
.
the results for
can downloa
d
e
workbook
e
rver Data Too
l

t
asks
good
a
te
your
e

a
nt
e

r
our
o
ttom
e
lp
a
miliar
o
t
to
r
Pivot
d
the
l
s
(SSDT), you can import the example workbook by using the Import from PowerPivot template in Visual Studio 2010.
The basic DAX concepts and formulas described here work the same for tabular model projects as they do in
PowerPivot. Only the authoring environment user interface is different. In SSDT, you can test your new formulas by
using the Analyze in Excel feature.
Let’s Begin!
We will frame DAX around three very important fundamental concepts: Syntax, Functions, and Context. Of course,
there are other important concepts in DAX, but understanding these three concepts will provide the best foundation
on which to build your DAX skills.
Syntax
Before you create your own formulas, let’s take a look at DAX formula syntax. Syntax includes the various elements
that make up a formula, or more simply, how the formula is written. For example, let’s look at a simple DAX formula
used to create new data (values) for each row in a calculated column, named Margin, in a FactSales table: (formula
text colors are for illustrative purposes only)



This formula’s syntax includes the following elements:
A. The equals sign operator (
=) indicates the beginning of the formula, and when this formula is calculated it
will return a result or value. All formulas that calculate a value will begin with an equals sign.
B. The referenced column
[SalesAmount] contains the values we want to subtract from. A column
reference in a formula is always surrounded by brackets []. Unlike Excel formulas which reference a cell, a
DAX formula always references a column.
C. The subtraction (
-) mathematical operator.
D. The referenced column
[TotalCost] contains the values we want to subtract from values in the
[SalesAmount] column.
When trying to understand how to read a DAX formula, it is often helpful to break down each of the elements into a
language you think and speak every day. For example, you can read this formula as:
In the FactSales table, for each row in the Margin calculated column, calculate (=) a value by subtracting (-) values in
the [TotalCost] column from values in the [SalesAmount] column.
Let’s take
a

This formu
A. T
h
f
o
B. T
h
r

e
C. T
h
f
u
D. P
a
p
a
E. T
h
F. T
h
k
n
You can re
a
For the m
e
FactSales t
a
When plac
by each ce
Notice the
r
column. In
complex c
a
functions l
a

Unlike the
in which t
h
preceded
b
formula. T
h
practice to
Note:
If the nam
e
in single q
u
outside th
e
a
look at anoth
la includes the
h
e measure na
o
llowed by a c
o
h
e equals sign
e
turn a result.
h
e function S
U

u
nctions later.
a
renthesis ()
s
a
sses a value t
o
h
e referenced
t
h
e referenced
c
n
ows on which
a
d this formul
a
e
asure named
S
a
ble.
ed into the Val
ll in the PivotT
a
r
e are a few th
i

particular, we
a
lculations and
a
ter.
Margin calcul
a
h
e column bel
o
b
y the table na
h
is can make l
o
always includ
e
e
of a table co
n
u
otation mark
s
e
ANSI alphan
u
er type of for
m
following syn
t

me Sum of
S
o
lon, followed
b
operator (=) i
n
U
M adds up all
o
s
urround one
o
o
a function.
t
able FactSa
l
c
olumn [Sal
e
column to ag
g
a
as:
S
um of Sales A
m
ues drop zone
a

ble, for exam
p
i
ngs different
a
introduced a f
u
manipulation
s
a
ted column ea
ngs. This is kn
o
me. Columns r
o
ng formulas t
h
e
the table na
m
n
tains spaces,
r
s
. You must als
o
u
meric charact
e
m

ula, one that i
s
t
ax elements:
S
ales Amo
u
b
y the calculati
n
dicates the be
o
f the number
s
o
r more argum
l
es.
e
sAmount] i
n
g
regate a SUM
m
ount, calcula
t
in a PivotTabl
e
p
le, Cell Phone

s
a
bout this for
m
u
nction, SUM.
F
s
with number
s
rlier, you see t
h
o
wn as a fully
q
eferenced in t
h
h
at reference
m
m
e in your mea
s
r
eserved keyw
o
o
enclose tabl
e
e

r range, regar
d
s
used in a me
a
u
nt. Formulas
on formula.
ginning of the
s
in the [Sales
A
ents. All functi
o
n
the FactSale
s
.
t
e (=) the SU
M
e
Field List, thi
s
s
in the USA.
m
ula compared
F
unctions are

p
s
, dates, time, t
e
h
e column [Sal
q
ualifie
d
colu
m
h
e same table
d
m
any columns
s
s
ure formulas,
o
rds, or disallo
w
e
names in qu
o
d
less of wheth
e
a
sure:

for measures
c
calculation fo
r
A
mount] colum
n
o
ns require at
s
table. With th
M
of values in t
h
s
measure calc
u
to the formula
p
re-written for
m
e
xt, and more.
esAmount] wa
m
n name in tha
t
d
o not require
s

horter and ea
s
even when in
t
w
ed characters
,
o
tation marks i
f
e
r your locale
s
c
an include the
r
mula. When c
a
n
. You will lear
n
least one argu
m
is argument, t
h
h
e [SalesAmou
n
u
lates and retu

we used for t
h
m
ulas that ma
k
You will learn
s preceded by
t
it includes th
e
the table nam
e
s
ier to read. H
o
t
he same table
,
you must enc
f
the name con
s
upports the c
h
measure nam
e
a
lculated, it will
n
more about

m
ent. An argu
m
h
e SUM functi
o
n
t] column in t
rns values defi
h
e Margin calc
u
k
e it easier to d
more about
the table Fact
S
e
column nam
e
e
be included i
n
o
wever, it is go
o
.
lose the table
n
tains any char

a
h
aracter set or
e
,

m
ent
o
n
he
ned
u
lated
o
S
ales
e

n
the
o
d
n
ame
a
cters
not.
It is very important your formulas have the correct syntax. In most cases, if the syntax is not correct, a syntax error will
be returned. In other cases, the syntax may be correct, but the values returned might not be what you are expecting.

PowerPivot (and SQL Server Data Tools) includes IntelliSense; a feature used to create syntactically correct formulas by
helping you select the correct elements.
Let’s create a simple formula. This task will help you further understand formula syntax and how the IntelliSense
feature in the formula bar can help you.
Task: Create a simple formula for a calculated column
1. If you are not already in the PowerPivot window, in Excel, on the PowerPivot ribbon, click PowerPivot
Window.
2. In the PowerPivot window, click the FactSales table (tab).
3. Scroll to the right-most column, and then in the column header, click Add Column.
4. Click in the formula bar along the top of the model designer window.

Your cursor now appears in the formula bar. The formula bar is where you can type a formula for a
calculated column or a measure.
Let’s take a moment to look at the three buttons to the left of the formula bar.

When the cursor is active in the formula bar, those three buttons become active. The leftmost button, the X,
is simply a cancel button. Go ahead and click it. Your cursor no longer appears in the formula bar, and the
cancel button and checkmark button no longer appear. Go ahead and click in the formula bar again. The
cancel button and the checkmark button now reappear. This means you are ready to start entering a
formula.
T
h
c
o
C
F
u
f
o
c

a
5. I
n
w
B
e
c
o
c
a
Y
o
6. T
y
7. N
If
c
a
A
f
b
o
t
h
8. R
i
That’s it! Y
o
formula ca
[SalesQua

n
in DAX; ro
w
Tip:
Somethin
g
arguments
be a text v
a
formula to
2, DAX rea
d
result is of
based on t
scope of t
h
Books Onli
Let’s try a
n
much if yo
u
several ele
m
Task: Cre
a
h
e checkmark
b
o
me back to it

lick the Fx but
t
u
nction dialog
o
rmula when
w
a
lculated colu
m
n
the formula b
w
ith all of the c
o
e
cause calcula
t
o
lumn name w
a
n also scroll t
o
o
ur cursor is n
o
y
pe a space, a
n
ow, type anot

h
you get an err
a
lculated colu
m
f
ter you press
E
o
ttom of the P
h
ree million ro
w
i
ght click the c
o
o
u just created
lculates a valu
e
n
tity] column.
N
w
context. You
w
g
really import
a
you are using

.
a
lue of “12”. T
h
read: Calculat
e
d
s this formul
a
course “3”, a
n
he data type o
h
is Quick Start.
ne.
n
other. This tim
u
don’t fully u
n
m
ents togethe
a
te a measure
b
utton is the c
h
in a little bit.
t
on. You’ll see

t
box is the easi
e
e create a me
a
m
n formula. Go
ar, type an eq
u
o
lumns in the
F
t
ed columns ar
ith the table n
a
o
the column n
o
w active to th
e
n
d then type a
h
er opening br
a
or, look carefu
m
n described e
a

E
nter to compl
o
werPivot win
d
w
s.
o
lumn header
a simple yet
v
e
by subtractin
g
N
otice how we
w
ill learn mor
e
a
nt to understa
.
For example,
i
h
is is because t
e
a result by ta
k
a

as: Calculate
a
n
umeric value.
D
f columns use
d
To learn more
e, you will cre
a
n
derstand the
f
r in correct sy
n
formula
h
eck formula
b
t
hat a new dial
e
st way to get
a
sure a little lat
ahead and cl
o
u
als sign =, the
F

actSales table.
e always creat
e
a
me. Go ahead
ame you want
,
e
right of [Sal
e
subtraction op
a
cket [. This ti
lly at your syn
t
a
rlier.
ete the formul
a
d
ow. It goes q
u
and rename th
v
ery powerful
D
g
the value in
t
just said “For

e
e
about row co
n
nd when typin
g
i
f you were to
t
he ampersand
k
ing the value
a
result by taki
n
D
AX calculates
d
in the argum
e
about data ty
p
a
te a measure
b
f
ormula. The i
m
n
tax.

b
utton. It does
n
og box appea
r
started enteri
n
er, but for no
w
o
se the Insert F
n type an ope
n
This is IntelliS
e
e
d in the activ
e
and scroll do
w
,
and then pre
s
e
sQuantity].
erator
- (a mi
n
me, select the
t

ax. If necessar
y
a
, the word Ca
u
ickly, even th
o
e column,
N
e
t
D
AX formula. F
o
t
he [ReturnQu
a
e
ach row”. This
n
text later.
g
an operator
i
t
ype the follo
w
(&) operator i
s
1 as text and a

n
g the numeri
c
resultant valu
e
e
nt. Data type
s
p
es and opera
t
b
y typing the f
o
m
portant thing
n
’t do much un
r
s; the Insert F
u
n
g a DAX form
u
w
, you do not n
unction dialog
n
ing bracket [
.

e
nse in action.
e
table you are
w
n and then d
o
s
s Tab.
n
us sign), and
t
[ReturnQuan
t
y
, compare it t
o
lculating app
e
o
ugh you just c
t
Sales.
o
r each row in
t
a
ntity] column
is a glimpse o
f

i
nto a DAX for
m
w
ing formula, =
s
for text conc
a
dd value 2 as
t
c
value 1 and a
d
e
s depending
o
s
in DAX are ve
t
ors in DAX for
m
o
rmula and by
here is to lear
n
til you’ve ente
r
u
nction dialog
b

u
la. We’ll add
a
eed to add a f
u
box.
.
You’ll see a s
m

in, there is no
o
uble-click [Sa
l
t
hen type anot
h
t
ity] column, a
n
o
the formula i
n
e
ars in the stat
u
alculated new
t
he FactSales t
a

from the value
f
another very
m
ula is the dat
1 & 2, the val
u
a
tenation. DAX
t
ext. Now, if yo
d
ding the nu
m
o
n the operato
ry important,
b
m
ulas, see the
using IntelliSe
n
how to creat
e
r
ed a formula.
W
b
ox. The Insert
a

function to a
u
nction to you
r
m
all window a
p
need to prece
d
l
esQuantity].
Y
h
er space.
n
d then press
E
n
the Margin
u
s bar along t
h
values for mor
a
ble, the NetS
a
in the
important con
c
a type in the

u
e returned w
o
interprets this
u were to type
m
eric value 2. T
h
r in the formul
a
b
ut outside the
DAX Referenc
e
nse. Don’t wor
e
a formula usi
n
W
e’ll
r

p
pears
d
e the
Y
ou
E
nter.

h
e
e than
a
les
c
ept
o
uld
= 1 +
h
e
a
, not
e
in
ry too
n
g
1. In the FactSales table, click in any empty cell in the Calculation Area (also known as the measure grid in the
model designer). This is the area of empty cells just below a table in the PowerPivot window.

2. In the formula bar, type the name
Previous Quarter Sales:.
3. Type an equals sign
= to begin the calculation formula.
4. Type the first few letters
CAL, and then double-click the function you want to use. In this formula, you want
to use the CALCULATE function.
5. Type an opening parenthesis

( to begin the arguments to be passed to the CALCULATE function.
Notice after typing the opening parenthesis, IntelliSense shows you the arguments required for the
CALCULATE function. You will learn about arguments in a little bit.
6. Type the first few letters of the FactSales table, and then in the dropdown list, double-click FactSales[Sales].
7. Type a comma (
,) to specify the first filter, then type, PRE, and then double-click the PREVIOUSQUARTER
function.
After selecting the PREVIOUSQUARTER function, another opening parenthesis appears, indicating another
argument is required; this time, for the PREVIOUSQUARTER function.
8. Type the first few letters
Dim, and then double-click DimDate[DateKey].
9. Close both the argument being passed to the PREVIOUSQUARTER function and the CALCULATE function by
typing two closing parentheses
)).
Your formula should now look like this:
Previous Quarter Sales:=CALCULATE(FactSales[Sales],
PREVIOUSQUARTER(DimDate[DateKey]))

10. C
el
You did it!
the total s
a
You were j
u
Notice the
can contai
n
such a for
m

In this for
m
an argum
e
Finally, yo
u
models an
d
function is
one.
Note:
Typically, i
n
using the
M
the DimDa
Tip:
You may b
have to’. A
model nee
measure y
o
functions t
the AutoS
u

Extra Cre
d
1. In the F
a

whole colu

2. On the
H
3. Click th
e
you can us
Immediate
lick the check
f
ement of the
s
You just creat
e
a
les for the pre
u
st introduced
PREVIOUSQU
A
n
up to 64 nes
t
m
ula would be
m
ula, you also
u
e
nt, which is ac

t
u
used the CAL
C
d
create more
c
outside the sc
o
n
order to use
T
M
ark as Date T
a
te table is sele
c
e asking: ‘Wha
nd, that is exa
c
ds to filter and
o
u saw earlier i
hat aggregate
u
m feature.
d
it Task: Creat
e
a

ctSales table,
mn.
H
ome tab, on
t
e
down arrow
n
e, too).
ly, a new mea
s
f
ormula butto
n
s
yntax.
e
d a measure
u
vious quarter,
d
to several im
p
A
RTER functio
n
t
ed functions. I
very difficult t
o

u
sed filters. Filt
t
ually another
f
C
ULATE functi
o
c
omplex form
u
o
pe of this Qu
i
T
ime Intellige
n
a
ble dialog bo
x
c
ted as the uni
t is the simple
s
c
tly what you c
a
calculate on a
s used to add
values as well.

e
a measure f
o
scroll to the R
e
t
he ribbon, clic
k

n
ext to AutoSu
s
ure is created
w
n
on the formul
u
sing DAX, and
d
epending on
p
ortant aspects
n
is nested as a
t is unlikely a f
o
o
create and d
e
ers narrow do

w
f
unction. You
w
o
n. This is one
u
las, you will li
k
i
ckStart, but as
n
ce functions i
n
x
. In the Conto
que date colu
m
s
t DAX formul
a
a
n do by usin
g
ggregated dat
up all the num
You can auto
m
o
rmula by usi

n
e
turnQuantit
y
k
the Measur
e
m, and then cl
w
ith the name
a bar to valida
t
not an easy o
n
the filters appl
of DAX formu
n argument p
a
o
rmula would
e
e
bug, and it pr
o
w
n what will b
e
w
ill learn more
of the most p

o
k
ely use this fu
n
your knowled
g
n
DAX formula
s
so DAX Formu
m
n.
a
I can create?’
g
a standard ag
a. For example
bers in a parti
c
m
atically creat
e
n
g the AutoS
u
y
column, and
t
e
s button.

ick Average (
n
A
verage o
f
t
e the formula
.
n
e at that. Wh
a
ied in a PivotT
a
las. First, this f
o
a
ssed to the C
A
e
ver contain s
o
o
bably would
n
e
calculated. In
about filters la
t
o
werful functio

n
n
ction many ti
m
g
e of DAX gro
w
s
, you must sp
e
la Samples.xls
x
Well, the ans
w
gregation fun
c
, the SUM fun
c
c
ular column.
D
e
formulas usin
u
m feature.
t
hen click on t
h
n
otice the othe

r
f
ReturnQ
u
.
If you get an
e
a
t this formula
a
ble or PivotC
h
o
rmula include
A
LCULATE func
t
o
many nested
n
’t be very fast
e
this case, you
t
er.
n
s in DAX. As
y
m
es. Discussin

g
w
s, pay special
e
cify a unique
d
x
workbook, th
e
w
er to that is ‘t
h
c
tion in a meas
c
tion in the Su
m
D
AX includes s
e
g standard ag
g
h
e column hea
d
r
standard agg
u
antity: foll
e

rror, verify ea
c
will do is calcu
h
art.
d two function
t
ion. DAX for
m
functions. In f
a
e
ither.
selected one fi
y
ou author dat
a
g
the CALCUL
A
attention to t
h
d
ate column b
y
e
DateKey col
u
h
e formula you

ure. Almost an
m
of Sales Am
o
e
veral other
g
regations by
u
d
er to select th
regation funct
i
owed by the
c
h
late
s.
m
ulas
a
ct,
lter as
a

A
TE
h
is
y


u
mn in
don’t
y data
o
unt
u
sing
e
i
ons
formula =AVERAGE([ReturnQuantity]).
Now wasn’t that easy? Of course, not all formulas you create will be so simple. But, by using the AutoSum feature, you
can create quick and easy formulas by using standard aggregation calculations.
This should give you a fairly good understanding of syntax used in DAX formulas. You were also introduced to some
really cool features like IntelliSense and AutoSum to help you create quick, easy, and accurate formulas. Of course
there is a lot more you can learn about syntax. A good place to learn more is the DAX Reference in PowerPivot Help
or SQL Books Online.
Syntax QuickQuiz
1. What does this button on the formula bar do?

2. What always surrounds a column name in a DAX formula?
3. How would you write a formula for the following:
In the DimProduct table, for each row in the UnitMargin calculated column, calculate a value by subtracting
values in the UnitCost column from values in the UnitPrice column?
Answers are provided at the end of this topic.
Functions
Functions are predefined formulas that perform calculations by using specific values, called arguments, in a particular
order or structure. Arguments can be other functions, another formula, column references, numbers, text, logical

values such as TRUE or FALSE, or constants.
DAX includes the following categories of functions: Date and Time, Information, Logical, Mathematical, Statistical, Text,
and Time Intelligence Functions. If you are familiar with functions in Excel formulas, many of the functions in DAX will
appear similar to you; however, DAX functions are unique in the following ways:
 A DAX function always references a complete column or a table. If you want to use only particular values
from a table or column, you can add filters to the formula.
 If you need to customize calculations on a row-by-row basis, DAX provides functions that let you use the
current row value or a related value as a kind of argument, to perform calculations that vary by context. You
will learn more about context later.
 DAX includes many functions that return a table rather than a value. The table is not displayed, but is used to
provide input to other functions. For example, you can retrieve a table and then count the distinct values in
it, or calculate dynamic sums across filtered tables or columns.
 DAX includes a variety of time intelligence functions. These functions let you define or select date ranges,
and perform dynamic calculations based on them. For example, you can compare sums across parallel
periods.
Sometimes it is difficult to know which functions you might need to use in a formula. PowerPivot, and the tabular
model designer in SQL Server Data Tools, include the Insert Function feature, a dialog box that helps you select
functions by category and provides short descriptions for each function.


Let’s create a new formula which includes a function you will select by using the Insert Function feature:
Task: Add a function to a formula by using Insert Function

In the FactSales table, scroll to the right-most column, and then in the column header, click Add Column.
1. In the formula bar, type an equals sign, =.
2. Click the Insert Function button. This opens the Insert Function dialog box.
3. In the Insert Function dialog box, click the Select a category list box. By default, All is selected, and all of the
functions in the All category are listed below. That’s a lot of functions, so you will want to filter the functions
to make it easier to locate the type of function you are looking for.
4. For this formula, you want to return some data that already exists in another table. For that, you are going to

use a function in the Filter category. Go ahead and click the Filter category, and then in Select a function,
scroll down and double-click the RELATED function. Click Ok to close the Insert Function dialog box.
5. Use IntelliSense to help you find and select the DimChannel[ChannelName] column.
6. Close the formula and then press Enter.
7. After you press Enter to complete the formula, the word Calculating appears in the status bar along the
bottom of the PowerPivot window. Now you will see that you just created a new column in the FactSales
table with channel information from the DimChannel table.
8. Rename the column
Channel.
Your formula should look like this:
=RELATED(DimChannel[ChannelName])
You were just introduced to another very important function in DAX, the RELATED function. The RELATED function
returns values from another table. You can use RELATED provided there is a relationship between the table you are
currently in and the table that contains the values you want to get. Of course, the RELATED function has immense
possibilities. In this case, you can now include the sales channel for each sale in the FactSales table. You can now hide
the DimChannel table from the PivotTable field list, making it easier to navigate and see only the most important
information you really need. Much like the CALCULATE function described earlier, the RELATED function is very
important, and you will likely use it many times.
As you can see, functions in DAX can help you create very powerful formulas. We really only touched on the basics of
functions. As your DAX skills improve, you will create formulas using many different functions. One of the best places
to learn details about all of the DAX functions is in the DAX Function Reference on MSDN.
Functions QuickQuiz
1. What does a function always reference?
2. Can a formula contain more than one function?
3. What category of functions would you use to concatenate two text strings into one string?
Answers are provided at the end of this topic.
Context
Context is one of the most important DAX concepts to understand. There are two types of context in DAX; row context
and filter context. We will first look at row context.
Row Context

Row context is most easily thought of as the current row. For example, remember the Margin calculated column you
saw earlier when learning about syntax? The formula =[SalesAmount] - [TotalCost] calculates a value in the Margin
column for each row in the table. Values for each row are calculated from values in two other columns, [SalesAmount]
and [TotalCost] in the same row. DAX can calculate the values for each row in the Margin column because it has the
context: For each row, it takes values in the [TotalCost] column and subtracts them from values in the [SalesAmount]
column.
In the selected cell shown below, the value, $49.54 in the current row was calculated by subtracting the value $51.54
in the [TotalCost] column from the value $101.08 in the [SalesAmount] column.

Row context doesn’t just apply to calculated columns. Row context also applies whenever a formula has a function
that applies filters to identify a single row in a table. The function will inherently apply a row context for each row of
the table over which it is filtering. This type of row context most often applies to measures.
Filter Context
Filter context is a little more difficult to understand than row context. You can most easily think of filter context as:
One or more filters applied in a calculation that determines a result or value.
Filter context does not exist in place of row context; rather, it applies in addition to row context. For example, to
further narrow down the values to include in a calculation, you can apply a filter context which not only specifies the
row context, but also specifies only a particular value (filter) in that row context.
Filter context is easily seen in PivotTables. For example, when you add TotalCost to the Values area, and then add Year
and Region to the Row or Columns, you are defining a filter context that selects a subset of data based on a given
year and region.
Why is filter context so important to DAX? Because, while filter context can most easily be applied by adding column
and row labels and slicers in a PivotTable, filter context can also be applied in a DAX formula by defining a filter using
functions such as ALL, RELATED, FILTER, CALCULATE, by relationships, and by other measures and columns. For
example, let’s look at the following formula in a measure named StoreSales:

Clearly this formula is more complex than some of the other formulas you’ve seen. However, to better understand this
formula, we can break it down, much like we’ve done with other formulas.
This formula includes the following syntax elements:
A. The measure name StoreSales, followed by a colon :.

B. The equals sign operator (
=) indicates the beginning of the formula.
C. The
CALCULATE function evaluates an expression, as an argument, in a context that is modified by the
specified filters.
D. Parenthesis
() surround one or more arguments.
E. A measure
[Sales] in the same table as an expression. The Sales measure has the formula:
=SUM(FactSales[SalesAmount]).
F. A comma (
,) separates each filter.
G. The referenced column and a particular value,
DimChannel[ChannelName] =”Store”, as a filter.
This formula will ensure only sales values, defined by the Sales measure, as a filter, are calculated only for rows in the
DimChannel[ChannelName] column with the value “Store”, as a filter.
As you can imagine, being able to define filter context within a formula has immense and powerful capability. Being
able to reference only a particular value in a related table is just one such example. Don’t worry if you do not
completely understand context right away. As you create your own formulas, you will better understand context and
why it is so important in DAX.
Context QuickQuiz
1. What are the two types of context?
2. What is filter context?
3. What is row context?
Answers are provided at the end of this topic.
Summary
Now that you have a basic understanding of the most important concepts in DAX, you can begin creating DAX
formulas for calculated columns and measures on your own. DAX can indeed be a little tricky to learn, but there are
many resources available to you. After reading through this topic a few times, and experimenting with a few of your
own formulas, you can learn more about other DAX concepts and formulas that can help you solve your own business

problems. There are many DAX resources available to you in PowerPivot Help, SQL Server Books Online, whitepapers,
and blogs from both Microsoft and leading BI professionals. The DAX Resource Center Wiki at
is a great place to start. The Data
Analysis Expressions (DAX) Reference on MSDN is also a great resource. Be sure to save it in your Favorites.
The DAX in the BI Tabular Model whitepaper, available for download at
a more detailed look at the concepts
introduced here as well as many other advanced concepts and formulas. This whitepaper also uses the same Contoso
DAX Sample Formulas.xlsx workbook you already have.
QuickQuiz Answers
Syntax:
1. Opens the Insert Function feature.
2. Brackets [].
3. =[UnitPrice] - [UnitCost]
Functions:
1. A table and a column.
2. Yes. A formula can contain up to 64 nested functions.
3. Text.
Context:
1. Row context and filter context.
2. One or more filters in a calculation that determines a single value.
3. The current row.

×