MDX Running total on every date member - ssas

I use the following MDX calculation to get the stock value per day:
SUM({NULL:[Date].[Calendar Hierarchy].CURRENTMEMBER} * [Measures].[Qty])
However this is giving me the following result
Date QtyOnHand
1/1/2015 10
4/1/2015 15
5/1/2015 16
Mark that the dates 2/1/2015 and 3/1/2015 are not shown, because there are no transactions on them.
I would like to get these shown to get the following result:
Date QtyOnHand
1/1/2015 10
2/1/2015 10
3/1/2015 10
4/1/2015 15
5/1/2015 16
How can I adjust my MDX statement to this?

Does IsEmpty help?
SUM(
{NULL:[Date].[Calendar Hierarchy].CURRENTMEMBER}
* IIF(
IsEmpty([Measures].[Qty])
,0
,[Measures].[Qty]
)
)
If it helps then it won't help performance.
I've tested the above and unsure why you do the cross-join inside the Sum - more standard is just to use the second argument.
This AdvWrks script is an example of your scenario i.e. there are gaps in the data:
SELECT
{[Measures].[Internet Sales Amount]} ON 0
,{
[Date].[Calendar].[Date].&[20050801]
:
[Date].[Calendar].[Date].&[20051201]
} ON 1
FROM [Adventure Works];
Here is the query using IsEmpty (with IIF):
WITH
MEMBER [Measures].[roll] AS
Sum
(
{[Date].[Calendar].[Date].&[20050801] : [Date].[Calendar].CurrentMember}
,IIF
(
IsEmpty([Measures].[Internet Sales Amount])
,0
,[Measures].[Internet Sales Amount]
)
)
SELECT
{[Measures].[Internet Sales Amount],[Measures].[roll]} ON 0
,{
[Date].[Calendar].[Date].&[20050801]
:
[Date].[Calendar].[Date].&[20051201]
} ON 1
FROM [Adventure Works];
Some of the results are the following:
So in your scenario I think this is very similar:
SUM(
{NULL:[Date].[Calendar Hierarchy].CURRENTMEMBER}
, IIF(
IsEmpty([Measures].[Qty])
,0
,[Measures].[Qty]
)
)

Related

MDX order doesn't work

I'm using the AdventureWorks Cube and MS SQL 2017 to learn MDX.
I'd like to return the top five selling subcategories of products, based on Internet Sales Amount:
SELECT Subset
(Order
([Product].[Product Categories].[SubCategory].members
,[Measures].[Internet Sales Amount]
,DESC
)
,0
,5
)
ON 0
FROM [Adventure Works]
The returned results are not sorted properly. Is there anything I did wrong?
Road Bikes Mountain Bikes Touring Bikes Tires and Tubes Helmets
$29,358,206.96 $26,492,684.38 $10,451,490.22 $925.21 $258,712.93
I also tried BDESC, seems to get the same result.
This query will help explain what happened. It was sorting correctly, but then it was displaying the default measure on the screen since you didn't specify the measure to display, only the measure to sort by:
SELECT {[Measures].[Internet Sales Amount], [Measures].DefaultMember} on 0,
Subset
(Order
([Product].[Product Categories].[SubCategory].members
,[Measures].[Internet Sales Amount]
,DESC
)
,0
,5
)
ON 1
FROM [Adventure Works]
I would recommend using that query but removing [Measures].DefaultMember and you should get the results you expect.
You want the TOPCOUNT MDX function
SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
, TOPCOUNT([Product].[Subcategory].[Subcategory].MEMBERS,5,[Measures].[Internet Sales Amount]) ON ROWS
FROM
[Adventure Works]

Sum of Previous months Sales with MDX

