Implementing Common Business Calculations in DAX - Chris Webb

download Implementing Common Business Calculations in DAX - Chris Webb

of 34

Transcript of Implementing Common Business Calculations in DAX - Chris Webb

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    1/34

    Implementing Common

    Business Calculations in DAXChris Webb

    [email protected] www.crossjoin.co.uk

    mailto:[email protected]:[email protected]

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    2/34

    DAX Calculations

    Who am I?• Chris Webb ( [email protected] )• In epen ent consultant specialising in

    Anal!sis "er#ices an $DX (an %o&er%i#ot

    an DAX') http &&&.crossjoin.co.uk• Blogger http c&ebbbi.spaces.li#e.com • Author

    $DX "olutions *n

    + ition – +,pert Cube De#elopment With Anal!sis"er#ices *--

    mailto:[email protected]://www.crossjoin.co.uk/http://cwebbbi.spaces.live.com/http://cwebbbi.spaces.live.com/http://www.crossjoin.co.uk/mailto:[email protected]

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    3/34

    DAX Calculations

    Agen a• What is DAX an &h! shoul I learn

    it?• Calculate columns an calculate

    measures• /o& conte,t an 0lter conte,t•

    Calculate()• 1alues()• Demos2 emos2 emos'

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    4/34

    DAX Calculations

    What is DAX?• DAX is the ne& calculation language 3or

    %o&er%i#ot• $DX is still the 4uer! language

    • It is a multi imensional calculation languagelike $DX2 but the esign goals &ere• $ake it eas! to o common calculations (easier

    than $DX)•

    $ake it eas! to use 3or +,cel po&er users2 hencethe +,cel5base s!nta,• DAX e,pressions are limite to a single

    (though o3ten #er! long) line o3 co e

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    5/34

    DAX Calculations

    Wh! learn DAX?• 6our po&er users &ill be learning it2 an !ou7ll nee to

    un erstan &hat7s going on in their %o&er%i#otmo els

    • It7s #er! likel! that it &ill be use in 3uture #ersions o3""A" 8 possibl! as an alternati#e to $DX 3or somecalculations

    • We love both DAX and MDX, and MDX is not going away. Thechallenge for us is to bring them together. We have some

    promising directions. That said – thin! you"ll see DAX evolving in

    new important directions that MDX will never cover, and a largeand growing portion of the calc wor! will be done in DAX. #o myadvice stands$ all you guys need to become DAX gurus

    ASAP .Amir Netz, Microsoft

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    6/34

    DAX Calculations

    DAX "!nta, an 9unctions• DAX s!nta, is base on +,cel s!nta,

    • :nl! thing to &atch out 3or is !ou nee to use ;; an I 3or e iting DAX is terrible 8 can use

    =ote%a an Colin Ban0el 7s DAX languagetemplate instea

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    7/34DAX Calculations

    /e3erencing Columns an $easures

    • DAX usuall! re4uires 3ull! 4uali0enames2 eg• $! able $!Column• $! able $!$easure

    • 6ou can use un4uali0e names incalculate column e0nitions2 &henre3erring to other columns in thesame table

    • DAX cannot use stan ar +,cel cellre3erences to ata else&here in the

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    8/34DAX Calculations

    Calculate Columns• DAX can be use to create t&o t!pes o3

    calculation calculate columns an calculatemeasures

    • Calculate columns are eri#e columns in a%o&er%i#ot table• Are e0ne insi e the %o&er%i#ot >I• A3ter the! are create 2 the! beha#e like an! other

    column• heir #alues are calculate imme iatel! a3ter the real

    ata has been loa e into %o&er%i#ot• Can be use to o basic + &ork2 eg concatenating

    0rst an last names2 eri#ing !ears 3rom ates etc

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    9/34DAX Calculations

    Calculate $easures• Calculate measures pro#i e the numeric

    #alues !ou see aggregate in pi#ot tables• All measures are calculate measures in

    %o&er%i#ot'• Are e0ne &ithin the pi#ot table in +,cel• Are calculate at 4uer! time

    • Basic sum2 count2 min2 ma, an a#eragecalculate measures can be create 3rom theright5click menu

    • $ore a #ance calculate measures can becreate b! entering !our o&n DAX e,pression

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    10/34DAX Calculations

    /o& Conte,t an 9ilterConte,t

    • /o& conte,t re3ers to the current ro& &here acalculation is taking place• here is no ro& conte,t &ithout a DAX aggregation 3unction

    • 9ilter conte,t re3ers to the currentl! selecte item oneach column on each table• 1er! similar to the Current$ember 3unction in $DX2 but

    han les multiselect elegantl!• 9ilter conte,t 3ollo&s the one5to5man! relationships

    bet&een tables• Both ro& an 0lter conte,t must be taken into

    account &hen &riting e,pressions 3or calculatemeasures

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    11/34

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    12/34

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    13/34

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    14/34

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    15/34DAX Calculations

    /o& Conte,t &ithCalculation

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    16/34DAX Calculations

    /o& Conte,t &ithCalculation

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    17/34DAX Calculations

    Calculate()• he Calculate() 3unction is the ke! to all

    a #ance DAX calculation 3unctionalit!• "ignature Calculate(+,pression2 "et9ilterE2

    "et9ilter*2...)• It allo&s an! e,pression to be e#aluate in aspeci0c 0lter conte,t

    • It &orks as 3ollo&s•

    $o i0es the current 0lter conte,t accor ing to the"et9ilter arguments !ou pass in• "hi3ts the ro& conte,t onto the 0lter conte,t• +#aluates !our e,pression in the ne& 0lter conte,t

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    18/34DAX Calculations

    Calculate()• "et9ilter arguments can either be

    • Boolean e,pressions2 egCA C> A +(C:>= /:W"()2 Consultants "kill FG$DXG)

    • able 3unctions2 so the 0lter conte,t 3or an! table isset to the ro&s returne b! the table 3unction2 egCA C> A +(C:>= /:W"()2 9I +/(Consultants2Consultants $easureE H ))

    • >se3ul table 3unctions inclu e•

    1alues()2 &hich returns a list o3 istinct #alues in acolumn in the current 0lter conte,t• All()2 &hich returns a list o3 all the #alues in a column

    ignoring 0lter conte,t

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    19/34

    DAX Calculations

    /atio to All an /atio to%arent

    • A common e,ample o3 a Jratio to all7 an Jratio to parent7calculations is a market shares

    • he numerator 3or this kin o3 calculation is eas!">$(KConsultantsK $easureE )

    • he enominator is the Jall7 or Jparent7 total• o get the total #alue o3 a measure &ith all 0lter conte,t

    cleare &e nee to use All( able)2 egCA C> A +(">$(KConsultantsK $easureE )2A (Consultants))

    • o get the total #alue o3 a measure &ith 0lter conte,t 3or just one column remo#e 2 &e nee All(Column)2 egCA C> A +(">$(KConsultantsK $easureE )2A (Consultants Consultant ))

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    20/34

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    21/34

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    22/34

    DAX Calculations

    %re#ious 6ear Lro&th• he 3ollo&ing is the best approach• FCA C> A +(">$(9actInternet"ales "alesAmount )

    2 DA +ADD(DimDate 9ullDateAlternate e! 2 5E2 6+A/)2 A (DimDate))

    • Works b!• 9in ing all the ates in the current !ear• "hi3ting each ate back one !ear• "etting this as the 0lter conte,t•

    "umming "alesAmount 3or these ates• B> &ill onl! gi#e correct results i3 !ou ha#e a

    complete set o3 ates in !our !ear'

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    23/34

    DAX Calculations

    %re#ious 6ear Lro&th• Al&a!s a goo i ea in DAX to break comple,

    calculations up into a series o3 simpler calculatemeasures

    • "o2 i3 &e use the pre#ious 3ormula to e0ne acalculate measure calle %re#ious 6ear "ales2 thegro&th calculation becomes

    • FI9(9actInternet"ales %re#ious 6ear "ales F-2 B A= ()

    • 2(">$(9actInternet"ales "alesAmount ) 59actInternet"ales %re#ious 6ear "ales ) 9actInternet"ales %re#ious 6ear "ales )

    • =otice2 like in $DX2 &e nee to trap i#ision b! Mero

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    24/34

    DAX Calculations

    /ules 3or ime Intelligence 3unctions

    • 9i#e rules 3or using ime Intelligence 3unctionsE. =e#er use the atetime column 3rom the 3act table in

    time 3unctions.*. Al&a!s create a separate ime table2 an make sure

    it contains complete !ears. Create relationships bet&een 3act tables an the

    ime table.N. $ake sure that relationships are base on a atetime

    column (an =: base on another arti0cial ke!column).O. he atetime column in the ime table shoul be at

    a! granularit! (&ithout 3ractions o3 a a!).

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    25/34

    DAX Calculations

    I3 !ou on7t 3ollo& therules...

    • As &e7#e alrea ! seen2 not ha#ing complete!ears makes it har to o relati#e timecalculations

    •In m! e,ample2 since I joine on a surrogateke! an not a Date ime ke!2 I nee e to aAll(DimDate) to m! calculation

    • 6ou can7t use /+ A +D() to bring the ate

    o&n to the 3act table either2 because thiscauses a circular re3erence error'• =ee to either alter the un erl!ing relational table

    or #ie&2 or import the ate imension table t&ice

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    26/34

    DAX Calculations

    6ear to Date• uckil! there are man! built5in time

    intelligence 3unctions 3or common calculations• +g 3or oing !ear5to5 ate sums2 &e ha#e

    otal6 D• F : A 6 D(">$(9actInternet"ales "alesAmount )2DimDate 9ullDateAlternate e!

    2 A (DimDate))• In this case2 otal6 D is a #ariant o3 Calculate&ith some 0lters set automaticall!

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    27/34

    DAX Calculations

    otal to Date• A total5to5 ate gets the running total 3rom

    the 0rst ate &e ha#e in our ata• =o built5in 3unction 3or it

    • We nee to use Calculate an set the 0lterto all ates 3rom the 0rst e#er ate to thelast ate in the current conte,t• We can use DA +"B+ W++= to get this ate

    range• he 0rst ate can be got b! using B A= ()• he last ate &ith the A" DA +() 3unction

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    28/34

    DAX Calculations

    otal to Date• he 0nal #ersion is

    FCA C> A +( ">$(9actInternet"ales "alesAmount )

    2DA +"B+ W++=(KDimDateK 9ullDateAlternate e!

    2 B A= ()2A" DA +( KDimDateK 9ullDateAlternate e! ))2 All(KDimDateK))

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    29/34

    DAX Calculations

    1alues() 8 like Current$ember butbetter'

    • he 1alues() 3unction acts like the $DXCurrent$ember 3unction

    • But it is better 1alues() returns a table2 so ithan les multiselect• Although &hen the table it returns onl! contains one

    ro& &e can still o a irect comparison &ith another#alue

    • he Distinct() 3unction &orks e,actl! the same

    &a! as 1alues() but• 1alues() &ill return the >nkno&n $ember• Distinct() &ill not

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    30/34

    DAX Calculations

    Distinct Count• o 0n a istinct count2 &e nee to

    count the ro&s in a table containingonl! the istinct #alues 3rom acolumn

    • he 1alues() 3unction returns such atable

    • Because it7s a table2 all &e nee too is count the number o3 ro&s in it

    • FC:>= /:W"(1A >+"(Consultants"kill ))

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    31/34

    DAX Calculations

    9inal houghts• DAX oes the eas! stuP #er! easil!• DAX oes the me ium5har stuP &ell too

    • =ot sure i3 po&er users &ill get it2 but• It7s easier than $DX• It7s more "Q 5like2 so more e#elopers &ill get it• It7s more elegant than $DX in man! &a!s2 eg

    multiselect• DAX oes not o the reall! har stuP &ell at all

    • "o 3or 0nancial apps2 3or e,ample2 $DX still &ins• But 3or ho& long?

    • I like DAX a lot

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    32/34

    DAX Calculations

    inks• http blogs.ms n.com po&erpi#ot• http &&&.po&erpi#ot.com• http po&erpi#otgeek.com•

    http po&erpi#ott&ins.com• http po&erpi#otpro.com• http &&&.po&erpi#ot5in3o.com• http c&ebbbi.spaces.li#e.com• http social.ms n.microso3t.com 9orums en5>"

    s4lkjpo&erpi#ot3ore,cel threa s• http &&&.business5intelligence.k ejonge.net

    • http s4lblog.com blogs marcoRrusso

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    33/34

    hanks'

  • 8/9/2019 Implementing Common Business Calculations in DAX - Chris Webb

    34/34

    Coming upS% X--EHow to Get Full Access to a Database Backup in 3 Minutesor Lessdera% --E

    En !to!en atabase e"elopment #as arri"e%ed &ate% --*$eir , Deforme , an Grotes%ue &Horrors 'tories from t#e$orl of ()'uest

    % --OE*pert +uer Anal sis wit# '+L 'entr#'(#entry % --TAttunit -#an.e Data -apture for ''('

    Attunity U"Q BI"