top 10 mdx for your cube

16
1. Top Products Percent of Sales The last calculation I have to show you utilizes a named set to return to the top 10 best selling products. Many clients like to monitor the sales of their best selling products this way. CREATE SET CURRENTCUBE.[Top 10 Internet Products] AS TopCount ( [Product].[Product].Children ,10 ,[Measures].[Internet sales amount] ) ; CREATE MEMBER CURRENTCUBE.[Measures].[Top 10 Products Internet Sales] AS Sum ( [Top 10 Internet Products] ,[Measures].[Internet Sales Amount] )

Upload: abacusdotcom2964

Post on 28-Oct-2015

53 views

Category:

Documents


2 download

DESCRIPTION

Top 10 MDX for Your CUBE

TRANSCRIPT

Page 1: Top 10 MDX for Your CUBE

1. Top Products Percent of Sales

The last calculation I have to show you utilizes a named set to return to the top 10 best selling products. Many clients like to monitor the sales of their best selling products this way.

CREATE

SET CURRENTCUBE.[Top 10 Internet Products] AS

TopCount

(

[Product].[Product].Children

,10

,[Measures].[Internet sales amount]

) ;

CREATE

MEMBER CURRENTCUBE.[Measures].[Top 10 Products Internet Sales] AS

Sum

(

[Top 10 Internet Products]

,[Measures].[Internet Sales Amount]

)

,FORMAT_STRING = "CURRENCY"

,VISIBLE = 1

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

Page 2: Top 10 MDX for Your CUBE

You can also add a calculated measure to show the percentage of sales of those 10 best selling products.

CREATE

MEMBER CURRENTCUBE.[Measures].[Top 10 Products Percent of Internet Sales] AS

Sum

(

[Top 10 Internet Products]

,[Measures].[Internet Sales Amount]

)

/

[Measures].[Internet Sales Amount]

,FORMAT_STRING = "#,##0.00 %;-#,##0.00 %"

,VISIBLE = 1

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

2. Period to Date Average

This calculation is very similar to #4 up above but instead of giving you the average Internet Sales for the past 12 months, the calculation will return the average Internet Sales for the current period. For example, if you view this calculation at the day level, you will see the average daily Internet Sales for the current month.

CREATE

Page 3: Top 10 MDX for Your CUBE

MEMBER CURRENTCUBE.[MEASURES].[Average Over Current Period] AS

Avg

(

[Date].[Calendar].CurrentMember.FirstSibling

:

[Date].[Calendar].CurrentMember

,[Measures].[Internet Sales Amount]

)

,FORMAT_STRING = "Currency"

,VISIBLE = 1

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

3. Percentage Growth From Previous Period

If you're wanting to measure the percentage of growth of a measure from a previous period, this is the calculation to use. A couple things to keep in mind: This calculation is hierarchy specific and is currently set up to measure the percentage of growth from the previous calendar year.

CREATE

MEMBER CURRENTCUBE.[MEASURES].[Percentage Growth From Previous Period] AS

CASE

WHEN

[Date].[Calendar].CurrentMember.Level IS [Date].[Calendar].[(All)]

THEN "NA"

WHEN

Page 4: Top 10 MDX for Your CUBE

IsEmpty

(

(

ParallelPeriod

(

[Date].[Calendar].[Calendar Year]

,1

,[Date].[Calendar].CurrentMember

)

,[Measures].[Internet Sales Amount]

)

)

THEN NULL

ELSE

(

(

[Date].[Calendar].CurrentMember

,[Measures].[Internet Sales Amount]

)

-

(

ParallelPeriod

(

[Date].[Calendar].[Calendar Year]

,1

Page 5: Top 10 MDX for Your CUBE

,[Date].[Calendar].CurrentMember

)

,[Measures].[Internet Sales Amount]

)

)

/

(

ParallelPeriod

(

[Date].[Calendar].[Calendar Year]

,1

,[Date].[Calendar].CurrentMember

)

,[Measures].[Internet Sales Amount]

)

END

,FORMAT_STRING = "#,##0.00 %;-#,##0.00 %"

,VISIBLE = 1

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

4. Rolling Average Calculation

Another common calculation used is one that calculates an average over the period of time, such as the monthly average of the past 12 months.

CREATE

Page 6: Top 10 MDX for Your CUBE

MEMBER CURRENTCUBE.[MEASURES].[Monthly Average Over Year] AS

Avg

(

[Date].[Month Name].CurrentMember.Lag(11)

:

[Date].[Month Name].CurrentMember

,[Measures].[Internet Sales Amount]

)

,FORMAT_STRING = "Currency"

,VISIBLE = 1

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

5. Percent of a Parent

This is very common calculation that I've seen used countless times to determine what percentage of a measurement makes up the total measurement for a larger body. In this example, I'm calculating what percent of Internet Sales for a product make up the total Internet Sales for the product's Sub Category.

CREATE

MEMBER CURRENTCUBE.[MEASURES].[Percent of Parent] AS

CASE

WHEN

IsEmpty([Measures].[Internet Sales Amount])

THEN NULL

WHEN

Page 7: Top 10 MDX for Your CUBE

[Product].[Product Categories].CurrentMember.Level

IS

[Product].[Product Categories].[(All)]

THEN 1

ELSE

(

[Product].[Product Categories].CurrentMember

,[Measures].[Internet Sales Amount]

)

/

(

[Product].[Product Categories].CurrentMember.Parent

,[Measures].[Internet Sales Amount]

)

END

