PY Total for Selected Time Attribute Hierarchy in SSAS cube - ssas

I was able to calculate PY Total for Selected Time Dimension's Attributes in individual Hierarchy but cannot figure out how to make it work for both Time Dimension Hierarchies with single calculated measure.
I had to USE Dynamic SET to populate Days and use AGGREGATE on the SET as follows -
CREATE DYNAMIC SET CURRENTCUBE.[Days]
AS {EXISTING [Time].[Time].[Day]};
CREATE MEMBER CURRENTCUBE.[Measures].[PY Sales Selected]
AS AGGREGATE(
GENERATE(EXISTING [Days],
{PARALLELPERIOD([Time].[Time].[Year],1,[Time].[Time].CurrentMember)}
),
[Measures].[Sales]
),
FORMAT_STRING = "#,#",
VISIBLE = 1
----
CREATE DYNAMIC SET CURRENTCUBE.[Days]
AS {EXISTING [Time].[Current].[Curr Day Flag]};
CREATE MEMBER CURRENTCUBE.[Measures].[PY Sales Selected]
AS AGGREGATE(
GENERATE(EXISTING [Days],
{PARALLELPERIOD([Time].[Current].[Curr Year Flag],1,[Time].[Current].CurrentMember)}
),
[Measures].[Sales]
),
FORMAT_STRING = "#,#",
VISIBLE = 1
Both calculations work individually but I would like to combine them so that I could just have one Calculated Measure [Measures].[PY Sales Selected].
We have following Time dimension structure -
I would appreciate any help!!

Related

MDX calculation for multiple measures

I have to create the next calculation for 20 different Measures in my OLAP. As you can see the described function calculates the value of a measure the previous year. I have to repeat the code 20 times only changing the specific measure ([Measures].[Sales]) and I think there is a way of changing the specific measure dynamically.
create MEMBER CURRENTCUBE.[Measures].[Sales ant] as
(ParallelPeriod(
[Dim Calendar].[Calendar].[Year]
,1
,[Dim Calendar].[Calendar].CurrentMember
), [Measures].[Sales]),
VISIBLE = 1 ,ASSOCIATED_MEASURE_GROUP = 'Fact Sales';
Is there is a way in MDX to know which measure in the [Measures] group is being used? In this case which is the best way to implement it.
If I remember correctly you can create hidden set of measures like this:
CREATE HIDDEN STATIC SET CURRENTCUBE.[My Set Of Measures] AS
{
[Measures].[Measure 1],
[Measures].[Measure 2],
[Measures].[Measure 3]
};
Then you can create scope assignment for this set:
Scope ([My Set Of Measures]);
this = (ParallelPeriod(
[Dim Calendar].[Calendar].[Year]
,1
,[Dim Calendar].[Calendar].CurrentMember
), Measures.CurrentMember)
End Scope;
Note that instead of concrete measure - [Measures].[Sales] you will reference a measure with Measures.CurrentMember, so when you choose [Measures].[Sales] on your report the scope above will apply.
Hope it will work.

How To Get All Items Created or Still Open For A Given Time

