How to merge two MDX query results having different measures and filters? - ssas

Let's start by saying that I'm a total newbie on MDX, I need to merge two (or more) query results into one pivot.
The queries will have the same dimensions on ROWS and COLUMNS, but different measures and filters (normally a time period).
Here is an example
Query 1:
SELECT
NON EMPTY {{[stores].[storecountry].[storecountry].Members}} ON COLUMNS,
NON EMPTY {{[SalesTypes].[Description].[Description].Members}} *
{[Measures].[TransactionValue], [Measures].[TransQty]} ON ROWS
FROM [Model]
WHERE ({[dDates].[Date].[Date].&[2016-01-05T00:00:00] : [dDates].[Date].[Date].&[2016-01-12T00:00:00]})
Result of query 1:
CA US
Regular Sale TransactionValue 761 16
Regular Sale TransQty 8 233
Return TransactionValue 156 4
Return TransQty 1 45
Query 2:
SELECT
NON EMPTY {{[stores].[storecountry].[storecountry].Members}} ON COLUMNS,
NON EMPTY {{[SalesTypes].[Description].[Description].Members}} *
{[Measures].[DiscountPerc]} ON ROWS
FROM [Model]
WHERE ({[dDates].[Date].[Date].&[2015-03-12T00:00:00] : [dDates].[Date].[Date].&[2015-06-02T00:00:00]})
Result of query 2:
CA US
Regular Sale DiscountPerc 40 % 59 %
Return DiscountPerc 32 % 43 %
Expected result after merging
CA US
Regular Sale TransactionValue 761 16
Regular Sale TransQty 8 233
Regular Sale DiscountPerc 40 % 59 %
Return TransactionValue 156 4
Return TransQty 1 45
Return DiscountPerc 32 % 43 %
Is it achievable without manually merging the AdomdClient.CellSet from the calling application?
Thank you!

I'd use calculated members:
with
Member [Measures].[TransactionValueReport] as
Aggregate(
{[dDates].[Date].[Date].&[2016-01-05T00:00:00]:[dDates].[Date].[Date].&[2016-01-12T00:00:00]},
[Measures].[TransactionValue]
)
Member [Measures].[TransQtyReport] as
Aggregate(
{[dDates].[Date].[Date].&[2016-01-05T00:00:00]:[dDates].[Date].[Date].&[2016-01-12T00:00:00]},
[Measures].[TransQty]
)
Member [Measures].[DiscountPercReport] as
Aggregate(
{[dDates].[Date].[Date].&[2015-03-12T00:00:00]:[dDates].[Date].[Date].&[2015-06-02T00:00:00]},
[Measures].[DiscountPerc]
)
Select
Non Empty [stores].[storecountry].[storecountry].Members on 0,
Non Empty [SalesTypes].[Description].[Description].Members * {[Measures].[TransactionValueReport],[Measures].[TransQtyReport],[Measures].[DiscountPercReport]} on 1
From [Model]

Related

MDX. Mixing measures with dimensions

Have multiple departments, which have monthly sales' measure and "Inception to Date" (ITD) and "Year to Date" (YTD) sales' data measures.
SELECT {[Dep1], [Dep1ITD], [Dep1YTD], [Dep2], [Dep2ITD], [Dep2YTD]} ON ROWS,
{[Calendar Date].[Calendar yyyy-MMM].[Calendar yyyy-MMM].ALLMEMBERS} ON COLUMNS
FROM (
SELECT ( { [Calendar Date].[Calendar yyyy-MMM].&[202206]:[Calendar Date].[Calendar yyyy-MMM].&[202209]}
) ON COLUMNS
FROM [MyCube])
Code above produces following result:
I do not have a need to show all YTD & ITD data, I need to show it only once - before the beginning of the period. Like this:
YTD & ITD are calculated from the May-2022 numbers minus May's sales.
In this example:
"Dep1 ITD": 538 = 543 - 5
"Dep2 ITD": 288 = 292 - 4
"Dep1 YTD": 23 = 28 - 5
"Dep2 YTD": 14 = 18 - 4
Is it possible to do with MDX?
If so, how complicated it would be?
Will it be faster than extract two separate data sets and link them in a front-end application?

Query to calculate the proportion [duplicate]

This question already has answers here:
Division of two statements with count returns zero
(2 answers)
Closed last year.
I am new to SQL (using SQLiteStudio) and I wrote such a query to calculate Proportion for each group ( number of rooms) from the total sale of apartments. But it doesn't calculate correctly. Give a result of 100 for each group. I don't know what I'm doing wrong can you guys help in any way? Thanks!
1. SELECT
2.l.num_rooms as "Number",
3.count(l.id_locale) as "Number sales",
4.round(count (l.id_locale)/
5.(SELECT count (l.type_locale)FROM locale
6.WHERE l.type_locale = 'Appartement' )*100,2) as "Proportion"
7.FROM locale l
8.WHERE l.type_locale = 'Appartement'
9.GROUP BY l.num_rooms
10.ORDER BY l.num_rooms;
type_locale
surface
num_rooms
fk_lieu
id_locale
Appartement
52
1
45
1
Appartement
45
2
67
21
Appartement
12
3
34
78
Appartement
67
2
89
12
Appartement
31
1
15
56
I see two problems in your code: 1) You are using and integer division (2/5 = 0); 2) the subquery is a correlated one while it should be unrelated to a main query. So use another alias in the subquery and convert at least one of the arguments in the expression to real.
SELECT l.num_rooms as "Number",
count(l.id_locale) as "Number sales",
round(count(l.id_locale)*1.0 /
(SELECT count(l2.id_locale) FROM locale l2
WHERE l2.type_locale = 'Appartement' )*100,2) as "Proportion"
FROM locale l
WHERE l.type_locale = 'Appartement'
GROUP BY l.num_rooms
ORDER BY l.num_rooms;

