Power BI DAX Tutorial for Beginners
What is DAX?
DAXorDataAnalysisExpressionsdriveallthecalculationsyoucanperforminPower BI.DAXformulasareversatile,dynamic,andverypowerful—theyallowyoutocreate newfieldsandevennewtablesinyourmodel.WhileDAXismostcommonly associatedwithPowerBI,youcanalsofindDAXformulasinPowerPivotinExceland SQLServerAnalysisServices(SSAS).
DAXformulasaremadeupof3corecomponentsandthistutorialwillcovereachof these: Syntax—ProperDAXsyntaxismadeupofavarietyofelements,someofwhichare commontoallformulas.
Functions DAXfunctionsarepredefinedformulasthattakesomeparameters andperformaspecificcalculation.

Context DAXusescontexttodeterminewhichrowsshouldbeusedtoperforma calculation.
Why is DAX Important in Power BI?
DAXformulasallowyoutogetthemostoutofyourdataandPowerBIinordertosolve businessproblemsefficiently.Youcanperformsimplecalculations(suchasasimple sumoraverage)andcreatemostvisualswithouteventouchingDAX.Forexample,if youwantedtocreateasimplechartshowingtotalprofityoucouldsimplydragthe

profitfieldontotheValuessectionofthechart,anditwouldperformasumofthe rowsinthatfield.
TherearetwocaseswhereitwouldbebettertocreateaDAXformula:
1.Ifyouwantedtore useaformulainmultipleplaces,suchasinmultiplechartsor asanexpressioninotherDAXformulas.Inthiscase,usingaDAXformulawould makeyourreportmoreefficientandeasiertochangeinthefuturesinceyouwould onlyneedtochangeasingleformularatherthanchangingmanyindividual formulasineachplacetheyareused.
2.IfyouwantedtocreatecomplexorcustomizedformulaswherejustasimpleSUM orAVERAGEwouldnotbesufficientforthebusinessproblemyouweretryingto solve.
Where are DAX Formulas Used in Power BI?
TherearethreewaysyoucanuseDAXformulasinPowerBI:
1.CalculatedTables
Thesecalculationswilladdanadditionaltabletothereport basedonaformula.
2.CalculatedColumns—Thesecalculationswilladdanadditionalcolumntoatable basedonaformula.Thesecolumnsaretreatedlikeanyotherfieldinthetable.
3.Measures Thesecalculationswilladdasummaryoraggregatedmeasuretoa tablebasedonaformula.

Themaindifferencebetweenthesethreetypesofcalculationsisintheircontext(more onthislater)andtheoutputstheyproduce.
Toaddanyoneofthesetypesofcalculationstoamodel,navigatetotheModelingtab oftheribbon.Hereyouwillfindthreechoicesforaddingeitheranewmeasure, calculatedcolumn,ortable.Alternatively,youcanright clickatableintheFieldspane, andyouwillgettheoptiontoaddanewmeasureorcalculatedcolumninthedrop downmenu.



oftenusefultohavemultiplenestedIFstatementsortousetheIFERRORfunctionto wraparoundanotherfunction,sothatanyerrorsintheformulaarerepresentedbythe valueyouspecify.
SomeofthemostcommonDAXfunctionsusedinreportsare:
1.Simplecalculations:COUNT,DISTINCTCOUNT,SUM,AVERAGE,MIN,MAX.
2.SUMMARISE:Returnsatabletypicallyusedtofurtherapplyaggregationsover differentgroupings.
3.CALCULATE:Performsanaggregationalongwithoneormorefilters.Whenyou specifymorethanonefilter,thefunctionwillperformthecalculationwhereall filtersaretrue.
4.IF:Basedonalogicalcondition,itwillreturnadifferentvalueforifitistrueor false.ThisissimilartotheCASEWHENoperationinSQL.

