Showing posts with label cells. Show all posts
Showing posts with label cells. Show all posts

Monday, March 12, 2012

MDX to work only on leaf cells

Hello!

Again I am unable to solve a rather simple looking problem with MDX. My cube looks something like this:

Group Detail MesA MesB MesC

a 1 300 50 37 (300 * 50 / 400)

a 2 300 100 75 (300 * 100 / 400)

a 3 300 250 187 (300 * 250 / 400)

a Total 300 400 133 (300 * 400 / 900)

... more groups (b, c, d ...) of this style ...

Grand Total 700 900 <NUM>

As you already noticed, MesA is a Measure that existeds on group grain only, so the value is the same for every entry on detail. My first goal is to distribute the MesA according to what I found on MesB (with rounding or such, a detail we can leave out here). I managed to do that as a computed column C (using very kind help from here) with the following MDX:

[Measures].[MesA] * [Measures].[MesB] / ([Dim].[Hie].CurrentMember.Parent, [Measures].[MesB])

This MDX works as intended for a leaf, but when it hits the first subtotal, it calculates the value using the totals from the grand total line. On the grand total line, it fails completly because there is no parent available anymore.

How can I avoid that? I tried my luck using the ISLEAF function, but I cannot get that working. I was able to produce the correct number (300) for the orange field using another bit of MDX:

([Measures].[MesC], [Dim].[Hie].Members) = Sum(Existing [Dim].[Hie].[Detail].Members, [Measures].[MesC])

That works on the first impression, but the little I understand of MDX makes me wonder what I am doing there at all. It only works on first impression, because in my example, if you use your Frontend Cube Browser to hide that row a-2, the total for the orange field should go down to 225, the sum of the remaining visible. It does not, as I "enforce" the value stored at this cell fields instead of relying on the cube to simple total them up.

So... what can I do? Thanks in advance!

Ralf

(Sorry for the weird font sizes in this text. I do not know why, but I cannot get the upper and lower half to show the same size no matter what I try. Please excuse that!)

Hi Ralf,

Since you want MesC to roll up like a regular cube measure, one approach would be to initially define MesC as a "sum" measure on the same measure group as MesB (you can use the same source field, if you wish). Then add a leaf-level calculation to the cube MDX script, using your "distribution" MDX formula on the right side:

([Measures].[MesC], Leaves([Dim])) =

[Measures].[MesA] * [Measures].[MesB] / ([Dim].[Hie].CurrentMember.Parent, [Measures].[MesB])

|||

Deepak,

thanks for the tip with the Leaves() set, it was very helpful to get my solution closer to where I need to go to, but...

Are any strange behaviours known for the Grand Total result known when using own calculations? I am getting the strangest results here.

I wonder if there is a good way to send Table Create scripts and Project Files in case someone wants to reproduce that - I am (almost) sure this is no error I am causing anymore, but a real bug in the analysis engine (although... hm... could I be the first to spot such an issue? Unlikely...)

I reduced my real project to the very smallest version for this example.

I use 4 Tables, 2 of those form a dimension with one hierarchie of 2 levels (2 items a and b on level 1 in one table, and 4 items a1,a2,b1,b2 on level 2 in the other), a 3rd table is another simple dimension (with only 2 items x and y on the top row) and finally the 4th is the measure table, connected to the finest grain of of the hierarchy dimension and the "other" dimension. The measure is a simple "sum up" integer, the fact table has 8 rows (4 x 2). Nothing in that setup that should raise any concern, everything totally straight forward. No properties of the dimensions or the measures were tuned or changed or... everything just as the wizards do create when you click through them.

The cube script has only the CALCULATE order followed by one custom line ([Measures].[Value], Leaves()) = 1... that is all.

After I set it up, it looks this way:

x y Grand Total

a a1 1 1 2

a2 1 1 2

Total 2 2 4

b b1 1 1 2

b2 1 1 2

Total 2 2 4

Grand Total 4 4 8

That is all, looks very easy and logical and ... ok, now hide a2 for example, or b1 or so... my Grand total row then changes to "102 - 502 - 902" or "202 - 602 - 804", depending which item you hide. If you unhide it again, everything works perfectly fine again. I want to add that the subtotal line for a or b does change to " 1 - 1 - 2" as expected. Subtotals is fine, only Grand total is wrong.

I think I can explain the numbers, at least their amount: Say I hide element a2. I know that element a1 has a value of 100 in the SQL table before overriding it with a "number one" in my script, therefore the grand total is calculated out of 100 (a1) + 1 (b1) + 1 (b2). Again, the value for a1 and the subtotal for a is correct, both display as 1. But when the grand total is calculated, the a section is not changed by my "set to 1" script.

Can anyone make sense of that? As I said, if someone wants to try it I will work on a way to either post create scripts or whatever needed to solve that.

Friday, March 9, 2012

mdx script context execution

Hi everybody.