I am working with a system were items are created (postDate dimension) and closed (endDate dimension). The endDate column is always populated with the last time the item was seen. An item is considered closed in a certain time if its last seen date is before the date you are querying. Each row in the fact table has the item, postDate, endDate, locationID, and some other dimensions used for aggregations. What I am trying to accomplish is getting all items still active for a given time frame. For example I want to know all items posted in November 2008 or before November 2008 that has not yet closed. In SQL it would look something like:
SELECT C.geoCountyArea,TM.CalendarYear,COUNT(DISTINCT a.itemid)
FROM [dbo].[factTable] a
JOIN dbo.dimDate AS TM
ON TM.DateKey BETWEEN postDate AND endDate
JOIN [dbo].[dim_geography] C
ON A.geographyID=C.geographyID
WHERE C.geoCountyArea = '1204000057'
AND TM.CalendarYear = 2008 AND TM.MonthNumberOfYear = 11
GROUP BY C.geoCountyArea,TM.CalendarYear
ORDER BY C.geoCountyArea,TM.CalendarYear
This returns 27,715 which is expected. Now, in MDX this looks like:
WITH MEMBER Measures.[itemCount] AS
AGGREGATE(
{NULL:[PostDate].[Month Name].&[2008]&[11]} * {[EndDate].[Month Name].&[2008]&[11]:NULL},
[Measures].[Fact_itemCount]
)
SELECT NON EMPTY (
Measures.[itemCount]
) ON 0,
NON EMPTY (
{[PostDate].[Month Name].&[2008]&[11]},
{[Geography].[Geo County Area].&[1204000057]}
)ON 1
FROM [Cube];
This returns 27,717 - which is 2 more than the SQL version that could be due to items with no end Date posted. Now, the complication comes when I want to get more than one explicit time - for example item count for all months in 2008 or item count for all years. I looked up methods to link a given param to another one via roll playing dimensions and came across this link. I altered my script so it looks like:
WITH MEMBER Measures.[itemCount] AS
AGGREGATE(
{NULL:LINKMEMBER([DATE].[Calendar].CURRENTMEMBER
,[PostDate].[Calendar])}
* {LINKMEMBER([DATE].[Calendar].CURRENTMEMBER
, [EndDate].[Calendar]):NULL}
, [Measures].[Fact_itemCount]
)
SELECT {Measures.[jobCount]} ON 0,
NON EMPTY (
{[DATE].[Month Name].&[2008]&[11]},
{[Geography].[Geo County Area].&[1204000057]}
)ON 1
FROM [Cube];
This, however, returns only the items created in November 2008 - value of 14,884. If I add in other months I do get individual counts for each month but, again, these are just the items created in those months.
How do I get the "active" item count for a given month/year/quarter without having do explicitly declare the time values in the AGGREGATE?
Can you use NonEmpty?
WITH MEMBER Measures.[itemCount] AS
AGGREGATE(
{NULL:
NONEMPTY(
[PostDate].[Month Name].MEMBERS //<<AMEND TO EXACT STRUCTURE USED IN YOUR CUBE
,[DATE].[Calendar].CURRENTMEMBER
).ITEM(0).ITEM(0)}
* {NONEMPTY(
[EndDate].[Month Name].MEMBERS //<<AMEND TO EXACT STRUCTURE USED IN YOUR CUBE
,[DATE].[Calendar].CURRENTMEMBER
).ITEM(0).ITEM(0): NULL}
, [Measures].[Fact_itemCount]
)
...
This ended up being the solution that provided valid results (tested against SQL calls against the warehouse tables):
WITH MEMBER Measures.[itemCount] AS
AGGREGATE(
{NULL:LINKMEMBER([Post Date].[Calendar],
[Post Date].[Calendar])}
* {LINKMEMBER([Post Date].[Calendar],
[End Date].[Calendar]):NULL},
[Measures].[Fact_itemCount]
)
SELECT {Measures.[itemCount]} ON 0,
NON EMPTY (
{[Post Date].[Month Name].Children},
{[Geography].[Geo County Area].&[1204000057]}
)
FROM [Cube]
Not that I am doing LINKMEMBER against the post and end dates - not against the global Date measure.

Common calculated measure across all dimensions

I have a cube with 4 dimensions and I have a measure called Transaction Count. Now I want to calculate the Percentage across all the dimensions for that above measure.
I also have a dimension called Cars. I have the count across all the Cars and now I have defined a calculated measure for calculating the Percentage of each car from the total number of transactions. But it will work only for that particular dimension.
How I can create a single percentage calculated measure which can be used across all the dimensions?
MDX for the calculated measure: (which is working for only Carmake dimension)
CASE
WHEN ISEMPTY( [MEASURES].[Trans COUNT] )
THEN 0
ELSE ([Dim Car Make].[Hierarchy].CURRENTMEMBER,
[MEASURES].[FACT COLORPERFORMANCE COUNT])/
( [DIM CAR MAKE].[CARMAKE].[(ALL)].[ALL],
[MEASURES].[Trans COUNT])
END
I already have a Trancount(1000) measure. Now I need to create a calculated measure Freq % which should be calculated across all the dimensions.
Screenshot -> http://i.stack.imgur.com/iuaQO.jpg (need 10 rep for posting images)
Table 1 in screenshot - you drag and drop the carmake dimension, then both Tran Count and Freq% should be calculated as per CarMake breakdown.
Table 2 in screenshot - you remove CarMake and drag Quality, then both Tran Count and Freq% should be calculated as per Quality breakdown.
Table 3 in screenshot - you remove Quality and drag Brand, then both Tran Count and Freq% should be calculated as per brand breakdown.
The best way i found is using AXIS() to dynamically get the currently used dimension.
with
member Member_Lvl as AXIS(0).item(0).level.ordinal --get the level for next calc
member All_Member as sum(ancestor(axis(0).item(0).hierarchy.currentMember,<yourMeasure>),Member_Lvl )
member Percent_of_All as sum(axis(0).item(0).hierarchy.currentMember,<yourMeasure>) / All_Member
select <Your Dim > on 0, {<Your Measure>, Percent_of_All} on 1
from <Your Cueb>
NOTICE: replace the SUM function on the calculations if you need other aggregation.

Calculated SSAS Member based on multiple dimension attributes

