Getting Started with MDX 20140625a

75
Getting Started with MDX Ron Moore Topdown Consulting, Inc. and Monica Christie Camuto Group

Transcript of Getting Started with MDX 20140625a

Page 1: Getting Started with MDX 20140625a

Getting Started with MDX

Ron Moore

Topdown Consulting, Inc.

and

Monica Christie

Camuto Group

Page 2: Getting Started with MDX 20140625a

About Topdown consulting

Page 3: Getting Started with MDX 20140625a

� Objective is to get you writing code fast

� Quick intro - A LOT more to learn

� Three sections

1. Queries

2. Basic member formulas

3. Beyond basics

� You should already understand Essbase Outlines and

multidimensional concepts

� Exercises use (modified) ASOSamp Application

� One hierarchy has answers and one for your work

About the Workshop

Page 4: Getting Started with MDX 20140625a

Where is MDX used?

� Queries for ASO and BSO

� ASO member formulas and stored calcs

� Smart View / Smart Query

� Embedded in MaxL

� Triggers, ASO Clear regions2.

Page 5: Getting Started with MDX 20140625a

1 - Queries

MDX SCRIPT EDITOR

MDX Script Editor

Page 6: Getting Started with MDX 20140625a

Syntax basics and concepts

1 - Queries

Page 7: Getting Started with MDX 20140625a

Every number lives in an intersection

Every intersection has a name2

One member name (coordinate) from each

(stored) dimension

Rule #1 of Multi-D databases

Page 8: Getting Started with MDX 20140625a

SELECT {Sales} ON COLUMNS

FROM [sample.basic]

{Year} ON ROWS

,

WHERE (East,Actual)

{[100]} ON AXIS (2)

,

A Simple Example 1 - Queries

SELECT SELECT

FROM [sample.basic]

Page 9: Getting Started with MDX 20140625a

Specifying Member Names

� East or [2014] or [Gross Profit]

� Markets.East or [Markets].[East]

� Member only or Dimension.Member

● Dimension.member is best practice

● Dimension is required if it’s ambiguous

� With or without square brackets

● Member name begins with a character other than a letter

● Member name has spaces

● Member name is also an MDX key word

Marketing Technologies Group | www.mtgny.com

Page 10: Getting Started with MDX 20140625a

Understanding Tuples and Sets

1 - Queries

Page 11: Getting Started with MDX 20140625a

Sets are Multiple Members from One Dimension

SELECT { [Sales], [Profit] } ON COLUMNS,

{ [Qtr1], [Qtr2], [Qtr3], [Qtr4] } ON ROWS,

{ [Colas], [Root Beer], [Cream Soda] } ON AXIS(2)

FROM [sample.basic]

where ([East],[Actual])

Marketing Technologies Group | www.mtgny.com

A Set is:

• One or more members from the

same dimension

• Enclosed in braces

• Separated by commas

1 - Queries

Page 12: Getting Started with MDX 20140625a

Tuples Specify Intersections

SELECT { [Dec] , [Jan] } ON COLUMNS,

{ [Units],[Transactions] } ON ROWS

From [ASOSamp.Sample]

([Prev Year], ) ([Curr Year], )

Dec Jan

Units #Missing 42,228

Transactions #Missing 44,500

1 - Queries

Page 13: Getting Started with MDX 20140625a

Understanding Tuples

( [Actual],[Sales] )

( [Sales] )

( [Jan],[Actual],[Sales] )

1 - Queries

Page 14: Getting Started with MDX 20140625a

Understanding Tuples

� Collection of member names separated by

commas, enclosed in parentheses

� INTERSECTION! - No more than one member

from each dimension

� Omit dimensions to include all elements of that

dimension

� A tuple that specifies one member name from

each dimension is a single cell

Marketing Technologies Group | www.mtgny.com

1 - Queries

Page 15: Getting Started with MDX 20140625a

Understanding Sets{ ( [Sales] ),

( [Prof % ]) }

{ ( [Sales], [P1] ),

([Prof % ], [P1] ) }

