Convert SQL query to MDX - have Group by & Count Functions - sql

I have the following SQL query which I am trying to convert into MDX:
select avg(skucount)
from
(
SELECT count(distinct [SKUCode]) as skucount
--,[SHOPCODE_WITHOUT_DIST]
FROM [HFPL_DW].[dbo].[FactSecondarySales]
where DISTCODE in
(
SELECT [DISTRIBUTORCODE]
FROM [HFPL_DW].[dbo].[DimDistHierarchy]
where REGION = 'KARACHI'
)
and month(saledate) = 7 and year(saledate) = 2018
group by [SHOPCODE_WITHOUT_DIST]
) as inner_query
The inner query returns the count of SKU saled on each shop(which is fulfilled by using "Group by ShopCode")
First I am trying to convert the inner query to MDX, I have tried the following:
WITH MEMBER [Measures].[SKU Count] AS
COUNT( NonEmpty( { [Product Hierarchy].[SKU].[SKU].Members }, ( [Shop Hierarchy].[SHOPCODE WITHOUT DIST] ) ) )
SELECT
{
[Measures].[SKU Count]
} ON COLUMNS,
NonEmpty(
{ [Product Hierarchy].[SKU].[SKU].Members },
([Shop Hierarchy].[SHOPCODE WITHOUT DIST] )
) ON ROWS
FROM
[Consolidated Sales]
where(
[Time Analysis].[Month].&[2018-07-01T00:00:00],
[Distribution Hierarchy].[DISTRIBUTORCODE].&[1002]
)
Reference: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/51988607-78cc-4520-88db-c6d3e99dd1fc/mdx-to-count-the-number-of-members-in-a-dimension-based-on-another-dimension?forum=sqlanalysisservices
It is not returning anything.
Kindly help me acheive the desired output of the Average SKUs saled(The outer query), the number of SKUs saled per shop (inner query)

Related

ssas value between two dates

I am trying to fetch the sums of tot_ack on MDX, so it would be shown on erit dimension and acc (account dimesion). However I can not figure out how to model the between dates. I am new to MDX.
select SUM(tot_ack) from
DBO.DIM_OBJ_ACC AS ACC INNER JOIN
DBO.FAKTA_SALDO AS saldo ON ACC.ACC_ID = saldo.ACC_ID INNER JOIN
DBO.DATE_OBJ_KP AS KP ON saldo.KP_ID = KP.KP_ID INNER JOIN
DBO.PERIOD AS PERIOD ON saldo.PERIOD = PERIOD.PERIOD
where
ACC.ACC _ID = '3001' and ACC.erit_ID = '1'
(DATEADD(day, 0,DATEADD(month, 11,DATEADD(year,2011-1900, 0))) BETWEEN KP.KP_DATE_START AND KP.KP_DATE_END) AND
(PERIOD.MONTH= 12 OR PERIOD .MONTH=0) AND (PERIOD.YEAR =2011)
My MDX-code
SELECT {[DIM OBJ ERIT].[ERIT ID].MEMBERS} ON COLUMNS,
{[DATE OBJ ACC].[ACC ID].members} ON ROWS
FROM [REPORT]
WHERE (Measures.[tot ack],
[PERIOD].[year].[2011],
[PERIOD].[month].[12],
[DATE OBJ KP].[KP DATE START].[Year].[2011],
[DATE OBJ KP].[KP DATUM END].[Year].[2011]
)
In mdx you specify range with ":". So your query should be
SELECT {[DIM OBJ ERIT].[ERIT ID].MEMBERS} ON COLUMNS,
{[DATE OBJ ACC].[ACC ID].members} ON ROWS
FROM [REPORT]
WHERE (Measures.[tot ack],
[PERIOD].[year].[2011],
[PERIOD].[month].[12],
[DATE OBJ KP].[KP DATE START].[Year].[2011]:[DATE OBJ KP].[KP DATUM END].[Year].[2011]
)
Take a look at the example below
select
[Measures].[Internet Sales Amount]
on columns,
[Product].[Category].[Category]
on rows
from [Adventure Works]
Result
Now lets filter it for 2013 -01-01 to 2013-01-10
select
[Measures].[Internet Sales Amount]
on columns,
[Product].[Category].[Category]
on rows
from [Adventure Works]
where
[Date].[Date].&[20130101]:[Date].[Date].&[20130110]
Result

MDX Query to get Last Year data without filtering based on current month

i have been doing a query to get previous year value in MDX but can't the the desired output. In sql i would do it like this. Question is how do i apply this query in MDX?
select data1, sum(data2) as sumthisyear, sumlastyear
from table1 a
left outer join
( select data1, sum(data2) 'sumlastyear'
from table1
where tableyear = 2017 group by data1
) b on a.data1 = b.data1
where tableyear= '2018' and datafilter = 'SampleFilter'
group by data1, sumlastyear
SSAS Cube Structure Image
I have done some research and did mdx function like parallelperiod but not giving the correct values as data are filtered based on 2018 year.
Here's my MDX query
CREATE MEMBER CURRENTCUBE.Measures.[SPLY Registered Sales] AS
iif( [DimProdDate].[Dates].CurrentMember Is
[DimProdDate].[Dates].[All].LastChild,
( PARALLELPERIOD( [DimProdDate].[Dates].[Calendar Year],
1,
Tail( NonEmpty( [DimProdDate].[Dates].[Full Date].MEMBERS,
[MEASURES].[Registered Sales]),
1
).Item(0)
),
[MEASURES].[Registered Sales] ),
( PARALLELPERIOD( [DimProdDate].[Dates].[Calendar Year],
1,
[DimProdDate].[Dates].CurrentMember
),
[MEASURES].[Registered Sales] )
);
Any help will be very much appreciated.
Thanks!

