MDX Aggregate DImensions to filter - mdx

I'm new to mdx and need your help:
[Item].[Segment] [Country].[World] [Measures].[Periodic]
1 Region A 150
2 Region B 60
3 Region C 1400
4 Region D 20
I have two dimensions Segment and World. If I take only world, I get no values. But I want to achieve to combine the two dimensions to one dimension on segment level as following:
[Item].[Segment] [Measures].[Periodic]
1 150
2 60
3 1400
4 20
Would an aggregation be useful in this case?
Thanks in advance!
The Structure is like following:
Cube_Structure
--> I need to combine both dimensions Segment and World in order to have one dimension on the row which shows me the values for the segments only!

Related

How to add a metric for top n count in mdx

I want to create a metric which will show only the top 10 result whenever applied.
Suppose the data is
item Price
A 20
B 45
C 50
D 80
E 10
F 90
G 85
H 55
I 40
J 100
I want to show the top 5 result in descending order. So, the expected result is:
j-100
f-90
g-85
d-80
h-55
other-165
I am already getting the result with the following MDX query:
With
Set [Top10] AS
(TOPCOUNT({ORDER( ({[DimProduct].[item].[All].Children})
,([Measures].[Price]),BDESC)},10))
MEMBER [DimProduct].[item].[OtherAll] AS
(avg({EXCEPT([DimProduct].[item].Members, [Top10])})
)
Select
[Measures].[ Price] on Columns,
{
[Top10]
,[DimProduct].[item].[OtherAll]
} on Rows
FROM [testcube]
Result:
j-100
f-90
g-85
d-80
h-55
other-165
I basically want to create a metric with the above query and save it to my cube solution.
So, when I drag item and price it will show all the data i.e all 10 rows.
A 20
B 45
C 50
D 80
E 10
F 90
G 85
H 55
I 40
J 100
And, when we drag our newly created metric then it will show top 5 result with the other row (other will be sum of rest of the rows)
j-100
f-90
g-85
d-80
h-55
other-165
Is there any way to achieve this functionality?
Edit 1
Created one dynamic set with top 10
Created calculated measure for others
Created another dynamic set to show both the results i.e top 10 and others.
But when we select the dynamic set to show top 10 + others, it is throwing the error:
A set has been encountered that can not contain calculated members

DAX SUMX over 2 levels

This should be easy but I must be missing something obvious. I have the following PowerPivot table:
Level 1 Level 2 Amount
------- ------- ------
A X 100
A Y 200
B X 400
B Y 600
I'm trying to build a measure (not a calculated column) that iterates over each item in Level 2 and divides the amount by the subtotal of all amounts for Level 1 equal to the current Level 1 amount.
In other words, I want the measure to do this
Level 1 Level 2 Measure
------- ------- ------
A X 100 / 300
A Y 200 / 300
B X 400 / 1,000
B Y 600 / 1,000
I have tried this:
AmountSum:=SUM(Table1[Amount])
Measure:= SUMX(Table1,
[AmountSum]/CALCULATE(SUMX(Table1, [AmountSum]), ALL(Table1[Level 2])))
This and other iterations I've tried always end up showing 1 for each calc meaning that (or so it appears) only the initial Level 2 row is in scope when calc'ing the denominator, despite my use of ALL.
Thanks in advance for any advice people can offer!
Edit:
Assume that the [AmountSum] logic is dependent on and needs to be calculated at the row level for the Level 2 items. It was simplified in the example above, but in reality it is dependent on the Level 2 data and at a Level 1 level should only be adding up results calculated at a Level 2 level.
Try this:
AmountSum:=SUM(Table1[Amount])
Measure:= DIVIDE([AmountSum], CALCULATE([AmountSum], ALL(Table1[Level 2])))
If it has to be calculated at the leaf level:
AmountSum:=SUMX(Table1, <YourMoreComplexCalcHere>)
Measure:= DIVIDE([AmountSum], CALCULATE([AmountSum], ALL(Table1[Level 2])))

sql more complicated querying measurements

