ssas value between two dates - ssas

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

Related

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

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)

How to count YTD days in my MDX query

I've below MDX query which gives me YTD amount for each my device perfectly, but I want count of those YTD days taken in consideration while calculating YTD amount.
How can I achieve those YTD days counts in my query?
WITH
MEMBER [Settlement Date].[Calendar].[CalendarYTD] AS
Aggregate
(
YTD
(
[Settlement Date].[Calendar].[Settlement Calendar Month].&[201412]
)
)
SELECT
[Settlement Date].[Calendar].[CalendarYTD] ON COLUMNS
,NON EMPTY
[Device].[DeviceID].Children ON ROWS
FROM [cube1]
WHERE
[Measures].[amount];
OR
you can use below AW MDX query while making changes which give same results as my above query:
WITH
MEMBER [Date].[Calendar].[CalendarYTD] AS
Aggregate(YTD([Date].[Calendar].[Month].[March 2015]))
SELECT
[Date].[Calendar].[CalendarYTD] ON COLUMNS
,[Product].[Category].Children ON ROWS
FROM [Adventure Works]
WHERE
[Measures].[Order Quantity];
this is a little swapped around but hopefully heading in the direction you require:
WITH
MEMBER [Date].[Calendar].[CalendarYTD] AS
Aggregate(YTD([Date].[Calendar].[Month].[March 2007]))
MEMBER [Measures].[DaysCompleteCurrYear] AS
Descendants
(
YTD([Date].[Calendar].[Month].[March 2007])
,[Date].[Calendar].[Date]
,SELF
).Count
SELECT
{
[Measures].[DaysCompleteCurrYear]
,[Measures].[Order Quantity]
} ON COLUMNS
,[Product].[Category].Children ON ROWS
FROM [Adventure Works]
WHERE
[Date].[Calendar].[CalendarYTD];
If we put the target month into a single member set then the family relationships are preserved and we can use that set in the custom measures:
WITH
SET [targMth] AS
{[Date].[Calendar].[Month].[March 2007]}
MEMBER [Date].[Calendar].[CalendarYTD] AS
Aggregate(YTD([targMth].Item(0).Item(0)))
MEMBER [Measures].[DaysCompleteCurrYear] AS
Descendants
(
YTD([targMth].Item(0).Item(0))
,[Date].[Calendar].[Date]
,SELF
).Count
SELECT
{
[Measures].[DaysCompleteCurrYear]
,[Measures].[Order Quantity]
} ON COLUMNS
,[Product].[Category].Children ON ROWS
FROM [Adventure Works]
WHERE
[Date].[Calendar].[CalendarYTD];

MDX for Zero months in last 12 = Number of zero transacting months in the last 12 months rolling window