,FORMAT_STRING = "Percent"

,VISIBLE = 1

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

6. Count Leaf Members of a Hierarchy

I've seen this kind of calculation used a lot in combinations with other measures and/or calculation. This calculation counts the Leaves, which are products, of the Product Categories hierarchy.

CREATE

MEMBER CurrentCube.[Measures].[Product Count] AS

Page 8: Top 10 MDX for Your CUBE

Count(Descendants([Product].[Product Categories].CurrentMember,,LEAVES))

,FORMAT_STRING = "#,##0.00;-#,##0.00"

,VISIBLE = 1

,DISPLAY_FOLDER = '10 MDX Calculations'

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

7. Percent of Total

More often then not, my clients want to be able to calculate the percent of a total amount for a hierarchy. In this example, I'm once again leveraging the Case statement.

CREATE

MEMBER CurrentCube.[Measures].[Percent of Internet Product Sales] AS

CASE

WHEN

IsEmpty([Measures].[Internet Sales Amount])

THEN NULL

ELSE

(

[Product].[Product Categories]

,[Measures].[Internet Sales Amount]

)

/

(

[Product].[Product Categories].[All]

,[Measures].[Internet Sales Amount]

Page 9: Top 10 MDX for Your CUBE

)

END

,FORMAT_STRING = "Percent"

,VISIBLE = 1

,DISPLAY_FOLDER = '10 MDX Calculations'

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

To adapt this calculation to your cube, just replace [Product].[Product Categories] with your dimension and hierarchy.

8. Profit Margin

Another popular calculation for obvious reasons is the calculation for profit margin. Basically all you need to understand for this calculation is the logic of a Case statement. We use the Case statement to check for a zero value in the denominator.

CREATE

MEMBER CurrentCube.[Measures].[Internet Profit Margin] AS

CASE

WHEN

IsEmpty([Measures].[Internet Sales Amount])

THEN NULL

ELSE

(

[Measures].[Internet Sales Amount]

-

[Measures].[Internet Total Product Cost]

Page 10: Top 10 MDX for Your CUBE

)

/

[Measures].[Internet Sales Amount]

END

,FORMAT_STRING = "Percent"

,VISIBLE = 1

,DISPLAY_FOLDER = '10 MDX Calculations'

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

9. Period to Period Growth

This is also a pretty common calculation I find and implement for my clients. It's also pretty straight forward. In this example, we're comparing this year's Internet Sales Amount to the previous year's Internet Sales Amount.

CREATE

MEMBER CurrentCube.[Measures].[Yearly Growth Internet Sales Amount] AS

(

[Date].[Calendar Year].CurrentMember

,[Measures].[Internet Sales Amount]

)

-

(

[Date].[Calendar Year].PrevMember

,[Measures].[Internet Sales Amount]

Page 11: Top 10 MDX for Your CUBE

)

,FORMAT_STRING = "Currency"

,VISIBLE = 1

,DISPLAY_FOLDER = '10 MDX Calculations'

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

The function here to pay attention to is the PrevMember function. The PrevMember function returns the previous member at a given level based on the supplied member.

For example, if the supplied member was 2011, the previous member would be 2010.

CREATE

MEMBER CurrentCube.[Measures].[Monthly Growth Internet Sales Amount] AS

(

[Date].[Month of Year].CurrentMember

,[Measures].[Internet Sales Amount]

)

-

(

[Date].[Month of Year].PrevMember

,[Measures].[Internet Sales Amount]

)

,FORMAT_STRING = "Currency"

,VISIBLE = 1

,DISPLAY_FOLDER = '10 MDX Calculations'

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

Page 12: Top 10 MDX for Your CUBE

Similar to our last calculation, if we wish to calculate the growth from month to month, we should reference the Month attribute of our Date dimension.

10. YTD, QTD, and MTD Calculations

These kinds of calculations are pretty common and I see these in a lot of cubes. They're pretty easy to wire up since there are only a couple simple MDX functions necessary to make this work.

CREATE

MEMBER CurrentCube.[Measures].[YTD Internet Sales Amount] AS

Aggregate

(

PeriodsToDate

(

[Date].[Calendar].[Calendar Year]

,[Date].[Calendar].CurrentMember

)

,[Measures].[Internet Sales Amount]

)

,FORMAT_STRING = "Currency"

,VISIBLE = 1

,DISPLAY_FOLDER = '10 MDX Calculations'

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

Let's start with the PeriodsToDate function. The PeriodsToDate function is going to return all the sibling members at the specified level up to the current member. The outer function, the Aggregate function, calculates the value based on the aggregation type specified in the cube for the measure. In our case,

Page 13: Top 10 MDX for Your CUBE

the Internet Sales Amount aggregation type is Sum, so the Aggregate function could be exchanged for the Sum function.

If I wanted to calculate the Month to Date for Internet Sales, I would simply exchange the reference for the Calendar Year level of the Calendar hierarchy with the Month level, as seen here:

CREATE

MEMBER CurrentCube.[Measures].[MTD Internet Sales Amount] AS

Aggregate

(

PeriodsToDate

(

[Date].[Calendar].[Month]

,[Date].[Calendar].CurrentMember

)

,[Measures].[Internet Sales Amount]

)

,FORMAT_STRING =

Currency

,VISIBLE = 1

,DISPLAY_FOLDER = '10 MDX Calculations'

,ASSOCIATED_MEASURE_GROUP = 'Internet Sales' ;

Page 14: Top 10 MDX for Your CUBE