Fun with Scoped Assignments

Slides:



Advertisements
Similar presentations
1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
Advertisements

Chris Webb Crossjoin Consulting Ltd
Cache –Warming Strategies for Analysis Services 2008 Chris Webb Crossjoin Consulting Limited
Semantics Static semantics Dynamic semantics attribute grammars
Introduction To MDX Dustin Ryan. A little bit about me…  Business Intelligence Consultant, Pragmatic Works  Technical editor for the many authors at.
C++ Lecture 6 Object Life-times Creating objects using new Using pointers to objects Aggregation (Containment –UML speak) Other C++ class features.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Implementing Business Analytics with MDX Chris Webb London September 29th.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Computer Science 1620 Programming & Problem Solving.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Chapter 6 Control Statements Continued
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
The Program Design Phases
Introduction Paul Turley SqlServerBiBlog.com Mentor, SQL Server MVP
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Implementing Common Business Calculations in DAX
IST722 Data Warehousing Business Intelligence Development with SQL Server Analysis Services and Excel 2013 Michael A. Fudge, Jr.
Performance Tuning Cubes and Queries in Analysis Services 2008 Chris Webb
Common Analysis Services Multidimensional Design Mistakes and How to Avoid Them Chris Webb
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Building an Offline Smart Client using Domain-Driven Design Principles Tim McCarthy.
General Programming Introduction to Computing Science and Programming I.
Optimizing Time-Series Calculations in SSAS
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
E145/STS173 Case Study Tips E145/STS173 Case Study Tips Professors Tom Byers and Randy Komisar Stanford University With special thanks to: Jan Chong Copyright.
Input, Output, and Processing
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
ISQS 6339, Data Management and Business Intelligence Cubism – Bells and Whistles Zhangxi Lin Texas Tech University 1.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore.
BI Terminologies.
CPS120: Introduction to Computer Science Decision Making in Programs.
Platinum Gold Silver Group BY: [Remember get your Tickets for Entry, Coach, Drink] Feedback Forms: [Fill these out at the end of each session.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
What it is and how it works
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1 Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
JavaScript, Fourth Edition
Advanced Tips And Tricks For Power Query
Object-Oriented Programming Chapter Chapter
Chapter 6 Control Statements Continued
BI Practice March-2006 COGNOS 8BI TOOLS COGNOS 8 Framework Manager TATA CONSULTANCY SERVICES SEEPZ, Mumbai.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
SQL Server Analysis Services Understanding Unified Dimension Model (UDM)
BISM Introduction Marco Russo
Advanced MDX Tips And Tricks Chris Webb. Who Am I? Chris Webb UK-based consultant and trainer:
Advanced Analysis Services Security Chris Webb Crossjoin Consulting Limited.
about me – Austin Senseman, CFA 5 years in Financial Services, Managed analytics for sales, marketing, risk, finance, &
Extending and Creating Dynamics AX OLAP Cubes
Object-Oriented Programming (OOP) Lecture No. 16
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Use the Power of Analytical Hierarchies in Your Cubes
Object-Oriented Programming (OOP) Lecture No. 16
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Topics Introduction to Functions Defining and Calling a Function
Boolean Expressions to Make Comparisons
CISC101 Reminders All assignments are now posted.
Review of Previous Lesson
Module 8: Introduction to MDX Module 8 Introduction to MDX
Presentation transcript:

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

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

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

Calculated Members

Calculated Members

Scoped Assignments

Scoped Assignments

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

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

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

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;

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

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

Subcube definition rules

Subcube definition rules

Subcube definition rules

Subcube definition rules

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

Not allowed!

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

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

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

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

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…

Assignments and aggregation: example #1 120 30 40 10

Assignments and aggregation: example #1 120 30 40 10 20

Assignments and aggregation: example #1 120 30 40 50 10 20

Assignments and aggregation: example #1 130 30 40 50 10 20

Assignments and aggregation: example #1 120 30 40 10

Assignments and aggregation: example #1 500 30 40 10

Assignments and aggregation: example #1 500 30 40 10 20

Assignments and aggregation: example #1 500 or 130? 30 40 50 10 20

Assignments and aggregation: example #1 130 30 40 50 10 20

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

The ‘Last Pass Wins’ rule 120 30 40 10

The ‘Last Pass Wins’ rule 120 30 40 10 20

The ‘Last Pass Wins’ rule 120 30 40 50 10 20

The ‘Last Pass Wins’ rule 130 30 40 50 10 20

The ‘Last Pass Wins’ rule 130 30 40 50 10 20

The ‘Last Pass Wins’ rule 140 or 290? 70 80 50 10 20 40

The ‘Last Pass Wins’ rule 140 70 80 50 10 20 40

The ‘Closest Pass Wins’ rule 60 15 40 10 20 5 + + -

The ‘Closest Pass Wins’ rule 60 15 50 40 10 20 5 + + -

The ‘Closest Pass Wins’ rule 60 15 50 40 10 20 5 + + -

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

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

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!

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

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

Thanks!