I have two tables (sql server), as shown below:
locations
id cubicfeet order
-------------------------------------
1 5 1
2 10 1
3 6 1
items
id cubic feet order
--------------------------------------
1 6 1
2 6 1
3 6 1
I need a query to tell me if all the items will fit into all the locations (for a given order). If all items will not fit into 1 or all locations then I need to create a new location for that given order - and then move any items that DID fit into the locations before to the new location (as many as fit). The new location will only be given a certain amount of cubic feet also - say 17. In this example, sum won't work because all 3 records are 6 so the sum is 18, which is less than the sum of 5,10,6, but the location with volume 5 can't fit any of the items since they are all volume 6 cubic feet.
the only way I think I can do it is creating temp tables in my sp and using a while loop to go through them and update the locations 1 at a time to see if it still fits more...

SPSS Compute Variable

Below is some data:
Test Day1 Day2 Score
A 1 2 100
B 1 3 62
C 3 4 90
D 2 4 20
E 4 5 80
I am trying to take the values from column 'day' and 'day2' and use them to select the row number for the column score. For example for Test A I would like to find the sum of 100 and 62 because that is the values of the first and second rows of score. Test B I would like to find the sum of 100, 62 and 90.
Is their anyway to do this in the Compute Variable window? Found in the menu Transform-Compute Variable?
I tried the following:
Score(MEAN(VALUE(Day1), VALUE(DAY2)))
This is not the proper way to call the cell location of Score and I received an error.
Can anyone help?
Thank you!
You really have two different datasets here. One is a dataset of scores numbered 1 through 5.
The other is a dataset that includes indexes into the score dataset. So the steps would be something like this.
First take the scores dataset and transpose it so that it has one row and 5 columns (Data>Transpose)
Then match that dataset to each case in the main dataset (Data>Merge Files>Add Variables).
Next you have to resort to using syntax directly.
You would declare a vector for the scores (VECTOR)
Finally, you use COMPUTE to index into the scores.
For your real problem, I suppose that you might have batches of scores and maybe there are some gaps. The Restructure Data Wizard can help you generalize this - convert cases into variables, but let's not go there yet.
HTH,
Jon Peck

how to find Sum(field) in condition ie "select * from table where sum(field) < 150"

I have to retrieve only particular records whose sum value of size field is <=150.
I have table like below ...
userid size
1 70
2 100
3 50
4 25
5 120
6 90
The output should be ...
userid size
1 70
3 50
4 25
For example, if we add 70,50,25 we get 145 which is <=150.
How would I write a query to accomplish this?
Here's a query which will produce the above results:
SELECT * FROM `users` u
WHERE (select sum(size) from `users` where size <= u.size order by size) < 150
ORDER BY userid
However, the problem you describe of wanting the selection of users which would most closely fit into a given size, is a bin packing problem. This is an NP-Hard problem, and won't be easily solved with ANSI SQL. However, the above seems to return the right result, but in fact it simply starts with the smallest item, and continues to add items until the bin is full.
A general, more effective bin packing algorithm would is to start with the largest item and continue to add smaller ones as they fit. This algorithm would select users 5 and 4.
What you're looking for is a greedy algorithm. You can't really do this with one SQL statement.
It's similar to the subset sum problem. You are definitely going to be into exponential time ...
There are several ways to solve subset
sum in time exponential in N. The most
naïve algorithm would be to cycle
through all subsets of N numbers and,
for every one of them, check if the
subset sums to the right number. The
running time is of order O(2^N*N), since
there are 2N subsets and, to check
each subset, we need to sum at most N
elements.
Unless you can constrain the problem to smaller subsets.
According to your definition as it stands you could get any of these tables:
userid size userid size
1 70 2 100
userid size userid size
3 50 4 25
userid size userid size
5 120 6 90
userid size userid size
1 70 2 100
3 50 3 50
userid size userid size
1 70 2 100
4 25 4 25
userid size userid size
1 70 4 25
3 50 6 90
4 25
userid size userid size
4 25 3 50
5 120 6 90
SQL sucks at guessing. Do you mean to say you want the most users who's total size is under a certain limit? You'll need to create a temp table of all the combinations of users, then select the ones who's total size is less then the limit, then select the one with the most users, and possibly the lowest user ID or something. Either way, it won't be fast due to the first step.
But do you want to maximize the number of results or minimize or you simply don't care? first two cases is constraints optimization for which there should be solution using SQL, the latter (as mentioned above) requires greedy strategy.