{ ( [Sales], [P1], [Jan] ),

( [Prof % ], [P1],[Jan] ) }

Marketing Technologies Group | www.mtgny.com

1 - Queries

Page 16: Getting Started with MDX 20140625a

Understanding Sets

� Sets are collections of tuples separated

by commas, enclosed in braces { }

� Tuples within a set must have the same

dimensions in the same order

Marketing Technologies Group | www.mtgny.com

1 - Queries

Page 17: Getting Started with MDX 20140625a

Syntax Review

� Select - From - Where query structure

� Specifying Member Names

● MemberName or [Member Name]

● [MemberName] or [Dim].[Member Name]

� Tuples

● ( MemberName1, MemberName 2 )

● Only one member from each dimension

● Includes all members from missing dimensions

� Sets

● { (Tuple1), (Tuple2)2}

● Tuples must have same dims in same order

1 - Queries

Page 18: Getting Started with MDX 20140625a

� members in our queries. Sets:

● Member-by-member

● Tuple-by-tuple

So far8 1 - Queries

Now 8

� MDX functions

● Children

● Descendants

● Generations

● Levels

Page 19: Getting Started with MDX 20140625a

Useful Functions 1 - Queries

Page 20: Getting Started with MDX 20140625a

Set of Children

{ [Qtr1].Children }

Or

{ Children([Qtr1]) }

Time

MTD

1st Half

Qtr1

Jan

Feb

Mar

Qtr2

Apr

May

Jun

1 - Queries

Page 21: Getting Started with MDX 20140625a

Set of Descendants

Syntax

Descendants ( member , [{ layer | index }[, Desc_flags ]])Example

Descendants ( [N Amer], 2 , After )

� Options

● Layer refers to a generation or level

● Index is n layers down from the member

● Flags specify relationships

•SELF

•AFTER

•BEFORE

•BEFORE_AND_AFTER

•SELF_AND_AFTER

•SELF_AND_BEFORE

•SELF_BEFORE_AFTER

•LEAVES

Marketing Technologies Group | www.mtgny.com

1 - Queries

Page 22: Getting Started with MDX 20140625a

Set of Descendants Part 1 - Queries

Page 23: Getting Started with MDX 20140625a

Marketing Technologies Group | www.mtgny.com

1 - QueriesWhat City?

Page 24: Getting Started with MDX 20140625a

Generations and Levels Functions

� Syntax