I'm attempting to create a new Calculated Measure that is based on 2 different attributes. I can query the data directly to see that the values are there, but when I create the Calculated Member, it always returns null.
Here is what I have so far:
CREATE MEMBER CURRENTCUBE.[Measures].[Absorption]
AS sum
(
Filter([Expense].MEMBERS, [Expense].[Amount Category] = "OS"
AND ([Expense].[Account Number] >= 51000
AND [Expense].[Account Number] < 52000))
,
[Measures].[Amount - Expense]
),
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'Expense';
Ultimately, I need to repeat this same pattern many times. A particular accounting "type" (Absorption, Selling & Marketing, Adminstrative, R&D, etc.) is based on a combination of the Category and a range of Account Numbers.
I've tried several combinations of Sum, Aggregate, Filter, IIF, etc. with no luck, the value is always null.
However, if I don't use Filter and just create a Tuple with 2 values, it does give me the data I'd expect, like this:
CREATE MEMBER CURRENTCUBE.[Measures].[Absorption]
AS sum
(
{( [Expense].[Amount Category].&[OS], [Expense].[Account Number].&[51400] )}
,
[Measures].[Amount - Expense]
),
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'Expense';
But, I need to specify multiple account numbers, not just one.
In general, you should only use the FILTER function when you need to filter your fact table based on the value of some measure (for instance, all Sales Orders where Sales Amount > 10.000). It is not intended to filter members based on dimension properties (although it could probably work, but the performance would likely suffer).
If you want to filter by members of one or more dimension attributes, use tuples and sets to express the filtering:
CREATE MEMBER CURRENTCUBE.[Measures].[Absorption]
AS
Sum(
{[Expense].[Account Number].&[51000]:[Expense].[Account Number].&[52000].lag(1)} *
[Expense].[Amount Category].&[OS],
[Measures].[Amount - Expense]
),
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'Expense';
Here, I've used the range operator : to construct a set consisting of all [Account Number] members greater than or equal to 51000 and less than 52000. I then cross-join * this set with the relevant [Amount Category] attribute, to get the relevant set of members that I want to sum my measure over.
Note that this only works if you actually have a member with the account number 51000 and 52000 in your Expense dimension (see comments).
An entirely different approach, would be to perform this logic in your ETL process. For example you could have a table of account-number ranges that map to a particular accounting type (Absorption, Selling & Marketing, etc.). You could then add a new attribute to your Expense-dimension, holding the accounting type for each account, and populate it using dynamic SQL and the aforementioned mapping table.
I don't go near cube scripts but do you not need to create some context via the currentmember function and also return some values for correct evaluation against the inequality operators (e.g.>) via the use of say the membervalue function ?
CREATE MEMBER CURRENTCUBE.[Measures].[Absorption]
AS sum
(
[Expense].[Amount Category].&[OS]
*
Filter(
[Expense].[Account Number].MEMBERS,
[Expense].[Account Number].currentmember.membervalue >= 51000
AND
[Expense].[Account Number].currentmember.membervalue < 52000
)
,
[Measures].[Amount - Expense]
),
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'Expense';
EDIT
Dan has used the range operator :. Please make sure your hierarchy is ordered correctly and that the members you use with this operator actually exist. If they do not exist then they will be evaluated as null:
Against the AdvWks cube:
SELECT
{} ON 0
,{
[Date].[Calendar].[Month].&[2008]&[4]
:
[Date].[Calendar].[Month].&[2009]&[2]
} ON 1
FROM [Adventure Works];
Returns the following:
If the left hand member does not exist in the cube then it is evaluated as null and therefore open ended on that side:
SELECT
{} ON 0
,{
[Date].[Calendar].[Month].&[2008]&[4]
:
[Date].[Calendar].[Month].&[1066]&[2] //<<year 1066 obviously not in our cube
} ON 1
FROM [Adventure Works];
Returns:

SSAS: Percent of Total not working in hierarchy

I have a simple olap cube - one set of measures and some unexciting dimensions.
I've add one calculation to get the "percent of total" sales against the gross sales measure. The code for this calculation is:
([Dim Stores].[Store Name].CurrentMember, [Measures].[Gross Sales])
/
([Dim Stores].[Store Name].Parent, [Measures].[Gross Sales])
This works.
Within the store dimension, there is a hierarchy called 'By State' where the stores are contained within.
Two questions please:
1. Any idea why the calculation would not work when I use the the 'By state' hierarchy i.e. the same calculation grouped by the next level up?
The state problem aside, any idea why my grand total shows an error even when I just use the Store Name?
TIA!
In poking around, I found a template within the "calculation tools" called "Percentage of Total". Using it, I translated my calculation to this:
Case
// Test to avoid division by zero.
When IsEmpty
(
[Measures].[Gross Sales]
)
Then Null
Else ( [Dim Stores].[By State].CurrentMember, [Measures].[Gross Sales] )
/
(
// The Root function returns the (All) value for the target dimension.
Root
(
[Dim Stores]
),
[Measures].[Gross Sales]
)
End
It worked!