I have a mdx script on a cube involving several cells through subcube. The subcube takes the values of some cells and use them to calculate the values of some other cells.

I have a user who has limited access rights on the dimensions, so that he can't see all the cells of the cube.

But I need that all the users can perform all the calculations in the cube even if there are some members (used to evaluate the calculations) they are not allowed to.

I know that the calculations script are evalueted in the context of the user querying the cube.

I wonder if there is a command to make the script being evaluated in the context of a specified user, an administrator in this case (something like "execute as"). So that the calculations are applied first, and after that, the user with reduced rights can see only the cells he is allowed to...

thank you very much.

Sorry, but there is no 'Execute As' feature and I can't think of anyway that you could reference a secured cell in a calculation.|||

Actually one possible solution to this would be to write a .Net stored procedure, that would would use AdoMd to connect back to the user with a new connection and execute an MDX query and return the results. When you deploy a stored proc assembly you have the option of specifying the account to impersonate.

Performance could be an issue with this approach as there is a bit of overhead involved in calling out to a stored procedure, but if your calc is at a fairly high level this might be a solution.

|||

Well, you give me a wisdom, thank you very much!

A more question, because I've never used a .Net stored procedure for Analysis Services. Do you mean that through it I can make the calculations in the script of the cube being executed by a specified user with all the necessary rights, and, after the calculations are realized on all the involved cells, then I can make the application to connect back as the current user to query the cube (therefore, seeing the previous results but only for the allowed cells)?

It'd be great.

have you any examples?

Thank you very very much!!!!

|||Check out this link for examples: http://www.codeplex.com/ASStoredProcedures|||

The AS Stored Procedure project has some good examples (I wrote some of them). However I don't think we have any that do exactly what you are after.

What would happen is that from a "limited" user, they would request a value.

That value would be calculated by a stored procedure. When the stored procedure is deployed it can be configured to run under a specific user account. In the stored proc it would run .Net code to run a query back against the current cube, but using a new connection (running as the account specified when the assembly was deployed). It would have to issue a full MDX query. This would return a cellset and the stored proc would extract the value of the required cell and return that value|||

Thank you very much again.

You say: “

v In the stored proc it would run .Net code to run a query back against the current cube, but using a new connection (running as the account specified when the assembly was deployed). It would have to issue a full MDX query.

v This would return a cellset and the stored proc would extract the value of the required cell and return that value

“

But I wonder if in this way the “poor” user (with limited rights) in Role1, issuing the query through the specified user for th AS SP, would be allowed to see also the cells he is forbidden to… In fact I need him to see only the cells he is allowed to, but with the values calculated with calculations involving all the cells (even the ones he is not allowed to). Is this possible this way?

However, I give you an example of what I need to do. Let’s say I need to do something like in the following script (a very simplified version of my script, but just to understand the type of calculation).

I have a dimension [DIM Mesi] with four key members [01], [02], [03], [00] and a one-level attribute hierarchy [Dim Mesi].

Role1 can access only the “[DIM Mesi].[DIM Mesi].&[01]”, “[DIM Mesi].[DIM Mesi].&[02]” and the ”existing” with these ones (I mean the ancestors of these members, which in this case, is the AllMember).

There is a basket member [DIM Mesi].[DIM Mesi].&[00] from where I take the values to be reversed on the three members [01], [02], [03] from the Measure [Costi1] into the same measure (or even in a different measure).

The calculations are something similar to the ones below.

I’d like the users of Role1 be allowed to see 1/3 of the values of cells of the &[00] member in the cells of &[01] and &[02], but they should not be able to see nor the &[00] member related cells neither the &[03] member related cells.

Further, the [DIM Mesi].[DIM Mesi].&[00] should be assigned a 0 value (even if the user can’t see that value) so that the total is not twice, in case I don’t use VisualTotals.

If possible, I’d like to even be able to decide whether he must see the Visual Totals or not, but I suppose that through the AS SP executed by another user I can only see the Actual Totals, or am I wrong?

THANK YOU VERY MUCH for your kind help!

CALCULATE;

CREATE SET CURRENTCUBE.[GoodMembers]

AS {

iif(IsError(StrToMember("[DIM Mesi].[DIM Mesi].&[01]")), {}, [DIM Mesi].[DIM Mesi].&[01]),

iif(IsError(StrToMember("[DIM Mesi].[DIM Mesi].&[02]")), {}, [DIM Mesi].[DIM Mesi].&[02]),

iif(IsError(StrToMember("[DIM Mesi].[DIM Mesi].&[03]")), {}, [DIM Mesi].[DIM Mesi].&[03])

};

scope

(

{[Measures].[Costi1]}

,

{

[GoodMembers]

}

);

this= [DIM Mesi].[DIM Mesi].&[00]/3

;

freeze(this);

end scope;

scope

([Measures].[Costi1],

{

[DIM Mesi].[DIM Mesi].&[00]

}

);

this= 0;

end scope

mdx script context execution

Hi everybody.

