How to use ITEM and when ITEM(0).ITEM(0) is redundant
In MDX we have the Item function which can be used in a number of ways. It is important to understand how it works and how it can be used to our advantage.
As a start, we can call Item over a set or over a tuple:
{set}.Item(0) or (tuple).Item(0)
If we use Item with a tuple, we must specify as an argument the integer position of the member within the tuple which we want. However, when we works with sets, we can either do the same, or specify a number of strings, which identify specific tuples. Some examples:
Item with a tuple:
(a,b).Item(0) = a
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0,
{
([Product].[Category].&[4], [Date].[Calendar].[Calendar Year].&[2008]).Item(0)
} ON 1
FROM [Adventure Works]
(a,b).Item(1) = b
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0,
{
([Product].[Category].&[4], [Date].[Calendar].[Calendar Year].&[2008]).Item(1)
} ON 1
FROM [Adventure Works]
Item with a set:
{a,b,c}.Item(0) = a
WITH
SET SET1 AS
{ ([Product].[Category].&[4],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[1],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[3],[Date].[Calendar].[Calendar Year].&[2008])
}
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0,
{
SET1.Item(0)
} ON 1
FROM [Adventure Works]
{a,b,c}.Item(“a”) = a
WITH
SET SET1 AS
{ ([Product].[Category].&[4],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[1],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[3],[Date].[Calendar].[Calendar Year].&[2008])
}
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0,
{
SET1.Item("([Product].[Category].&[4],
[Date].[Calendar].[Calendar Year].&[2008])")
} ON 1
FROM [Adventure Works]
{(a1,b1),(a2,b2),(a3,b3)}.Item(“a1″,”b1″) = (a1,b1)
WITH
SET SET1 AS
{ ([Product].[Category].&[4],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[1],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[3],[Date].[Calendar].[Calendar Year].&[2008])
}
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0,
{
SET1.Item("[Product].[Category].&[4]",
"[Date].[Calendar].[Calendar Year].&[2008]")
} ON 1
FROM [Adventure Works]
When we specify a number of strings as arguments, we get the tuple which is defined by these strings/coordinates.
Now, let’s see what happens when we have a set of tuples and we use Item on it with a single argument:
{(a1,b1),(a2,b2),(a3,b3)}.Item(0) = (a1,b1)
WITH
SET SET1 AS
{ ([Product].[Category].&[4],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[1],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[3],[Date].[Calendar].[Calendar Year].&[2008])
}
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0,
{
SET1.Item(0)
} ON 1
FROM [Adventure Works]
We get a tuple back. Therefore, if we use a second Item function over the first one, we will get the member on that position from the tuple:
{(a1,b1),(a2,b2),(a3,b3)}.Item(0).Item(0) = (a1,b1).Item(0) = a1
To illustrate the concept:
WITH
SET SET1 AS
{ ([Product].[Category].&[4],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[1],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[3],[Date].[Calendar].[Calendar Year].&[2008])
}
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0,
{
SET1.Item(0).Item(0)
} ON 1
FROM [Adventure Works]
This gives us the whole amount for Accessories, while:
WITH
SET SET1 AS
{ ([Product].[Category].&[4],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[1],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[3],[Date].[Calendar].[Calendar Year].&[2008])
}
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0,
{
SET1.Item(0).Item(1)
} ON 1
FROM [Adventure Works]
gives us the total amount for 2008.
Even if we do:
WITH
SET SET1 AS
{ ([Product].[Category].&[4],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[1],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[3],[Date].[Calendar].[Calendar Year].&[2008])
}
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0,
{
SET1.Item(0).Item(0).Item(0).Item(0).Item(0).Item(0).Item(0).Item(0)
} ON 1
FROM [Adventure Works]
we still get the amount for accessories. But if we do:
WITH
SET SET1 AS
{ ([Product].[Category].&[4],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[1],[Date].[Calendar].[Calendar Year].&[2008]),
([Product].[Category].&[3],[Date].[Calendar].[Calendar Year].&[2008])
}
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0,
{
SET1.Item(0).Item(1).Item(1).Item(0).Item(0).Item(0).Item(0).Item(0)
} ON 1
FROM [Adventure Works]
we get nothing back. This is because there is no element on the second position/coordinate of the tuple ([Date].[Calendar].[Calendar Year].&[2008]).
No matter how many times we use Item(0), it always returns the first member of the result set, which then is itself a set and so on. Therefore calling Item(0) after another Item(0) is rarely necessary and should be done only if we need it.






