Total Sum of Calculated member is wrong - ssas

I have there tables:
Artikal (eng. Item)
Grupa (eng. Group)
Potrosnja (eng. Consumption)
Potrosnja is my Measure table and Artikal is Dimension. Grupa contains also a parent-child relationship between groups. For now, is only one level. Inside the Artikal dimension I created
Hierarchy: Grupa.ParentId, Grupa.GrupaId, Artikal.Id. Name of Hierarhcy is PGA.
This shows well in Power BI. I also create some calculated Members: Cijena LY and Vrijednost LY.
I need to create a calculated member and the formula goes
(Cijena - Cijena LY)* Kolicina
First I tried to create like a calculated member but I saw that my total was also creating like the formula of totals of formulas. I want that my total for this member is SUM of calculated member column
I trying to do this with SCOPE, but I don't get the right result. Here is my script:
CREATE MEMBER CURRENTCUBE.[Measures].[Suma razlike] AS null,
VISIBLE = 1;
SCOPE([Measures].[Suma razlike],[Artikal].[PGA].members);
This =sum(
Descendants([Artikal].[PGA].CurrentMember,,LEAVES),
(([Measures].[Cijena]-[Measures].[Cijena LY])*[Measures].[Kolicina]));
END SCOPE;
Can somebody tell me what I am doing wrong? I creating this in Analysis services by Microsoft.

The solution was provided on the different blog by Alexei Stoyanovsky:
The problem was that was created hidden subselect. I needed to create a Dynamic set of Artikal. Here is solution:
CREATE DYNAMIC SET CURRENTCUBE.[MyTestSet] AS [Artikal].[Id].[Id].members;
SCOPE([Measures].[Suma razlike], [Artikal].[Id].members); --Includes All +
other members
This = case when [Artikal].[Id].currentmember.level.ordinal>0 then
([Measures].[Cijena]-[Measures].[Cijena LY])*[Measures].[Kolicina]
else
sum(existing [MyTestSet], ([Measures].[Cijena]-[Measures].[Cijena LY])*
[Measures].[Kolicina]) --All member is not included
end;
FORMAT_STRING([Measures].[Vrijednost - CPI])='Percent';
FORMAT_STRING([Measures].[Postotak razlike])='Percent';
FORMAT_STRING([Measures].[Razlika artikal - CPI])='Percent';
END SCOPE;

Related

SSAS Calc measure on filtering multi-members

I am trying to create a formula in Calculated measure but none is working correctly
Dimension Cell has multiple members including Control and Core. I also have measure called [Measures].[Rate] which is precalculated as percentage
I need to achieve below formula
(([Measures].[Rate] in [Cell].[ABC].&[Control] and [Cell].[ABC].&[Core])
- ([Measures].[Rate] not in [Cell].[ABC].&[Control] and [Cell].[ABC].&[Core]))
/ ([Measures].[Rate] in [Cell].[ABC].&[Control] and [Cell].[ABC].&[Core])
Something like (A+B)/A but I am not able to calculate individual A and B.
Please note [Measures].[Rate] is in percentage format so cannot be summed up
EDIT
Also any idea if the same above has to be done with two slices from different dimension for single measure
eg.
([Measures].[Rate] in [Cell].[ABC].&[Control] and [Cell].[ABC].&[Core] also in [Data].[PQR].&[Yes])
or
SUM (
{ [Cell].[ABC].&[Control] , [Cell].[ABC].&[Core] }
,{[Data].[PQR].&[Yes])}
,[Measures].[A]
)
Is above workable or what will be its syntax
Uhmm maybe something like:
CREATE MEASURE [Measures].[SpecialRate]
AS
AGGREGATE({[Cell].[ABC].&[Control],[Cell].[ABC].&[Core]}, [Measures].[Rate])
- AGGREGATE(EXCEPT([Cell].[ABC].MEMBERS,{[Cell].[ABC].&[Control],[Cell].[ABC].&[Core]}), [Measures].[Rate])
/ AGGREGATE({[Cell].[ABC].&[Control],[Cell].[ABC].&[Core]}, [Measures].[Rate])
,VISIBLE = 1;
The easiest way is to redesign the Cell dimension to include a new rollup column which includes both Control and Core into one rollup.
If that's not feasible and you have to do it in MDX then one way is to create a calculated measure on the dimension and then use it:
CREATE MEMBER CurrentCube.[Cell].[ABC].[All].[Control and Core] as
AGGREGATE({[Cell].[ABC].&[Control], [Cell].[ABC].&[Core]})
,VISIBLE=0;
CREATE MEMBER CurrentCube.[Cell].[ABC].[All].[Not Control and Core] as
AGGREGATE(-{[Cell].[ABC].&[Control], [Cell].[ABC].&[Core]})
,VISIBLE=0;
CREATE MEMBER CurrentCube.[Measures].[My Calc] as
(([Measures].[Rate], Cell].[ABC].[All].[Control and Core])
- ([Measures].[Rate], Cell].[ABC].[All].[Not Control and Core]))
/ ([Measures].[Rate], Cell].[ABC].[All].[Control and Core]);