5.IFERROR:Looksforanyerrorsforaninnerfunctionandreturnsaspecifiedresult
6.ISBLANK:Checksiftherowsinacolumnareblankandreturnstrueorfalse. UsefultouseinconjunctionwithotherfunctionslikeIF.
7.EOMONTH:Returnsthelastdayofthemonthofagivendate(columnreferencein adateformat)forasmanymonthsinthepastorthefuture.
8.DATEDIFF:returnsthedifferencebetween2dates(bothascolumnreferencesin dateformats)indays,months,quarters,years,etc.
Understanding Context in DAX Formulas
DAXformulasinPowerBIaredynamic,andtheychangeaccordingtothecontextin whichtheywerecreated.It’simportanttohaveanunderstandingofhowcontextswork inDAX,asitcanhelpsaveyoualotofheadacheswhenyourunintoconfusingerrors inyourformulas.
TherearetwomaintypesofcontextinDAX:rowcontextandfiltercontext.
Row Context
Thisreferstojust“thecurrentrow”acrossallcolumnsofatableandalsoextendstoall columnsinrelatedtablestoo.ThistypeofcontextletstheDAXformulaknowwhich rowstouseforaspecificformula.
Hereisanexampleofaformulaforacalculatedcolumnthathasarowcontext:
Cost Price Per Unit =
Inthisexample,thecalculationofCostPricePerUnitisdoneonarowbyrowbasis. ThismeansthatDAXneedstoknowwhatthecurrentrowisasitproceedsthroughthe dataset,makingthecalculationandpopulatingthenewcolumnwiththeresult.

Rowcontextisimplicitincalculatedcolumns.Thisisbecausethecalculationsthatare performedincalculatedcolumnsaredoneonarowbyrowbasis,andthustherow contextisdefinedbydefault.However,thisisnotthecaseinmeasuressincethe aggregationsareappliedforallrowsinatable.Thesecalculationsdonotneedtohave anyknowledgeofacurrentrowsinceallrowsareaggregatedtogether.
Asanexampleofameasure,considerthefollowingDAXformula:
Profit margin = SUM (
Inthiscase,theentireProfitcolumnissummedtoproduceasinglenumber,andthis isdividedbythesumoftheentireSalescolumn.DAXdoesnotneedtoknowthe

currentrow,sinceitisperforminganaggregation.Thus,thismeasurehasnorow context.
Toexplicitlydefinearowcontextinameasure,youneedtouseaspecialfunction calledaniterator.ExamplesofiteratorfunctionsareSUMX,AVERAGEX,andCOUNTX. Thesefunctionswillfirstperformacalculationonarowbyrowbasis,andthen performthefinalaggregationontheresult(ie.sum,average,count,etc.).Inthisway, therowcontextisdefinedexplicitlybytheuseoftheseiterators.
Let’stakealookatanexampleofaniteratorfunctioninaction:
Average Cost Per Unit = AVERAGEX ( financials, financials[COGS] / financials[Units Sold] )
Thisexampleperformstwocalculations:first,theexpressionisevaluatedonarowby rowbasis,andthentheresultisappliedtotheAVERAGEfunction.Analternativeway ofreachingthissameresultistofirstcreatethecalculatedcolumn‘CostPricePerUnit’ aswedidaboveandthencreateaseparateAVERAGEmeasureofthatcolumn. However,knowingwhentousetheseiteratorfunctionscanmakeyourreportsmore efficientanduseuplessmemoryasyouareeffectivelyabletoperformtwo calculationsusingjustasingleformula.
Filter Context
Filtercontextisappliedontopofarowcontextandreferstoasubsetofrowsor columnsthatarespecifiedasfiltersinthereport.Filterscanbeappliedinafewways:
DirectlyinaDAXformula
Throughthefieldsthatmakeupavisual(suchastherowsandcolumnsina matrix)
AgoodexampleofhowtoaddafiltercontexttoaDAXformulaistousethe CALCULATEfunction,whichallowsyoutoaddoneormorefilterparameterstothe