In MDX, the price rank of a given number is 0

I have an OLAP cube containing the type price sum for each of number.
Using MDX, how can I output the rank of a given number?
Result should be as follows:
Heare is the MDX query, but all rank values are 0. What is wrong in query?
WITH MEMBER [Measures].[Rank Sum of price] AS
RANK([NUM_1].[All Numbers].CURRENTMEMBER
,[NUM_1].[All Numbers]
,[Measures].[Sum of price])
SELECT
{
[Measures].[Sum of price]
, [Measures].[Rank Sum of price]
}
ON COLUMNS,
[NUM_1].[All Numbers]
ON ROWS
FROM schema1
Maybe do your ordering in a custom set before hitting the rank function:
WITH
SET OrderedNums AS
Order
(
[NUM_1].[All Numbers].[All Numbers].MEMBERS
,[Measures].[Sum of price]
,BDESC
)
MEMBER [Measures].[Rank Sum of price] AS
Rank
(
[NUM_1].[All Numbers].CurrentMember
,OrderedNums
)
SELECT
{
[Measures].[Sum of price]
,[Measures].[Rank Sum of price]
} ON 0
,[NUM_1].[All Numbers].[All Numbers].MEMBERS ON 1
FROM schema1;

Get data from two related fact tables based on common date dimension in MDX

We have two fact tables Fact_Order and Fact_Product.
In fact_Order we have columns OrderId,OrderDate and OrderQuantity.
In fact_Product we have columns ProductReleaseDate and ProductCost
Now we want to join the Releasedate with all the orders that fall within a week in MDX.
Help would be highly appreciated because i am stuck on this from last two days
This is the MDX that i am trying to write for the same.
WITH MEMBER [Week1 order] AS
FILTER
(
Measures.OrderQuantity,
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
SELECT [Week1 order] ON 0,
NON EMPTY
{
[Release Date].[Date].[Date],
FILTER
(
[DimOrder].[OrderID].[OrderID],
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
,FILTER
(
[Order Date].[Date].[Date],
[Release Date].[Date].currentmember : [Release Date].[Date].currentmember.LEAD(6)
)
} ON 1
FROM
[ProductReleaseCube]
Does this help?
WITH SET ValidCombOfDates AS
FILTER
(
NonEmpty
(
{
[Release Date].[Date].[Date].MEMBERS * [Order Date].[Date].[Date].MEMBERS
}, Measures.OrderQuantity
),
[Order Date].[Date].CURRENTMEMBER.MEMBER_VALUE - [Release Date].[Date].CURRENTMEMBER.MEMBER_VALUE <=7
)
MEMBER Measures.ExpectedOrderQuantity AS
IIF
(
ISEMPTY(Measures.OrderQuantity),
0,
Measures.OrderQuantity
)
SELECT
ValidCombOfDates * [DimOrder].[OrderID].[OrderID].MEMBERS
ON 1,
Measures.ExpectedOrderQuantity
ON 0
FROM
[ProductReleaseCube]

mdx put dimension member in measures

In a SSAS 2005 cube.
I have a product dimension, it has a attribute: sp (selling price)
I want to list the sp along with other facts for products. But the query below returns null for sp. Idea?
with member [measures].[sp] as
[Products].[Current Sp].currentmember
select {
[Measures].[Sold value]
, [measures].[sp]
} on 0
,
nonempty(
{[Sales order details].[Receipt No].[Receipt No].allmembers}
*{[Sales order details].[Line No].[Line No].allmembers}
*{[Products].[Product code].[Product code].allmembers}
, [Measures].[Sold value]
) on 1
from (
select [Time].[Day].&[20140430] on 0 from (
select [Branch].[Branch].&[2] on 0 from (
select [Sales order details].[Receipt No].[680207] on 0 from [Rmis]
)
)
)
update:
this is the final working query. I added [Products].[SKU].[SKU] because otherwise Current Sp returns 'All' (the null in original question is because of not using .Member_value). Current Sp and Product code are not related while they both related to [Products].[SKU].
with member [measures].[sp] as
[Products].[Current Sp].currentmember.MEMBER_value
select {
[Measures].[Sold value]
, [measures].[sp]
} on 0
,
nonempty(
{[Sales order details].[Receipt No].[Receipt No].allmembers}
*{[Sales order details].[Line No].[Line No].allmembers}
*{[Products].[Product code].[Product code].allmembers}
*{[Products].[SKU].[SKU]}
, [Measures].[Sold value]
) on 1
from (
select [Time].[Day].&[20140430] on 0 from (
select [Branch].[Branch].&[2] on 0 from (
select [Sales order details].[Receipt No].[680207] on 0 from [Rmis]
)
)
)
with member [measures].[sp] as
[Products].[Current Sp].CurrentMember.MemberValue
very close...just need to specify which property to display