Alias MDX Query SSAS - ssas

WITH MEMBER [Measures].[NetPromoterScore] AS (IIF(([Measures].[Net Promoter Score] = 1/0 OR
[Measures].[Net Promoter Score] = -1/0 OR ISEMPTY([Measures].[Net Promoter Score])
OR [Measures].[Avg Revenue Per Unit] = 1/0 OR [Measures].[Avg Revenue Per Unit] = -1/0 OR
ISEMPTY([Measures].[Avg Revenue Per Unit]) ), NULL, [Measures].[Net Promoter Score]))
MEMBER [Measures].[AvgRevenuePerUnit] AS (IIF(([Measures].[Net Promoter Score] = 1/0
OR [Measures].[Net Promoter Score] = -1/0 OR ISEMPTY([Measures].[Net Promoter Score])
OR [Measures].[Avg Revenue Per Unit] = 1/0 OR [Measures].[Avg Revenue Per Unit] = -1/0
OR ISEMPTY([Measures].[Avg Revenue Per Unit]) ), NULL, [Measures].[Avg Revenue Per Unit]))
SELECT NON EMPTY
{ {[Measures].[NetPromoterScore],[Measures].[AvgRevenuePerUnit]} }
ON COLUMNS ,
NON EMPTY
{{Hierarchize(DrilldownLevel({[Roles].[Enterprise Role].[ALL]}))}}
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME
ON ROWS
from Enterprise
WHERE (FILTER( [Employees].[EID].[EID],[Measures].[Avg Revenue Per Unit]> 700),
{[Areas].[Area].&[3]},
{[Markets].[Market].&[1]},{[Regions].[Region].&[2]},{[Locations].[Location].&[6],[Locations].[Location].&[6]},
{[Dates].[Date].&[20130219]:[Dates].[Date].&[20130318]})
As you see I have aliased [Net Promoter Score] column name with [NetPromoterScore] and [Avg Revenue Per Unit] column name with [AvgRevenuePerUnit]. But in my C# code, the names used are [Net Promoter Score] and [Avg Revenue Per Unit] and I can't change these previous name with new aliased name because I have to make changes in dozens of files and then I have to test whole application. Can I alias twice in the query above to get same previous column names or can I without aliasing get the same result from the query above?

Actually, in the sense of MDX, you have not aliased the members, but created new members.
I see two possibilities how you can solve this:
Move these calculations to the cube calculation script, i. e. rename
the original measures in the cube, and define calculated measures
with the name that you need and the same calculation that you have
in your query. You would then make the original renamed measures
invisible. This would be the best solution in my eyes, as it would
let every cube user benefit from the improvement, without breaking
external code or queries.
Depending on how exactly you reference the measures in your C# code
(you did not post samples), you could just use the caption property
of the measures, i. e. re-write the member definitions as follows:
WITH MEMBER [Measures].[NetPromoterScore] AS IIF(...), CAPTION = 'Net Promoter Score'
MEMBER [Measures].[AvgRevenuePerUnit] AS IIF(...), CAPTION = 'Avg Revenue Per Unit'

Related

MDX - Top N elements of every subgroup

Let's assume I have a cube with a sales fact table and 3 dimensions: Time (with hierarchy year-month-day), Geography (with hierarchy continent-country-region) and Product with some characteristics about the product sold, let's take the brand for instance.
What I am trying to do is to display the top N brands with respect to the measure chosen, in this case average sales (which is already in the cube as total sales/number of products sold), for every region and month.
I also need to display the country and the year, for clearness.
I have searched for the solution to this everywhere, I came close to it but not completely. I hope someone can help me figure it out.
So I used generate and topcount with the following query, but the problem is that the topcount calculates the N best selling brands over the whole dataset, not for every region and month subgroup. And then applies this top N to every subgroup.
WITH SET [Top N Brands] AS
Generate([Geography].[Region].Children,
TopCount(Geography].[Region].CurrentMember * [Gpu Product].[Brand].[Brand].MEMBERS,
5, ([Measures].[averageSales])))
SELECT {[Measures].[averageSales]} ON COLUMNS,
NON EMPTY ( [Time].[Year].[Year],
[Time].[Month].[Month],
[Geography].[Country].[Country],
[Top N Brands]) ON ROWS
FROM [Cube]
So I am getting this, with the global top 5 brands distributed over the regions, if sold there:
But I should get this with different top 5s for every region:
What am I missing?
You need to use rank. Take a look at the example below. I am using the sample Adventure Works Db, here I am listing For each country, for each product category in that country, the top three subcategories according to internet sales.
WITH
MEMBER [Measures].[Internet Sales Amount Rank] AS
RANK( ([Customer].[Country].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].CurrentMember),
ORDER( ([Customer].[Country].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].Members) , [Measures].[Internet Sales Amount], BDESC)
)
select
non empty
([Measures].[Internet Sales Amount])
on columns
,
non empty
([Customer].[Country].[Country],
[Product].[Category].[Category],
filter([Product].[Subcategory].[Subcategory],[Measures].[Internet Sales Amount Rank]<4))
on rows
from [Adventure Works]
Result

Scope Statement Alternative to Slow CrossJoin Count MDX

