iccube : how to count number of members with where clause in a mdx? - mdx

Using the following mdx, the result is the same if I run it with or without the where clause : It returns the total nbr of years in the hierarchy.
with member nbYears as [Time].[Year].[Year].count
select nbYears on 0
from [Sales]
where [Time].[2007]

You can use the Existing MDX function
WITH
member nbYears as (Existing [Time].[Year].[Year]).count
SELECT nbYears on 0
FROM [Sales]
WHERE [Time].[2007]

Related

Getting results for even years only

MDX has a nice feature whereby I can specify a range of members:
SELECT ([Canada],[2006]:[2011]) on Rows,
[Measures].members on Columns
FROM [Sales]
Is there a way to calculate the set of even years: {[2006], [2008], [2010]}? I am looking for a way that would work for large sets, so that listing the years manually is not an option.
You can filter you function using a filter function, a declared function and MOD function (MOD returns the remainder from the division - like % in Java ) :
WITH
FUNCTION isEven(Value _number) as Mod( Int(_number) , 2 ) = 0
SELECT
FILTER( [Date].[Date].[Year] as t, isEven( Year( t.current.key) ) ) on 0
FROM [Cube]
If you are using this filter often you could create a FilterEven declared function once in the script (same for isEven() )
Try this. I used adventure works for the query.For the mod logic i took help from
Mod Logic
WITH
MEMBER [Measures].[Data Type] AS
[Date].[Day of Year].CurrentMember.Properties ("Member_Value",TYPED)
MEMBER [Measures].[IsEven] as
[Measures].[Data Type]-Int([Measures].[Data Type]/2)*2
select {[Measures].[Internet Order Count] }
on columns,
filter (
[Date].[Day of Year].[Day of Year],
[Measures].[IsEven]=0)
on rows
from [Adventure Works]
Plus you can have a column in the date dimension have 1,0 to indicate if the year is even or odd. Then simply use that column in the MDX query , no need to do all the above manipulations

MDX Error while creating Calculated Measure

I am trying to create a calculated measure that finds the difference between two measures by using the following mdx query
WITH MEMBER [Measures].[Available]
AS ([Measures].[Capacity days] ,[Project].[Projects by Name].[All],[Project].[Projects by Code].[All])
- ([Measures].[Worked Days] ,EXCEPT([Project].[Projects by Name].[Project].MEMBERS,
[Project].[Projects by Name].[Project].&[1214]),[Version].[Version].[Combined],[Charge].[Charge].[All])
In case of second measure Worked Days I want to access it with respect to all projects except one ,so am using EXCEPT function which results in the following error
" The function expects a string or numeric expression for the argument. A tuple set expression was used"
Is there any other way to perform this operation?
The query is mixing tuples with sets. Perhaps you can check this gentle introduction of MDX for main concepts and notations.
The second tuple is using a set (the result of EXCEPT) as its second member which is not possible. You could use the aggregate function as following to compute the [Worked Days] over the members of this set instead :
AS ( [Measures].[Capacity days], ... )
- Aggregate(
Except (
[Project].[Projects by Name].[Project].MEMBERS,
[Project].[Projects by Name].[Project].&[1214]
),
( [Measures].[Worked Days], ... )
)

MDX filter member in where clause vs filter member in From clause

IN ADVENTURE WORKS what is difefrence between this two situations:
1) filter member in From Clause:
with
member [existing_categries_Member_Count]as
count(existing[Product].[Category].[Category].members)
member [existing_subcategries_Member_Count]as
count(existing[Product].[subCategory].[subCategory].members)
select
{
[existing_subcategries_Member_Count],
[existing_categries_Member_Count]
} on 0
FROM (
SELECT [Product].[Subcategory].&[31] ON 0
FROM [Adventure Works])
Results:
existing_subcategries_Member_Count existing_categries_Member_Count
37 4
AND
2) Filter Member In WHERE Clause:
with
member [existing_categries_Member_Count]as
count(existing[Product].[Category].[Category].members)
member [existing_subcategries_Member_Count]as
count(existing[Product].[subCategory].[subCategory].members)
select
{
[existing_subcategries_Member_Count],
[existing_categries_Member_Count]
} on 0
FROM [Adventure Works]
WHERE [Product].[Subcategory].&[31]
Results :
existing_subcategries_Member_Count existing_categries_Member_Count
1 1
I expect the same results for the two queries but I see diffrent results.
Please tell me the difference between WHERE AND SUbcube
thanks
When you put a member in a WHERE clause, you override the default member (the context is changed).
After the subselect is evaluated the current member on [Product].[Subcategory] is still [Product].[Subcategory].[All Products]
As you used the existing keyword in your calculated members, you can not have the same results. If you remove it, your 2 queries will give the same result.

Why AVG function perform the SUM?

I would to execute a query with a calculated member which returns the AVG (of the measure) of the Coil belonging to a particular LINESPEED.
The query is:
With
Member [Measures].[Avg1] As
AVG(
([LINESPEED].currentmember,
[GRUPPO].[Coil].currentmember)
,
[Measures].[KPI1]
)
SELECT [Measures].[Avg1] on 0,
non empty {[LINESPEED].children} On 1
from[HDGL]
But the AVG function compute exactly the sum of the KPIs of the coil related to a particular LINESPEED!!
Why?
Your formula is using a single tuple, so AVG() is equivalent to SUM() :
AVG( ([LINESPEED].currentmember, [GRUPPO].[Coil].currentmember), ...)

Can I reformulate this MDX query to use sets instead of an "And"?

with member [Measures].[BoughtDispenser] as
Sum(Descendants([Customer].[Customer].CurrentMember, [Customer].[Customer]),
Iif(
(IsEmpty(([Item].[ItemNumber].&[011074], [Measures].[Sale Amount]))
And IsEmpty(([Item].[ItemNumber].&[011069], [Measures].[Sale Amount]))
)
Or IsEmpty([Measures].[Sale Amount]),
0 , 1
)
)
select
{[Measures].[Sale Amount]} on columns,
non empty filter([Customer].[Customer].children, [Measures].[BoughtDispenser])
* {[Item].[ItemNumber].members}
on rows
from [Sales]
where [EnteredDate].[Quarter].&[2010-01-01T00:00:00]
;
The object is to show all the items purchased by customers who also bought either of the two dispensers (011069 and 011074).
I based the calculated member on a query I found to do basket analysis. I feel like there should be a way to write it with the set {[Item].[ItemNumber].&[011074], [Item].[ItemNumber].&[011069]} instead of the two IsEmpty tests. Everything I've tried ended up having every Customer in the result.
My environment is SQL Server Analysis Services 2005.
Yes I can! It just required a slightly different approach to the calculated member:
with member [Measures].[BoughtDispenser] as
Sum(Descendants([Customer].[Customer].CurrentMember, [Customer].[Customer])
* {[Item].[ItemNumber].&[011069], [Item].[ItemNumber].&[011074]},
[Measures].[Quantity Shipped]
)
select
{[Measures].[Sale Amount]} on columns,
non empty filter([Customer].[Customer].children, [Measures].[BoughtDispenser])
* {[Item].[ItemNumber].members}
on rows
from [Sales]
where [EnteredDate].[Quarter].&[2010-01-01T00:00:00]
;