I have a mdx script on a cube involving several cells through subcube. The subcube takes the values of some cells and use them to calculate the values of some other cells.

I have a user who has limited access rights on the dimensions, so that he can't see all the cells of the cube.

But I need that all the users can perform all the calculations in the cube even if there are some members (used to evaluate the calculations) they are not allowed to.

I know that the calculations script are evalueted in the context of the user querying the cube.

I wonder if there is a command to make the script being evaluated in the context of a specified user, an administrator in this case (something like "execute as"). So that the calculations are applied first, and after that, the user with reduced rights can see only the cells he is allowed to...

thank you very much.

Sorry, but there is no 'Execute As' feature and I can't think of anyway that you could reference a secured cell in a calculation.|||

Actually one possible solution to this would be to write a .Net stored procedure, that would would use AdoMd to connect back to the user with a new connection and execute an MDX query and return the results. When you deploy a stored proc assembly you have the option of specifying the account to impersonate.

Performance could be an issue with this approach as there is a bit of overhead involved in calling out to a stored procedure, but if your calc is at a fairly high level this might be a solution.

|||

Well, you give me a wisdom, thank you very much!

A more question, because I've never used a .Net stored procedure for Analysis Services. Do you mean that through it I can make the calculations in the script of the cube being executed by a specified user with all the necessary rights, and, after the calculations are realized on all the involved cells, then I can make the application to connect back as the current user to query the cube (therefore, seeing the previous results but only for the allowed cells)?

It'd be great.

have you any examples?

Thank you very very much!!!!

|||Check out this link for examples: http://www.codeplex.com/ASStoredProcedures|||

The AS Stored Procedure project has some good examples (I wrote some of them). However I don't think we have any that do exactly what you are after.

What would happen is that from a "limited" user, they would request a value.

That value would be calculated by a stored procedure.

When the stored procedure is deployed it can be configured to run under a specific user account.

In the stored proc it would run .Net code to run a query back against the current cube, but using a new connection (running as the account specified when the assembly was deployed). It would have to issue a full MDX query.

This would return a cellset and the stored proc would extract the value of the required cell and return that value|||

Thank you very much again.

You say: “

v In the stored proc it would run .Net code to run a query back against the current cube, but using a new connection (running as the account specified when the assembly was deployed). It would have to issue a full MDX query.

v This would return a cellset and the stored proc would extract the value of the required cell and return that value

“

But I wonder if in this way the “poor” user (with limited rights) in Role1, issuing the query through the specified user for th AS SP, would be allowed to see also the cells he is forbidden to… In fact I need him to see only the cells he is allowed to, but with the values calculated with calculations involving all the cells (even the ones he is not allowed to). Is this possible this way?

However, I give you an example of what I need to do. Let’s say I need to do something like in the following script (a very simplified version of my script, but just to understand the type of calculation).

I have a dimension [DIM Mesi] with four key members [01], [02], [03], [00] and a one-level attribute hierarchy [Dim Mesi].

Role1 can access only the “[DIM Mesi].[DIM Mesi].&[01]”, “[DIM Mesi].[DIM Mesi].&[02]” and the ”existing” with these ones (I mean the ancestors of these members, which in this case, is the AllMember).

There is a basket member [DIM Mesi].[DIM Mesi].&[00] from where I take the values to be reversed on the three members [01], [02], [03] from the Measure [Costi1] into the same measure (or even in a different measure).

The calculations are something similar to the ones below.

I’d like the users of Role1 be allowed to see 1/3 of the values of cells of the &[00] member in the cells of &[01] and &[02], but they should not be able to see nor the &[00] member related cells neither the &[03] member related cells.

Further, the [DIM Mesi].[DIM Mesi].&[00] should be assigned a 0 value (even if the user can’t see that value) so that the total is not twice, in case I don’t use VisualTotals.

If possible, I’d like to even be able to decide whether he must see the Visual Totals or not, but I suppose that through the AS SP executed by another user I can only see the Actual Totals, or am I wrong?

THANK YOU VERY MUCH for your kind help!

CALCULATE;

CREATE SET CURRENTCUBE.[GoodMembers]

AS {

iif(IsError(StrToMember("[DIM Mesi].[DIM Mesi].&[01]")), {}, [DIM Mesi].[DIM Mesi].&[01]),

iif(IsError(StrToMember("[DIM Mesi].[DIM Mesi].&[02]")), {}, [DIM Mesi].[DIM Mesi].&[02]),

iif(IsError(StrToMember("[DIM Mesi].[DIM Mesi].&[03]")), {}, [DIM Mesi].[DIM Mesi].&[03])

};

scope

(

{[Measures].[Costi1]}

,

{

[GoodMembers]

}

);

this= [DIM Mesi].[DIM Mesi].&[00]/3

;

freeze(this);

end scope;

scope

([Measures].[Costi1],

{

[DIM Mesi].[DIM Mesi].&[00]

}

);

this= 0;

end scope