The below MDX works for my purpose but is extremely slow. Is there a scope statement Essentially I want to count the remaining cross joined contact/purchases combinations where the summed amount is $>5000.
The cross product is a total of 290M rows but I am not sure how to structure this differently to improve performance. Thank you for any help.
CREATE HIDDEN STATIC SET [Over 5K Plus Test 2]
AS NONEMPTY (([Contact].[Contact ID].[Contact ID],[Fund Sold].[Fund Sold ID].[Fund Sold ID]),
[Measures].[FA And Team Gross Sales with FAs Including All Vehicles]);
CREATE MEMBER CURRENTCUBE.[Measures].[FA and Team Product Count]
AS COUNT(EXISTING((Filter([Over 5K Plus Test 2], [Measures].[FA And Team Gross Sales with FAs Including All Vehicles] >= 5000)))),
Try this which avoids the Filter:
CREATE MEMBER CURRENTCUBE.[Measures].[FA and Team Product Count]
AS SUM(
Existing [Contact].[Contact ID].[Contact ID].Members
* Existing [Fund Sold].[Fund Sold ID].[Fund Sold ID].Members,
IIF([Measures].[FA And Team Gross Sales with FAs Including All Vehicles] >= 5000, 1, Null)
);
If that is still slow then post the calculation behind FA And Team Gross Sales with FAs Including All Vehicles
The more efficient way to accomplish this requires a bit more effort but will perform better because it avoids the Existing function. First you have to create a column in the DSV which is a calculated column in the fact table using this expression:
CAST(null as int)
Then create a new measure called “FA and Team Product Count” on this column. Expand the column binding and choose NullHandling=Preserve. This has to be a physical measure not a calculated measure because only scoped assignments to physical measures aggregate up.
Then add the following statement to the MDX script (instead of the calculated measure mentioned at the top):
([Measures].[FA and Team Product Count],
[Contact].[Contact ID].[Contact ID].Members,
[Fund Sold].[Fund Sold ID].[Fund Sold ID].Members) =
IIF([Measures].[FA And Team Gross Sales with FAs Including All Vehicles] >= 5000, 1, Null);

DAX Measure with different granularities

I'm looking for help with how to write a specific DAX measure. Here is a simplified version of my data and model:
Tables:
Model:
Measures:
Total Amt:=SUM(Amounts[Amt])
Total Pos Amt:=SUMX(Amounts, IF([Amt]<0,0,[Amt]))
Total Amt All:=CALCULATE([Total Pos Amt],ALL(Ptr))
Total Amt All 2:=SUMX(Bridge,CALCULATE([Total Pos Amt],ALL(Ptr)))
Total Amt All 3:=SUMX(VALUES(Bridge[Pri]),CALCULATE([Total Pos Amt],ALL(Ptr)))
As you can see in the first PivotTable (where [Pri] & [Ptr] are row fields), the highlighted cells show values with an issue. The [Total Pos Amt] measure sums up the [Amt] column in the Amounts table by iterating through it and evaluating an expression where negative amounts are treated as zero and positive amounts are kept. At a [Pri] level granularity, I want that same logic to apply (i.e. evaluate the expression at a [Ptr] level). The problem with the [Total Amt All] measure is that on the PivotTable I get a row for [Ptr] Z under [Pri] A which I don't want. Measures [Total Amt All 2] and [Total Amt All 3] solve that issue but the subtotals at a [Pri] level are wrong in [Total Amt All 2] and the grand total is wrong in [Total Amt All 3].
Any help would be greatly appreciated! How can I write a measure that won't show a [Ptr] that is not associated with a [Pri] per the Bridge table, but that also correctly sums up the [Total Pos Amt] measure at a [Pri] level?
So one of your problems might be which fields you're using in your PivotTable. I got it work by using your bridge table as the fields:
TotalAmtBridged:=CALCULATE ( SUMX(Amounts, IF([Amt]<0,0,[Amt]) ) , Bridge )
FinalTotalAmt:= Calculate([TotalAmtBridged], ALL(Bridge[Ptr])
And then the PivotTable uses Bridge[Pri] and Bridge[Ptr]. So TotalAmtBridged just forces your total amount to use the Bridge context, and then FinalTotal says ignore Ptr (i.e. for each row we're displaying figure out the total amount for Bridge[Pri] only).
And then the grand total's already doing that, so Bob's your uncle.

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 -> MDX -> Creating a column percentage within my query based on counts

SELECT
NON EMPTY {[Measures].[Fact Order Count]}
ON COLUMNS,
{ ([Front Manager].[Front Manager Id].[Front Manager Id].ALLMEMBERS * [Order Type].[Order Type].[Order Type].ALLMEMBERS ) }
ON ROWS
FROM
[TEST_DW] CELL PROPERTIES VALUE
So, I have three columns in the output:
Front Manager, Order Type, Order Count
The above query shows me the counts for each manager and order type combination. I need a fourth column which would be a percentage of the types of orders for each front manager.
So, if there are four types of orders (A, B, C, D), and a manager had 25 of each order totaling 100. The fourth column would read 25%.....
I have scoured the web on how to do this, but have really come up short on this one. Any direction on this would be greatly appreciated, I am definitely new to MDX. Thanks.
What you're looking for are MDX Calculated members.
Let's assume the member for order A is called : [Order Type].[Order Type].[Order A] and we want to calculate the percentage from the total.
WITH
MEMBER [Order A] AS ([Order Type].[Order Type].[Order A],[Measures].[Fact Order Count]) / ([Measures].[Fact Order Count]) , FORMAT_STRING = 'Percent'
SELECT
{[Measures].[Fact Order Count],[Measures].[Order A]} on 0
...
What is important in the calculated members is that you can evaluate any MDX tuple (e.g ([Order Type].[Order Type].[Order A],[Measures].[Fact Order Count]) ). This changing if needed the values coming from the pivot axis (defined in on 0 and on 1..). Note you can add calculated members for the measures as well as the other dimensions.