Dimensions dragged in filter pane do not slice scoped measures

A measure 'X' gets its value from different fact tables.Lets consider Time (Fiscal Week, Month)dimension and Channel dimension. For different combination of attributes in these two dimensions X will get its value from different tables as follows:
Week + Channel - gets from table FactTrafficByWeekChannel
Week - gets from table FactTrafficByWeek
Month + Channel - gets from table FactTrafficByMonthChannel
Month - gets from table FactTrafficByMonth
To achieve this I added these fact to cube and created a calculated measure and scope scripts to overwrite the scope. Following is the scope script statement:`
CALCULATE;
CREATE MEMBER CURRENTCUBE.[Measures].[Y]
AS (0),
FORMAT_STRING = "Standard",
VISIBLE = 1;
Scope
([Measures].[Y],[Dim Time].[Fiscal Week].[Fiscal Week].Members
) ;
This = [Measures].[X - Vw Fact Total Weekly Traffic];
End Scope ;
Scope
([Measures].[Y],[Dim Time].[Fiscal Week].[Fiscal Week].Members,
[Dim Campaign].[Channel].[Channel].Members
) ;
This = [Measures].[X - Vw Fact Total Weekly Traffic By Channel];
End Scope ;
Scope
([Measures].[Y],[Dim Time].[Fiscal Month].[Fiscal Month].Members
) ;
This = [Measures].[X - Vw Fact Monthly Traffic];
End Scope ;
Scope
([Measures].[Y],[Dim Time].[Fiscal Month].[Fiscal Month].Members,
[Dim Channel].[Channel].[Channel].Members
) ;
This = [Measures].[X - Vw Fact Monthly Traffic By Channel];
End Scope ;
`
Above code works fine when corresponding dimension attributes are dragged in browsing pane but do not work when added to filter pane.
Fiscal Week dimension dragged to browsing pane this works. But
Fiscal Week dimension dragged to filter pane does not work.
This is because attributes added to filter pane are added as sub-cube statements.
Is there a way to achieve this when attributes are dragged to filter pane as well?
Time dimension Attribute relationship (as asked by Greg)
Any help is highly appreciated. Thanks in advance
One way is using dynamic named set as described in this post in the "Using dynamic sets to detect subselects" section.
Add this to the bottom of your existing MDX script:
Create dynamic named set CurrentCube.[SelectedWeeks] as [Dim Time].[Fiscal Week].[Fiscal Week].Members;
Scope
([Measures].[Y],[Dim Time].[Fiscal Month].[All],
[Dim Time].[Fiscal Week].[All],
[Dim Channel].[Channel].[All]
) ;
This = iif(SelectedWeeks.Count<[Dim Time].[Fiscal Week].[Fiscal Week].Members.Count,[Measures].[X - Vw Fact Total Weekly Traffic],0);
End Scope ;
But the problem with that approach is that it is going to get totally out of control checking all the permutations of single select on channel vs multi select on week, etc. And it will likely be slow.
Instead I would recommend removing the Create Member statement for Y from your MDX script and adding a new measure to your "by channel by week" measure group. Call the new physical measure Y and make it a Sum and connect it to a new SQL column that returns 0 on every row. This is just a placeholder measure you will do scoped assignments against. Why physical? When you make a scoped assignment on a physical measure at the channel level for example that assignment aggregates up. (Assignments to calc measures don't aggregate up.) This helps solve the multiselect issue. You assign at the channel level for example and then the All channel number is correct even under multiselect.
That being said, I am not optimistic you will be able to figure out the correct order of scoped assignments so that all 4 of your measures show up where you want with proper multiselect handling. But feel free to try.
One tip is to use Freeze([Measures].[Y], [Dim Time].[Fiscal Month].[All]) after one scoped assignment to months if you are happy with that portion of the cube but find subsequent scoped assignments are changing parts you were happy with.
If you need further help include a screenshot of the Attribute Relationships tab for Dim Time.

using scope with calculated member

I have problem in my calculated member. Whenever this member involve in calculation or query it take large time to execute. I am trying to narrow down execution time.
I have to remove IIF condition from the members and start using scope instead.
CREATE Member CurrentCube.[Measures].[AvgAmount] as
IIF(ISLeaf([Customer].[ParentCustomer].currentmember),
[Measures].[Value],
(SUM([CCube^Customer].[ParentCustomer].CURRENTMEMBER.CHILDREN) /
COUNT([Customer].[ParentCustomer].CURRENTMEMBER.CHILDREN))
) ,
Format_String = "#.0000000;-#.0000000;0;0",
Non_Empty_Behavior = [Measures].[Amout];
I have created hierarchy of customer which is [ParentCustomer] here. I want to see avg amount of all the children under the parent customer but when I am looking child level which does not have any children in it should only show the [Measures].[Amout].
Thanks in advance
Regards,
Sam
From your question, I assume you really want to have the average of the children, and not the average of all leaf level descendants. The latter could be implemented as follows:
Create a new measure group on the customer dimension table which has a single measure 'customer count' which would just be implemented as count - or of your customer dimension as a granularity that is finer than a single customer - countdistinct of the customer key or something like this.
Then just define your measure as
CREATE Member CurrentCube.[Measures].[AvgAmount] as
[Measures].[Value] / [Measures].[customer count],
Format_String = "#.0000000;-#.0000000;0;0",
Non_Empty_Behavior = [Measures].[Amout];
This assumes that the aggregation of [Measures].[Value] is defined as sum or one of the semi additive aggregations, but not max or min or something similar.
However, I assume from your question that this is not what you want. Instead you want to see the average of the children at each level. And I assume that [Customer].[ParentCustomer] is a standard user hierarchy and not a parent child hierarchy. Then, the approach suggested in the title, using SCOPE, would work. Let's assume you have three levels in your [Customer].[ParentCustomer] hierarchy:
The (implicitly defined) All level, just containing the All member
level A, built from attribute A of the dimension
level B, which is the leaf level and built from attribute B of the dimension
Then, under similar assumptions about the [Measures].[Value] aggregation, you could define the AvgAmount measure as follows:
// create the measure as it is correct for level B:
CREATE Member CurrentCube.[Measures].[AvgAmount] as
[Measures].[Value],
Format_String = "#.0000000;-#.0000000;0;0",
Non_Empty_Behavior = [Measures].[Amout];
// overwrite the definition for level A:
SCOPE([Customer].[ParentCustomer].[A].Members);
[Measures].[AvgAmount] = [Measures].[Value] / (EXISTING [Customer].[B].[B].Members).Count
END SCOPE;
// overwrite the definition for the ALl level:
SCOPE([Customer].[ParentCustomer].&[All]);
[Measures].[AvgAmount] = [Measures].[Value] / (EXISTING [Customer].[A].[A].Members).Count
END SCOPE;
This approach, using SCOPE, would not work for a parent child hierarchy, buta syou do not write you have one, I just assume you don`t.

How can i create and use generic calculation for statistical function Stdev ?

How can create generic calculated member which i can reuse in order to calculate the statistical deviation on any logical related dimension(s) and measurement ? I need to be able to select any dimension and measure.
I found related discussion here from which i took stdDev function example.
I've created the calculation, but the result it empty.
CREATE MEMBER CURRENTCUBE.[Measures].calcTest
AS iif(IsEmpty(Axis(0).Item(0)), NULL,
StdDev(Axis(1).Item(0).Item(0).Dimension.Children, Axis(0).Item(0))),
FORMAT_STRING = "#,##0.00;-#,##0.00",
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'TestMeasure' ;
If you want to use it for all measures, you would put your calcTest member to another hierarchy not used in the query, maybe an utility hierarchy (see below), and then reference the Measures.CurrentMember instead of Axis(0).Item(0).
To get the calculation generalized for all hierarchies except the measures is hard, as somewhere you have to tell the calculation which hierarchy to use. And to do this in the cube before the query is defined cannot be done. Hence, the approach to use the leftmost hierarchy in the rows (Axis(1).Item(0)) taken in your question can be changed to reference something different in the query, but cannot be completely flexible. Of course, an approach could be to define, say, three similar members, one for the first, one for the second, and one for the third hierarchy in the rows.
For the utility hierarchy, you would create a dimension table with just one row and one column. Let's say the data content of this column in the single row is "-standard-"and reference this single member in all fact tables from an additional foreign key. Then you build the dimension into the cube with the references from all measure groups. Lets say the attribute hierarchy is called [Utility].[Utility]. Make the attribute hierarchy non aggregatable, and set the default member to the "-standard-" member. In the calculation script, you can then add members to this utility dimension like
CREATE MEMBER [Utility].[Utility].StdDevRows1 // StdDev for leftmost hierarchy in the rows
AS StdDev(Axis(1).Item(0).Item(0).Dimension.Children, Measures.CurrentMember),
FORMAT_STRING = "#,##0.00;-#,##0.00",
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'TestMeasure' ;
CREATE MEMBER [Utility].[Utility].StdDevRows2 // StdDev for second hierarchy in the rows
AS StdDev(Axis(1).Item(1).Item(0).Dimension.Children, Measures.CurrentMember),
FORMAT_STRING = "#,##0.00;-#,##0.00",
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'TestMeasure' ;
CREATE MEMBER [Utility].[Utility].StdDevRows3 // StdDev for third hierarchy in the rows
AS StdDev(Axis(1).Item(2).Item(0).Dimension.Children, Measures.CurrentMember),
FORMAT_STRING = "#,##0.00;-#,##0.00",
VISIBLE = 1 , ASSOCIATED_MEASURE_GROUP = 'TestMeasure' ;
The advantage of a separate utility hierarchy is that you can combine it with all measures, times, etc., as it is not itself a member of these.

SSAS 2012 Calculated Member for Percentage

Being an SSAS newbie, I was wondering if it's possible to create a calculated member that references an individual row's value as well as the aggregated value in order to create a percentage?
For example, if I have a fact table with ValueA, I'd like to create a calculate member that essentially performed:
[Measures].[ValueA] (for each row I've sliced the data by) / [Measures].[ValueA] (the total)
Also I'd like to keep the total as the sum of whatever's been filtered in the cube browser. I feel certain this must be possible but I'm clearly missing something.
You can use the Axis function. Her is an example:
WITH MEMBER [Measures].[Percentage] AS
[Measures].[ValueA] / (Axis(1).CurrenMember.Parent, [Measures].[ValueA])
SELECT {[Measures].[ValueA], [Measures].[Percentage]} ON 0,
'what you want' ON 1
FROM your cube
(You may need to add check in the calculated member expression)