I am working on MDx query to count the number ZERO txn for each product for last 12 months from today's date. Prepared MDX query for Adventure works fine but my 2nd query running on my Acutal cube working correctly.
Can you please help me out to rectify the query or any filter needed to get correct results like 1st sample query?
1st Sample Query on Adventure Works: (working correctly)
WITH Member [Measures].[Months With Zero Sales] AS
COUNT(
FILTER(
DESCENDANTS(
{[Date].[Calendar].[Month].&[2008]&[1].LAG(12):[Date].[Calendar].[Month].&[2008]&[1]}, [Date].[Calendar].[Month]) ,
[Measures].[Sales Amount] = 0 ) )
SELECT {[Measures].[Months With Zero Sales]} ON 0,
[Product].[Product Model Lines].[Product Line].Members on 1
FROM [Adventure Works];
Product Lines Months With Zero Sales]
----------- -----------------
Accessory 0
Componenets 0
Mountain 0
Road 0
Touring 6
2nd Query on my Cube:
WITH Member [Measures].[Zero Months in last 12] AS
COUNT(
FILTER(
DESCENDANTS(
{[Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1].LAG(12):
[Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1]},
[Settlement Date].[Calendar].[Settlement Calendar Month]),
[Measures].[Transaction Count] = 0 ) )
SELECT {[Measures].[Transaction Count] ,[Measures].[Zero Months in last 12]} ON 0,
[Terminal].[terminal ID].members on 1
FROM [cubetxn]
I'm expecting if non zero where "transaction count" is (null), even if there is transaction count not null, there could be chances to have one month with empty tuples.
Can you please correct my query or someone can give me sample query which should able to give me total zero month txn count for last 12 month of each product.
Terminal ID Transaction Count Zero Months in last 12
---------- ----------------- -------------------------
All Terminals 504,112,053 0
Q1001 138,832 0
Q1002 (null) 0
Q1003 88,800 0
Q1004 (null) 0
First comment is that this is a quicker pattern to use when doing a count/filter:
WITH
MEMBER [Measures].[Months With Zero Sales] AS
Sum
(
Descendants
(
{
[Date].[Calendar].[Month].&[2008]&[1].Lag(12)
:
[Date].[Calendar].[Month].&[2008]&[1]
}
,[Date].[Calendar].[Month]
)
,IIF
(
[Measures].[Sales Amount] = 0
,1
,null
)
)
SELECT
{[Measures].[Months With Zero Sales]} ON 0
,[Product].[Product Model Lines].[Product Line].MEMBERS ON 1
FROM [Adventure Works];
Based on this more efficient approach does the following work?
WITH
MEMBER [Measures].[Zero Months in last 12] AS
Sum
(
Descendants
(
{
[Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1].Lag(12)
:
[Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1]
}
,[Settlement Date].[Calendar].[Settlement Calendar Month]
)
,IIF
(
[Measures].[Transaction Count] = 0
,1
,0
)
)
SELECT
{
[Measures].[Transaction Count]
,[Measures].[Zero Months in last 12]
} ON 0
,[Terminal].[terminal ID].MEMBERS ON 1
FROM [cubetxn];
If there is still a problem then why not reconstruct your script so you can manually check what to expect. So for the AdvWrks script I'd run this so that I could actually confirm that the 6 is correct:
SELECT
{[Measures].[Sales Amount]} ON 0
,
Descendants
(
{
[Date].[Calendar].[Month].&[2008]&[1].Lag(12)
:
[Date].[Calendar].[Month].&[2008]&[1]
}
,[Date].[Calendar].[Month]
)
*
[Product].[Product Model Lines].[Product Line].MEMBERS ON 1
FROM [Adventure Works];
So against your cube what does the following return?
SELECT
{[Measures].[Transaction Count]} ON 0
,
Descendants
(
{
[Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1].Lag(12)
:
[Settlement Date].[Calendar].[Settlement Calendar Month].&[2014]&[1]
}
,[Settlement Date].[Calendar].[Settlement Calendar Month]
)
*
[Terminal].[terminal ID].MEMBERS ON 1
FROM [cubetxn];
//Total Transactions count Weekend Sunday (i’ve date dimesion which holds day_in_week=’Sun’ for respective dates)
WITH SET AllTranSundays as
EXISTS(DATE.DATE.DATE.MEMBERS, DATE.DATE.day_in_week.&[Sun], "Sales")
//Count of all sundays which had transactions.
MEMBER Measures.CntAllTranSundays AS
COUNT(AllTranSundays)
//YTD Days = Count of days within calendar year to date
MEMBER Measures.CntDaysYTD as
COUNT(DESCENDANTS(ANCESTOR([DATE].[date].CURRENTMEMBER,
3), 3).item(0).item(0)
: [DATE].[date].CURRENTMEMBER)
//YTD Transactions = Transaction count year to date
MEMBER Measures.CntTranYTD as
COUNT(
EXISTS(
{DESCENDANTS(ANCESTOR([DATE].[date].CURRENTMEMBER,
3), 3).item(0).item(0)
: [DATE].[date].CURRENTMEMBER}, , "Sales"
)
//Zero months in last 12 = Number of zero transacting months in the last 12 months rolling window
MEMBER Measures.CntZeroTransactingMonths as
COUNT(
{ANCESTOR([DATE].[date].CURRENTMEMBER, 1).ITEM(0).LAG(12) : ANCESTOR([DATE].[date].CURRENTMEMBER, 1).ITEM(0)}
-
EXISTS({ANCESTOR([DATE].[date].CURRENTMEMBER, 1).ITEM(0).LAG(12) : ANCESTOR([DATE].[date].CURRENTMEMBER, 1).ITEM(0)}
, ,
"Sales")
)
//Consecutive zero months = Number of consecutive zero transacting months in last 12 months rolling window
Question Do you want a count of instances when consecutive months had zero transactions.
Or do you want a count of number of months which had consecutively no transactions?
What I am trying to say is say Jan, March, June, July, August, October, November had no transactions in the rolling last 12 months
As per case 1: the answer should be 2
As per case 2: the answer should be 5.
Will update my answer as per your clarification.
SELECT
{Measures.CntAllTranSundays, Measures.CntDaysYTD, Measures.CntTranYTD, Measures.CntZeroTransactingMonths} ON 0
FROM [YourCube]
where
[date].[date].[date].&[02/28/2015]

MDX to count common members - EXISTS alternative

Let me describe the issue giving example from [Adventure Works] cube.
Following MDX returns count of 17473
SELECT NON EMPTY { [Measures].[Internet Order Count] } ON COLUMNS,
[Internet Sales Order Details].[Sales Order Number] on ROWS
FROM [Adventure Works])
WHERE ( [Sales Reason].[Sales Reason].&[1] -- price
and following returns count of 3515
SELECT NON EMPTY { [Measures].[Internet Order Count] } ON COLUMNS ,
[Internet Sales Order Details].[Sales Order Number] on ROWS
FROM [Adventure Works]
WHERE ( [Sales Reason].[Sales Reason].&[2]) -- on promotion
I would like to count [Sales Order Number] which are common in [Sales Reason].&1 and [Sales Reason].&[2]
SQL equivalent would be:
select count(distinct f.SalesOrderNumber)
from FactInternetSales f
join FactInternetSalesReason fs
on f.SalesOrderNumber = fs.SalesOrderNumber and f.SalesOrderLineNumber = fs.SalesOrderLineNumber
where fs.SalesReasonKey = 1 and fs.SalesOrderNumber in
(select SalesOrderNumber from FactInternetSalesReason fs1 where fs1.SalesReasonKey = 2)
-- sales reason 1 = 17473
-- sales reason 2 = 3515
-- common 1689
I got common count using following mdx:
WITH MEMBER [Measures].[common] AS count
( exists ( exists ([Internet Sales Order Details].[Sales Order Number].[Sales Order Number].Members,
[Sales Reason].[Sales Reason].&[1],"Internet Orders"
),
[Sales Reason].[Sales Reason].&[2],"Internet Orders"
)
)
SELECT NON EMPTY [Measures].[common] ON COLUMNS
FROM [Adventure Works]
-- 1689
But use of EXISTS is rather slow for my requirement. Please suggest an alternative.
Also please see related thread here
Thank you
Please try adding both reasons to the WHERE clause as a single set:
SELECT
NON EMPTY
{[Measures].[Internet Order Count]} ON COLUMNS
,[Internet Sales Order Details].[Sales Order Number] ON ROWS
FROM [Adventure Works]
WHERE
{
[Sales Reason].[Sales Reason].&[2]
,[Sales Reason].[Sales Reason].&[1]
};
Here is an alternative that runs faster, only looks at the common orders, and does not use the EXISTS function:
WITH
SET [AllOrders] AS
[Internet Sales Order Details].[Sales Order Number].[Sales Order Number].MEMBERS
SET [OrdersIntersection] AS
Intersect
(
NonEmpty
(
[AllOrders]
,{
(
[Sales Reason].[Sales Reason].&[1]
,[Measures].[Internet Order Count]
)
}
)
,NonEmpty
(
[AllOrders]
,{
(
[Sales Reason].[Sales Reason].&[2]
,[Measures].[Internet Order Count]
)
}
)
)
MEMBER [Measures].[commonCount] AS
[OrdersIntersection].Count
SELECT
//NON EMPTY //<<not needed
[Measures].[commonCount] ON COLUMNS
FROM [Adventure Works];

Count an intersection where two dates match

I have several Role Playing dimensions that are FK's to the Date Dimension. How would I best count the intersection where a date from from Date Dimension matches the Date in a Role Playing Dimension?
In SQL it would be something like:
Select DimDate,
COUNT(1)
From DimDateTable D
join RolePlayingTable R on D.Date = R.Date
Group by DimDate
I thought this would work:
With
Member [Measures].[First Date In] as
(
[Measures].[Account Count],
[Sprocket Date].[Date].CurrentMember
)
Member [Measures].[First Date Out] as
(
[Measures].[Account Count],
[Widget Date].[Date].CurrentMember
)
Select
{
[Measures].[First Date In],
[Measures].[First Date Out]
} on 0,
Non Empty
[Dim Calendar].[Date].[Date] on 1
From [Cube-Bert]
But it only counts the intersecting points and not where the Sproket.Date = Calendar.Date
I also tried this, but it's not right either:
with
Member [Measures].[Count] as
Sum(
Exists(
{[Sproket Date].[Date].[Date]},
{[Dim Calendar].[Date].[Date]}
)
,[Measures].[Account Count]
)
select [Measures].[Count] on 0,
Non Empty
[Dim Calendar].[Date].members on 1
from [Cube-Bert]
I put the date matching logic into the DSV and produced a flag column in the fact table, 1 = true, 0 = false.