Comparing two date fields in MDX - mdx

I have sql query like:
Select Count(*)
from [Response]
where ResponseEndDate < ValidDate
How do I write this in MDX?

WITH MEMBER [Measures].cnt
AS
([Employee].[Department Name].MEMBERS
,[Employee].[Start Date].&[2002-02-05T00:00:00]
:
[Employee].[Start Date].&[2003-02-05T00:00:00]).COUNT
SELECT
[Measures].cnt ON 0
FROM [Adventure Works]

Related

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.

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:

Checking Multiple condition in case statement using MDX query

How to write the below case statement in mdx.I moved the below mentioned columns in the fact table.
sum(case when IsSummary = 1 and IsAudited = 1 and FinalAuditFlag = 1 then 1
else 0 end) AuditsCompleted,--Count - Completed Audits
i tried the below MDX query.
WITH
MEMBER [Measures].[count]
AS
(
case ([Measures].currentmember )
when 0 then ([Measures].[Is Summary] )
else 1
end
)
Select
{[Measures].[count]} on 0,
[Dim_table1].[TableId].members on 1
from [Dsv]
What you have done is almost correct. Just change .currentmember to an actual measure in your cube. Currently when you have the following it is referring to itself i.e. currentmember by the black arrow it referring to the measure count by the black arrow...
This is in AdvWrks:
SELECT
{[Measures].[Internet Sales Amount]} ON 0
,{[Product].[Product Categories].[Category]} ON 1
FROM [Adventure Works];
It returns this:
If I want to replace the empty cell for Components the we can use CASE:
WITH
MEMBER [Measures].[count] AS
CASE [Measures].[Internet Sales Amount]
WHEN 0 THEN "XXX"
ELSE [Measures].[Internet Sales Amount]
END ,format_string = "#,###,##0"
SELECT
{
[Measures].[count]
} ON 0
,{[Product].[Product Categories].[Category]} ON 1
FROM [Adventure Works];
This now returns this:
IIF is used a lot more in MDX than CASE - IIF is nearly always faster. So the above equivalent using IIF is the following:
WITH
MEMBER [Measures].[count] AS
IIF
(
[Measures].[Internet Sales Amount] = 0
,"XXX"
,[Measures].[Internet Sales Amount]
)
,format_string = "#,###,##0"
SELECT
{[Measures].[count]} ON 0
,{[Product].[Product Categories].[Category]} ON 1
FROM [Adventure Works];
I am assuming that your requirement is to find the sum over all the members of [Dim Table1].[TableID] hierarchy.
If so, then the below should work for you:-
WITH MEMBER Measures.[Count] AS
SUM(
[Dim_table1].[TableId].CHILDREN,
[Measures].[Is Summary] * [Measures].[Is Audited] * [Measures].[Final Audited Flag] //If any is 0, it's 0, else 1
)
SELECT Measures.[Count]
FROM [Dsv]
Let me know if further slicers are needed to be added or if my understanding is incorrect.

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 Query Assistance for Filter with 2 Dimensions

Trying to apply a filter with 2 dimensions values.
SELECT [Measures].[OrdItemCount] ON 0,
{
FILTER
(
[Date].[Date].[Date],
[Date].[Date].[Date].MEMBERVALUE = Format(Now(), "yyyyMMdd"))
)
}.ITEM(0)
ON 1
FROM
[Adventure Works]
I am assuming your requirement is to get the OrdItemCount for today. If so, use the query below:
with member [Measures].now as
now(), format_string = "yyyymmdd"
SELECT [Measures].OrdItemCount ON 0,
{FILTER([Date].[Date].[Date], [Date].[Date].MEMBERVALUE = [Measures].now)}.ITEM(0) on 1
from [Adventure Works]