MDX-How can I get attribute key with MDX
User Rating: / 18
PoorBest 
Written by Ramunas Balukonis   
Monday, 11 June 2007 06:38

Q: How can I get attribute key with MDX

A:

To do so, use Member_Key function:

WITH
MEMBER Measures.ProductKey as [Product].[Product Categories].Currentmember.Member_Key
SELECT {Measures.ProductKey} ON axis(0),
[Product].[Product Categories].Members on axis(1)
FROM [Adventure Works]

Added May 19, 2009 by Vidas Matelis.

You can also get the same results by using Properties("Key") function:

WITH
MEMBER Measures.ProductKey as [Product].[Product Categories].Currentmember.Properties("Key")
SELECT {Measures.ProductKey} ON axis(0),
[Product].[Product Categories].Members on axis(1)
FROM [Adventure Works]

For coposite keys Member_Key and Properties("Key") will return NULL values. You should use Properties("Key0"), Properties("Key1"), etc to get composit keys:

WITH
MEMBER Measures.StateProvinceKey0 AS [Customer].[State-Province].CurrentMember.Properties("KEY0")
MEMBER Measures.StateProvinceKey1 AS [Customer].[State-Province].CurrentMember.Properties("KEY1")

SELECT {Measures.StateProvinceKey0, Measures.StateProvinceKey1} ON axis(0),
[Customer].[State-Province].Members on axis(1)
FROM [Adventure Works]

Here are results for the last query:

State-ProvinceStateProvinceKey0StateProvinceKey1
All Customers0#Error
AlabamaALUS
AlbertaABCA
ArizonaAZUS
BayernBYDE
BrandenburgBBDE
British ColumbiaBCCA
BrunswickNBCA
CaliforniaCAUS
Charente-Maritime17FR
ColoradoCOUS

Note: There is performance penalty when you convert dimension key to a measure.

 

 
Comments (3)
3 Wednesday, 21 September 2011 15:43
AaronLS
The problem with this approach is the Measures hierarchy can't appear on different axes. So you might want to put a dimension property on rows and a measure on columns, but you will be unable to do that.
2 Wednesday, 20 May 2009 02:19
Vidas M.
Bhudev,

I added some explanation about composite keys to this FAQ entry.
1 Monday, 18 May 2009 06:15
Bhudev
If Key is Custom Key I mean based on multiple columns, I'm in doubt that it will work. I faced this issue in SSAS 2005.

Tags: faq, mdx
 

Data Warehouse Explorer