Solved

SSAS conditional caculation

Posted on 2009-03-31
3
257 Views
Last Modified: 2016-02-12
I am new to SSAS in SQL Server 2005.  I want to create a calculated value, that has the value of an amount measure or zero depending the value of a dimension.  To outline a simple example assume I have a dimension called AccountCode with values of "4000", "4001", "5000", "5001", "5002", "6000", "6001", "6002", "6003", etc.  and I have a measure called "Amount".

I want to create 3 calculated values as outlined below:

Sales = Amount where AccountCode >= "4000" and AccountCode <= "4999" else 0
CostOfSales = Amount where AccountCode >= "5000" and AccountCode <= "5999" else 0
GeneralExpenses = Amount where AccountCode >= "6000" and AccountCode <= "6999" else 0

So in the OLAP Cube, I would have the measures Amount plus the three named above.
0
Comment
Question by:bandreini
3 Comments
 
LVL 8

Accepted Solution

by:
tpi007 earned 500 total points
ID: 24037500
Ideally, I would recommend you investigate parent child heirarchies in SSAS 2005.
http://msdn.microsoft.com/en-us/library/ms174846.aspx  
http://blogs.technet.com/paolt/archive/2005/02/17/375497.aspx
Bascially you can have a parentid column in accountcode source table and use parentid column to represent structure, This is usually done  in source data. Calculated memebers can be quite slow as calculations are done on fly as opposed to a meaure.    
Alternatively you can add sql below into datasource view of data for fact table and reference the columns as measure in their own right. Heiracrchies are preferreed option for your scenario but can take a little time to setup and a little complex to implement. They also require the parentid column to be maintained as chnages to your account structure change.  
 

CREATE TABLE [dbo].[#tblCosts](

	[AccountCode] [int] NOT NULL,

	[Amount] [decimal](18, 2) NOT NULL

) 
 

INSERT INTO #tblCosts (AccountCode,Amount) VALUES (4000,3.00)

INSERT INTO #tblCosts (AccountCode,Amount) VALUES (5000,4.00)

INSERT INTO #tblCosts (AccountCode,Amount) VALUES (5000,14.00)

INSERT INTO #tblCosts (AccountCode,Amount) VALUES (5800,3.00)

INSERT INTO #tblCosts (AccountCode,Amount) VALUES (4500,14.00)

INSERT INTO #tblCosts (AccountCode,Amount) VALUES (6700,24.00)

INSERT INTO #tblCosts (AccountCode,Amount) VALUES (6600,24.00)
 

select AccountCode,

(CASE WHEN AccountCode BETWEEN 4000 AND 4999 THEN Amount

      ELSE 0 END) AS Sales,

(CASE WHEN AccountCode BETWEEN 5000 AND 5999 THEN Amount

      ELSE 0 END) AS CostOfSales,

(CASE WHEN  AccountCode BETWEEN 6000 AND 6999 THEN Amount

      ELSE 0 END) AS GeneralExpenses

FROM #tblcosts

Open in new window

0
 

Author Closing Comment

by:bandreini
ID: 31565185
I do understand the reason why to this approach.  I have successfully implemented and tested it!  Thanks!  
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now