The below query works okay.
With Member PyMonthSales as
SUM(PARALLELPERIOD([Date].[Calendar].[Month], 1),[Measures].[Internet Sales Amount])
select
{PyMonthSales} on Columns,
[Date].[Calendar].[Month] on Rows
FROM
(
Select {
[Date].[Calendar].[Month].&[2006]&[5], [Date].[Calendar].[Month].&[2006]&[4]
} on Columns
From [Adventure Works]
)
But I just want one Grand total. So I removed the rows, on the above query
With Member PyMonthSales as
SUM(PARALLELPERIOD([Date].[Calendar].[Month], 1),[Measures].[Internet Sales Amount])
select
{PyMonthSales} on Columns
FROM
(
Select {
[Date].[Calendar].[Month].&[2006]&[5], [Date].[Calendar].[Month].&[2006]&[4]
} on Columns
From [Adventure Works]
)
This is where the problem starts. Returns null. Any assistance would be great.
Thanks for your time.
Naz
It ended up a bit messy!!
WITH
MEMBER [Measures].x AS
Aggregate
(
Generate
(
{
[Date].[Calendar].[Month].&[2006]&[5]
,[Date].[Calendar].[Month].&[2006]&[4]
}
,{
ParallelPeriod
(
[Date].[Calendar].[Month]
,1
,[Date].[Calendar].CurrentMember
)
}
)
,[Measures].[Internet Sales Amount]
)
SELECT
x ON 0
FROM [Adventure Works];
The thing I kept forgetting was the curly brackets around the ParallelPeriod section - without those an exception is raised because Generate always wants a SET as its second argument.

MDX query for bottomcount function

SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
,BottomCount
(
NonEmpty([Customer].[Customer].[Customer].MEMBERS)
,10
,[Measures].[Internet Sales Amount]
) ON ROWS
FROM [Adventure Works]
WHERE
[Date].[Calendar].[Calendar Year].&[2005];
The above query is showing me result having last 10 customer names with NULL measure value. I am using Adventure works Cube.
As per my understanding "bottomcount" is not working fine with where clause in "mdx" query. I want to fetch last 10 customer names for 2005 with measure value and hopefully i am looking for solution without using keyword "DESCENDANTS".
Please let me know in case, I am doing something wrong.
Give this a try:
bottomcount(
nonempty(
[Customer].[Customer].[Customer].Members
,[Measures].[Internet Sales Amount]
)
,10
,[Measures].[Internet Sales Amount]
)
You were getting the Customer names with NULL because in OLAP you are doing the cartesian product between the 2 dimensions (Measures is a special dimension, but it still acts as a dimension) it does not matter that you don't have values it will still crossjoin the members in your 2 sets and unless you request just the nonempty ones it will still give you the NULLs.
And if you'd like their full internet sales amount then move some of the logic into the WITH clause:
WITH
SET bot AS
NonEmpty
(
[Customer].[Customer].[Customer].MEMBERS
,(
[Measures].[Internet Sales Amount]
,[Date].[Calendar].[Calendar Year].&[2005]
)
)
SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
,BottomCount
(
bot
,10
,[Measures].[Internet Sales Amount]
) ON ROWS
FROM [Adventure Works];

Count Working Days MDX

I would like to count the working days of a spesific time range and then find the Average Daily Dispatches.Currently the time range is at WHERE statement.
I believe that I have include the date range in the 1st Member but I can't figure how to count the dates in a month range.
Any suggestments?
WITH MEMBER [Measures].[Working Days] AS
COUNT(Date.[Working Date].&[1])--Doesn't work
MEMBER [Measures].[Average Daily Dispatches] AS
[Measures].[Total Dispatches]/[Measures].[Working Days]
SELECT [Measures].[Average Daily Dispatches] ON 0
FROM [cube]
WHERE (
[Date].[Month].&[2015-01-01T00:00:00]:[Date].[Month].&[2015-08-01T00:00:00]
);
Use the NonEmpty function to yield those working dates on which there was a dispatch. Then use COUNT on top of this.
WITH MEMBER [Measures].[Working Days] AS
COUNT
(
NonEmpty
(
[Date].[Working Date].MEMBERS,
[Measures].[Total Dispatches]
)
)
Some sort of alternative seems to exist using Exists/Existing (I think 1 is Monday in the AdvWrks cube!)
WITH
MEMBER [Measures].[MondayCnt] AS
Count
(
Exists
(
(EXISTING
[Date].[Calendar].MEMBERS)
,[Date].[Day of Week].[1]
)
)
MEMBER [Measures].[Averge] AS
[Measures].[Internet Sales Amount] / [Measures].[MondayCnt]
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[MondayCnt]
,[Measures].[Averge]
} ON 0
,
[Date].[Calendar].[Month].&[2006]&[6]
:
[Date].[Calendar].[Month].&[2007]&[6] ON 1
FROM [Adventure Works];
The above results in the following:

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]