MDX script - mix empty and non empty on the same axis

I would like to ask your help to understand the main logic of MDX query. I would like to query different dimensions from the cube. But: in some case I would like to hide the NULL values, but in other dimension it is still needed. In my case I would like to provide date weeks and it dates for every day within the time period. (Even if it has no data) The content would be order number, position number and company and of course the KPI: OTD_customer. I tried many times to fix the null value issue, but I'm beginner on MDX. I tried to use NON EMPTY keyword and also Nonempty function, but still don't have the wished result. One more complicated thing is to filter the date for time period: based on actual day the last 15 weeks. (this part is working fine)
> SELECT NON EMPTY
> { [OTD_customer_new] } ON COLUMNS, NON EMPTY
> {
> (
> (( strtoset("LASTPERIODS(15,([Date - Plan Delivery Date].[Calendar Week].&[" + left(ltrim("2017 KW 10"),4) +" KW " + right(ltrim("2017 KW 10"),2) + "]) ) ")
> , [Order Number].[Order Number].[Order Number].ALLMEMBERS
> , [Position Number].[Position Number].[Position Number].ALLMEMBERS
> ,[Date - Plan Delivery Date].[Day of the Week].ALLMEMBERS
> ,[Group Structure].[Group Structure].ALLMEMBERS
> ) )
> *
> ([Date - Plan Delivery Date].[Date].[All].FirstChild:
> tail
> ( filter
> ( [Date - Plan Delivery Date].[Date].[All].Children,
> [Date - Plan Delivery Date].[Date].CurrentMember.Name <= Format(Now(), "yyyyMMdd")
> ), 1
> ).item(0)
> ))
> }
> ON ROWS
> FROM [ProductionCube]
Shall I use the filter in where condition? what is the best way to query the result from the cube? When it will be in normal relation database I would use a simple joins for this purpose.. But here in MDX I don't see the possibility with left and inner joins in script. Based on my understanding the hole axis may have the possibility to filter the null values. Do you have any idea? (thank you for reading) Current result example can be found here
The Non Empty function designed to filter empty axes (when your output is calculated). The NonEmpty function does the calculation of set. So, if you run:
Non Empty {Set1} * {Set2}
You'll get only crossjoined non-empty records.
If you run:
NonEmpty({Set1} * {Set2}, [Measures].[NonEmptyMeasure])
You'll get the same result, but the mechanic is a bit different. If you want to filter only specific sets you may use NonEmpty around these sets:
{Set1} * NonEmpty({Set2}, [Measures].[NonEmptyMeasure])
Set1 won't be filtered. Nonetheless, all members of Set1 will crossjoined with all non-empty members of Set2. Most likely that's not what you want.
You have many extra attributes joined to your date. What do expect them to return for empty records? I may guess you want to return All member, say, Set1 is a date set and Set2 is a company set:
NonEmpty({Set1} * {Set2}, [Measures].[NonEmptyMeasure]) + { {Set1} - NonEmpty({Set1}, [Measures].[NonEmptyMeasure])} * {Set2}.Item(0).Parent
Explanation:
1. NonEmpty({Set1} * {Set2}, [Measures].[NonEmptyMeasure]) -- returns non-empty records
2. {Set1} - NonEmpty({Set1}, [Measures].[NonEmptyMeasure]) -- returns empty dates
3. {Set2}.Item(0).Parent -- returns All member
The result will look like the following:
|=====================================|
| 2016 week 51 | Company1 | 1 |
| 2016 week 51 | Company2 | 1 |
| 2016 week 51 | Company3 | 1 |
| 2016 week 52 | All companies | NULL |
=======================================
Is it the wished result?

Filter non empty members on row

I have the following MDX query:
select
NON EMPTY [Measures].Members ON COLUMNS,
NON EMPTY {[MY_DIMENSION.MY_HIERARCHY].[VALUE].Members} ON ROWS
from
[MY_CUBE]
It gives the following result:
[Measures].[COUNT_TICKET]
-------------------------------------------------------------
[MY_DIMENSION.MY_HIERARCHY].[#null] 14 333 458 (<-- not needed)
[MY_DIMENSION.MY_HIERARCHY].[VAL1] 4 864
[MY_DIMENSION.MY_HIERARCHY].[VAL2] 5 588
[MY_DIMENSION.MY_HIERARCHY].[VAL3] 2 567
[MY_DIMENSION.MY_HIERARCHY].[VAL3] 4 500
Which takes a long time because there are a big number of null values out there.
Is it possible to filter the #null Members of my hierarchy?
Try the following
select
NON EMPTY {[Measures].Members} ON COLUMNS,
NON EMPTY {[MY_DIMENSION.MY_HIERARCHY].[VALUE].Members} ON ROWS
from
[MY_CUBE]

How to filter measures in MDX

I need to filter the measure values as
MeasureA MeasureB
10 10
15 15
5 20
20 20
Here I need to get only the measures are not equal, I am using filter function as but not working
Select Filter({[Measures].[A],
[Measures].[B]},
([Measures].[A]-
[Measures].[B])=0)
on 0
from [Cube]
Expected result set
MeasureA MeasureB
5 20
What am I missing?
Try using a dimension instead of your measures for the first part of the filter statement. Assuming you are querying products then your query might look like:
select {[Measures].[A],[Measures].[B]} on columns,
filter ({[Products].Members},[Measures].[A] = [Measures].[B]) on rows
from [Sales Cube]
You might want to try creating a calculated field in the DSV for this Fact table...
CASE
WHEN MeasureFieldA != MeasureFieldB THEN 1
ELSE 0
END
Then you can create a "fact dimension" and have this calculated field as an attribute to be used in your queries or calculated measures.