1 / 51

Fun with Scoped Assignments

Fun with Scoped Assignments. Chris Webb www.crossjoin.co.uk. Who Am I?. Chris Webb chris@crossjoin.co.uk Independent Analysis Services and MDX consultant and trainer Author: “MDX Solutions” and “Expert Cube Development with SSAS 2008” SQL Server MVP Blogger: http://cwebbbi.wordpress.com.

Télécharger la présentation

Fun with Scoped Assignments

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fun with Scoped Assignments Chris Webb www.crossjoin.co.uk

  2. Who Am I? • Chris Webb chris@crossjoin.co.uk • Independent Analysis Services and MDX consultant and trainer • Author: “MDX Solutions” and “Expert Cube Development with SSAS 2008” • SQL Server MVP • Blogger: http://cwebbbi.wordpress.com

  3. Agenda • What are scoped assignments and why should I use them? • Defining subcubes • Assignments to calculated measures • Complex subcubes • Assignments to real measures and aggregation • FREEZE and IF • Assigning to properties

  4. What are scoped assignments? • Scoped assignments allow you to overwrite the value of cells inside your cube • Contrast this with calculated members, which extend the space of the cube • They are defined on the MDX Script of your cube, on the Calculations tab in BIDS

  5. Calculated Members

  6. Calculated Members

  7. Scoped Assignments

  8. Scoped Assignments

  9. Why should I use them? • Among other things, they allow you to: • Make calculations behave in different ways in different parts of the cube • Break up logic for complex calculations into more comprehensible parts • Perform calculations at low levels of granularity that can then be aggregated up very efficiently • They are often the most efficient way of writing certain calculations

  10. Defining subcubes • Scoped assignments have two parts: • The subcube definition - the part of the cube whose value will be overwritten • The assignment - the new value written to the subcube • There are two ways of defining a subcube • Direct assignment • The SCOPE statement

  11. Direct assignment syntax • Direct assignment uses the following syntax:(subcube definition) = value; • For example:([Measures].[Internet Sales Amount])=1; • I prefer not to use it, because this syntax can very quickly become unreadable

  12. SCOPE statements • SCOPE statements take the form:SCOPE(subcube definition); THIS = value;END SCOPE; • For example:SCOPE([Measures].[Internet Sales Amount]); THIS=1;END SCOPE;

  13. Nesting SCOPE statements • The advantage of using SCOPE statements is that you can nest several to build up complex subcubes, eg:SCOPE([Measures].[Internet Sales Amount]); SCOPE([Date].[Calendar Year].[Calendar Year].MEMBERS); THIS=1; END SCOPE;END SCOPE; • This has obvious benefits for readability • You can also make assignments at different levels within the nesting

  14. Subcube definition rules • A subcube definition is made up of one or more sets of members from different hierarchies • These sets can be included in a single SCOPE statement, or several nested SCOPE statements • They define the boundaries of the subcube on different hierarchies

  15. Subcube definition rules

  16. Subcube definition rules

  17. Subcube definition rules

  18. Subcube definition rules

  19. Subcube definition rules • There are limitations on the shape of a subcube you can assign to • If you break them, you will get the infamous ‘arbitrary shaped set’ error • These are that a set of members can only contain: • One member from an attribute hierarchy • Several members from an attribute hierarchy, not including the All Member • Every member from an attribute hierarchy, including the All Member

  20. Not allowed!

  21. Subcube definition best practices • Best to ignore user hierarchies • Include sets of members from different hierarchies on the same dimension in the same SCOPE statement • Avoids problems with attribute overwrite • Specifically scope on one or more measures • Unless you really do want to scope on all measures • Even then can cause problems if you add new measures/measure groups in the future

  22. Assignments to calculated members • Scoped assignments can be made to calculated members as well as real members • In fact, assignments to calculated measures are probably the most common use • Calculated measures don’t aggregate up… • You should not assign to a set containing both calculated members and real members • In most cases you can’t do this

  23. Complex subcubes • Often we will want to scope on several attributes in a dimension at once • Eg all calendar year attributes, or all financial year attributes • To do this, you need to know your attribute relationships • The rule is: • For the lowest attribute, scope on all members in the hierarchy including the All Member • For the highest attribute, scope on all members in the hierarchy except the All Member

  24. Controlling area of calculation • Calculated measures often need to do different things in different places • Some calculations should not be done at all in some places • Eg you may not want to see a 3 month moving average at the year level • Eg previous period growth for your first year with data • Some calculations may need different logic in different part of the cube • Eg tax calculations could be different every year • Eg calculation logic may change for business reasons, but you want to see the older algorithm used for historical data • Eg previous year growth could mean ‘previous financial year’ or ‘previous calendar year’ depending on which hierarchy you’re using

  25. Assignments to real measures • When you assign to real (ie not calculated) measures, any values you assign will automatically aggregate up through the cube • This in turn may overwrite previous assignments that you have made • …and make it very difficult to know what’s going on…

  26. Assignments and aggregation: example #1

  27. Assignments and aggregation: example #1

  28. Assignments and aggregation: example #1

  29. Assignments and aggregation: example #1

  30. Assignments and aggregation: example #1

  31. Assignments and aggregation: example #1

  32. Assignments and aggregation: example #1

  33. Assignments and aggregation: example #1

  34. Assignments and aggregation: example #1

  35. Special cases for aggregation • In some cases, a cell can be calculated in two different ways from previous assignments • When this happens, the assignment made last in the script will take precedence • This is the ‘last pass wins’ rule • Also, when there are unary operators, these will always take precedence • This is the ‘closest pass wins’ rule

  36. The ‘Last Pass Wins’ rule

  37. The ‘Last Pass Wins’ rule

  38. The ‘Last Pass Wins’ rule

  39. The ‘Last Pass Wins’ rule

  40. The ‘Last Pass Wins’ rule

  41. The ‘Last Pass Wins’ rule

  42. The ‘Last Pass Wins’ rule

  43. The ‘Closest Pass Wins’ rule + + -

  44. The ‘Closest Pass Wins’ rule + + -

  45. The ‘Closest Pass Wins’ rule + + -

  46. Uses for assignments to real measures • There are several practical uses for making assignments to real measures: • Replacing custom member formulas, so you can have all your code in the MDX Script • Always scope on members on the lowest level first then work upwards • Performing calculations at a low level of granularity and them aggregating the results • Eg currency conversion

  47. Assignment ‘Passes’ • Each new assignment creates a new ‘pass’ in the script • Think of the cube like an onion, with each new pass as a new layer in the skin on the outside • When you refer to an existing cell value in an assignment, you are actually referring to that cell value in the previous pass • Therefore avoiding infinite recursion

  48. The FREEZE statement • When you assign a value to a cell, you create something like a pointer to that cell • So you don’t copy the new values assigned subcube • So if the values the assignment points to subsequently change, the values in your subcube will change • Using the FREEZE statement on a subcube stops this happening • Do not use FREEZE as a substitute for correct ordering of calculations • There is a performance overhead for using it!

  49. The IF statement • Conditional assignments can be made using the IF statement:SCOPE(MEASURES.SALES); IF MEASURES.SALES>100 THEN THIS=0 END IF;END SCOPE; • Note that even when the condition evaluates to false and no assignment is made, a new pass is created

  50. Assigning cell property values • The following cell property values can also be directly assigned to: Format_String, Non_Empty_Behavior, Language, Font_Size, Font_Flags, Font_Name, Fore_Color, Back_Color • Error values can be assigned to cells using the ERROR statement • Useful when: • A single measure holds values in different currencies which should be formatted differently depending on the currency • Debugging complex subcube definitions

More Related