● Dimension.Generations(#)

● Dimension.Levels(#)

� Examples

● [Year].Generations(2).members

● [Year].Levels(0).members

Marketing Technologies Group | www.mtgny.com

1 - Queries

Page 25: Getting Started with MDX 20140625a

Generations Property

{[Year].Generations}

{[Year].Generations(2)}

{[Year].Generations(2).Members}

Gen1, Quarters, Months

{Quarters }

{ Qtr1, Qtr2, Qtr3, Qtr4 }

1 - Queries

Page 26: Getting Started with MDX 20140625a

1 - Queries

Select [Time].Levels(0).Members ON COLUMNS,

{ [Products].Generations(2).members} ON ROWS

FROM [KSCOPE14].[SAMPLE]

Select [Time].Levels(index).Members ON COLUMNS,

{ [Products].Generations(index).members} ON ROWS

FROM [KSCOPE14].[SAMPLE]

Generations and Levels Functions

Page 27: Getting Started with MDX 20140625a

Suppress missing rows and/or columns

SELECT NON EMPTY { [Qtr1].Children } ON COLUMNS,

NON EMPTY Descendants([Geography] ,2, After) ON ROWS

FROM [KSCOPE14].[Sample]

Non Empty

Page 28: Getting Started with MDX 20140625a

Exercises

� Exercise

● 1 – My first query

● 2 – Creating intersection

● 3 – Functions: children and levels

● 4 – Functions: descendants and generations

Note: Please use KSCOPE14.sample

1 - Queries

Page 29: Getting Started with MDX 20140625a

2. Basic member formulas

Page 30: Getting Started with MDX 20140625a

Simple Ratios 2 – member formulas

Page 31: Getting Started with MDX 20140625a

Simple Ratios with Relative References

([Jan], [Avg Units / Transaction] ) = ( [Jan], [Units] ) / ( [Jan], [Transactions])

Calculation POV from left side is passed to all terms on the right side

([Feb], [Avg Units / Transaction] ) = ( [Feb], [Units] ) / ( [Feb], [Transactions])

( [Avg Units / Transaction] ) = ( [Units] ) / ( [Transactions])

2 – member formulas

Page 32: Getting Started with MDX 20140625a

Crossing One Dimension

� Price = ( [Markets],[Price] )

Page 33: Getting Started with MDX 20140625a

Fixed (Absolute) References

( Share) = ( Sales) / ([Total Market] , Sales)

(NY, Share) = (NY, Sales) / ([Total Market] , Sales)

(MA,Share) = (MA,Sales) / ([Total Market] , Sales)

(CT, Share) = (CT, Sales) / ([Total Market] , Sales)

The POV from the left side is passed to all terms on the right side

Unless you override it with a member name

2 – member formulas

Page 34: Getting Started with MDX 20140625a

Fixed References on Multiple Dimensions

( Price) = (Market, Product, Price)

(NY, [Diet Cola], Price) = (Market, Product, Price )

(MA, Cola, Price) = (Market, Product, Price)

( CT, Cola, Price) = (Market, Product, Price)

2 – member formulas

Page 35: Getting Started with MDX 20140625a

[Pct of Worldwide Units] = Units / “worldwide units”

[Pct of WW Units ] = Units / ( Geography, Units)

[Pct of All Ages WW Units] = Units / ( [Age Groups],Geography, Units)

Pct of Total

Page 36: Getting Started with MDX 20140625a

Trans Pct of Parent

Relative Reference

[Transactions]/

([Transactions],[Geography].CurrentMember.Parent)

Absolute Reference

[Transactions]/

([Transactions],[Geography].[Connecticut].Parent)

2 – member formulas

Page 37: Getting Started with MDX 20140625a

2 – member formulasTrans Pct of Parent

� CurrentMember.parent = to nesting

functions

� CT/North East

� Sum of North East and South = 1

� Geography 0 because it does not have

a parent

Page 38: Getting Started with MDX 20140625a

2 – member formulasTrans Pct of Region x Prod Fam

Ancestor ( member, layer | index )

� CurrentMember.parent = to nesting

functions

� CT/North East

� Sum of North East and South = 1

� Geography 0 because it does not have

a parent

Page 39: Getting Started with MDX 20140625a

2 – member formulasTrans Pct of Region x Prod Fam

Ancestor ( member, layer | index )

� CurrentMember.parent = to nesting

functions

� CT/North East

� Sum of North East and South = 1

� Geography 0 because it does not have

a parent

Page 40: Getting Started with MDX 20140625a

Bonus material

Page 41: Getting Started with MDX 20140625a

Parents on Two Dimensions

Page 42: Getting Started with MDX 20140625a

Two Dimensions with Parent

[Transactions]/

([Transactions],

[Geography].CurrentMember.Parent,

[Products].CurrentMember.Parent

)

Page 43: Getting Started with MDX 20140625a

Ancestors on Two Dimensions

Page 44: Getting Started with MDX 20140625a

Two Dims with Ancestor

[Transactions]/

([Transactions],

Ancestor ([Geography].CurrentMember,

[Geography].Generations(2)

),

Ancestor ([Products].CurrentMember,

[Products].Generations(2)

)

)

Page 45: Getting Started with MDX 20140625a

Part 3 Beyond Basics

� IIF

� CASE

� Rolling calculations

Page 46: Getting Started with MDX 20140625a

IIF(Conditional test,True logic,False logic )

IIF(

IsUDA([Measures].currentmember, “Expense”),

[Prev Year] – [Curr Year],

[Curr Year] – [Prev Year]

)

IIF

Page 47: Getting Started with MDX 20140625a

CASE

When condition Then logic

Else (optional)

End

CASE

When IS([Time].Currentmember, [Qtr1])

Then [Mar]

End

Case

Page 48: Getting Started with MDX 20140625a

PrevMember and NextMember

Syntax

[Member].PrevMember

Example

[Sales] =

( [Sales],

[Year].CurrentMember.PrevMember

)

* 1.10

Marketing Technologies Group | www.mtgny.com

Page 49: Getting Started with MDX 20140625a

Lead and Lag

Syntax

member.Lag (index [,layertype ] [, hierarchy ] )

Example

[Sales] =

( [Sales],

[Year].CurrentMember.lag(2)

)

* 1.10

Page 50: Getting Started with MDX 20140625a

Rolling Calculations

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

Prev Year 3 4 6 8 9 10 5 5 7 13 9 4

Curr Year 7 3 1 1 6 5 5 - - - - -

One Time Dimension

Two Time Dimensions

Page 51: Getting Started with MDX 20140625a

Functions Used

� Avg ( set [,numeric_value_expression [,IncludeEmpty ] ])

� Sum ( set [,numeric_value_expression ] )

� LastPeriods(numeric_value_expression[,member[,hierarchy]] )

� Count ( set [, IncludeEmpty] )

Page 52: Getting Started with MDX 20140625a

Rolling Calculations Step by Step

1. Create a Simple Average()

2. Create a range with LastPeriods()

3. Make range relative with .CurrentMember

Page 53: Getting Started with MDX 20140625a

Avg ( LastPeriods(6, Jun ) , Units)

Rolling Average Example

Avg ( {Jan : Jun } , Units)

Avg ( LastPeriods(6, [Time].Currentmember ), Units)

Page 54: Getting Started with MDX 20140625a

2D Rolling Calcs

� Number of months needed from previous year changes

each month

� “Count” the months available in the Currentmember

year

● Count(LastPeriods(6,[Time].Currentmember)

� Subtract to get months needed from previous year

● 6 - Count(LastPeriods(6,[Time].Currentmember)

� Use the difference relative to Dec● LastPeriods(

6 - Count(LastPeriods(6,[Time].CurrentMember)),

Dec )

Page 55: Getting Started with MDX 20140625a

2D Rolling Calc Example

Sum(LastPeriods(6,[Time].Currentmember),Units)

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

Previous Year 4 4 8 1 1 8 4 9 9 5 5 4

Current Year 8 8 9 7 2 2 9 10 2 6 7 7

9 5 5 4

+ Sum ( set

[,numeric_value_expression ] )

Months NA This Yr

, Last Year’s Units ), (Units,[Years].Currentmember.Prevmember)

LastPeriods(6 - count(LastPeriods(6,[Time].Currentmember))

, Dec)

Page 56: Getting Started with MDX 20140625a

Thank you for attending

The End

Page 57: Getting Started with MDX 20140625a

Deleted Slides

Page 58: Getting Started with MDX 20140625a

Levels and Generations

� Function form and property form

� Generations and Levels refers to a specific layer number

� Specific generation or levelDimension.Generations ( GenNum )Dimension.Levels ( LevNum )

Marketing Technologies Group | www.mtgny.com

Page 59: Getting Started with MDX 20140625a

A Set of Ancestors

� Ancestors ( member , layer | index )

� e.g. Ancestors ( Jan , 2 )

Marketing Technologies Group | www.mtgny.com

Page 60: Getting Started with MDX 20140625a

Set of Ancestors

,

Page 61: Getting Started with MDX 20140625a

Example:Simple Assignments and Ratios

Profit Pct

Marketing Technologies Group | www.mtgny.comMarketing Technologies Group | www.mtgny.com

Essbase Calc

Language

“Gross Profit” / “Net Sales”;

MDX [Gross profit] / [Net Sales]

Page 62: Getting Started with MDX 20140625a

Example:Inventory Balance Forward

Marketing Technologies Group | www.mtgny.com

Essbase Calc

Language

@Prior(Inventory)

+”Inventory Change”;

MDX ([Measures].[Inventory],

[Year].CurrentMember.PrevMember )

+

( [Measures].[Inventory Change] )

Page 63: Getting Started with MDX 20140625a

Example: Referring to Relatives

Share of Region

Marketing Technologies Group | www.mtgny.com

Essbase

Calc

Units /@Ancestval(“Market Total”, 3, Units );

MDX

[Units] /

( [Units],

Ancestor([Market Total].CurrentMember,

[Market Total].Generations(3) )

)

Page 64: Getting Started with MDX 20140625a

Thank you

� Thanks for joining

� Please email me your comments:

● Your success level

● Speed of presentation

● Quality/Accuracy of documentation

● Effective/ineffective slides and presentations

● Email questions on content and solutions

[email protected] and/or

[email protected]

Page 65: Getting Started with MDX 20140625a

CrossJoin()

� Crossjoin() creates the Cartesian product of two sets

� Crossjoin( { Jan, Feb }, {[2006], [2007]} )

returns

{(Jan,[2006]),(Jan,[2007]),(Feb,[2006]), (Feb, [2007])}

� Notes

● Only 2 sets at time –nest crossjoins for more than 2

● Second set changes fastest

Marketing Technologies Group | www.mtgny.com

Page 66: Getting Started with MDX 20140625a

Descendants Using Layer

Page 67: Getting Started with MDX 20140625a

Layer (Reset layer)QTD YTD

Gen5 Jan Jan Jan

Gen5 Feb Jan+Feb Jan+Feb

Gen5 Mar Jan+Feb+Mar Jan+Feb+Mar

Gen4 Qtr1 Reset

Gen5 Apr Apr Jan+Feb+Mar+Apr

Gen5 May Apr+May Jan+Feb+Mar+Apr+May

Gen5 Jun Apr+May+Jun Jan+Feb+Mar+Apr+May+Jun

Gen4 Qtr2 Reset

Gen3 1st Half

Gen5 Jul Jul Jan+Feb+Mar+Apr+May+Jun+Jul

Gen5 Aug Jul+Aug Jan+Feb+Mar+Apr+May+Jun+Jul+Aug

Gen5 Sep Jul+Aug+Sep Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep

Gen4 Qtr3 Reset

Gen5 Oct Oct Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct

Gen5 Nov Oct+Nov Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov

Gen5 Dec Oct+Nov+Dec Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov+Dec

Gen4 Qtr4 Reset

Gen3 2nd Half

Gen2 Year Total Reset

Gen1 Time

Page 68: Getting Started with MDX 20140625a

Periods to Date

� Year To Date

Sum(

{PeriodsToDate ([Time].Generations(2),

[Time].CurrentMember )

}

)

� Quarter to Date

Sum(

{PeriodsToDate ([Time].Generations(4),

[Time].CurrentMember )

}

)

Page 69: Getting Started with MDX 20140625a
Page 70: Getting Started with MDX 20140625a

Create a Simple Average

� Syntax:

Avg ( set [,numeric_expression [,IncludeEmpty ] ])

� Example:

Avg ({Jan:Jun}, Units)

Page 71: Getting Started with MDX 20140625a

LastPeriods() to Create a Range

� Syntax:

LastPeriods ( numeric_expression [, member [, hierarchy ]

] )

� Example:Avg (

LastPeriods( 6, Jun),

Units)

Marketing Technologies Group | www.mtgny.com

Page 72: Getting Started with MDX 20140625a

.CurrentMemberto Make the Range Relative

Example:

Avg (

LastPeriods (6, [Time].CurrentMember),

Units)

Marketing Technologies Group | www.mtgny.com

Page 73: Getting Started with MDX 20140625a

Syntax Review #1

� Select - From - Where query structure

� MemberName or [Member Name]

� [MemberName] or [Dim].[Member Name]

� { MemberName1, [Member Name2]2 }

Marketing Technologies Group | www.mtgny.com

Page 74: Getting Started with MDX 20140625a

Using Generation or Level Names

Page 75: Getting Started with MDX 20140625a

Share of Ancestor

[Transactions]/

( [Transactions],

Ancestor ( [Geography].CurrentMember,

[Geography].Generations(2)

)

)

Ancestor ( member ,

layer | index

[, hierarchy ]

)