cubewise-code / tm1py

TM1py is a Python package that wraps the TM1 REST API in a simple to use library.
http://tm1py.readthedocs.io/en/latest/
MIT License
190 stars 109 forks source link

Retrieve Element Attribute/Alias #373

Closed sootytm1 closed 4 years ago

sootytm1 commented 4 years ago

Searched in quite a few places but can't find how to/an example of how to extract an alias/attribute value for an element.

If I know the Dim, Hierarchy and Element name is there an tm1py object/service that can return the element attribute?

I found this by accident is this correct? Where ele_dim is a List of elements

 tm1.elements.get_attribute_of_elements(tempdim,tempdim,"Caption_Default",ele_dim[0])
rclapp commented 4 years ago

You have a lot of options, just depends what you're trying to do (bigger picture)

1) if you're writing code that needs to look up attributes from the same dim multiples times you could execute an MDX query on the attributes cube and store it as a frame to reduce trips to the server.

2) if you want a list of attributes to iterate over you could execute set mdx

3) if you want an attribute in the context of a data query you can add it using the Dimension Properties function in mdx

4) finally if you just want an attribute value check out the element service for methods like get_attribute_of_elements and get_alias_element_attributes

rkvinoth commented 4 years ago

I have given three methods here, but there are other methods available as well depending on your design:

from TM1py.Services import TM1Service
tm1=TM1Service()

# To get the attribute values for all elements in a dimension
print(tm1.elements.get_attribute_of_elements('Month', 'Month', 'NextMonth'))

# Create and execute an MDX
mdx = 'SELECT {[Month].[01]} ON ROWS, {[}ElementAttributes_Month].[NextMonth]} ON COLUMNS FROM [}ElementAttributes_Month]'
print(tm1.cells.execute_mdx_values(mdx))

# Pass the element name and attribute name as a string to the get_value method
print(tm1.cells.get_value(cube_name='}ElementAttributes_Month', element_string="Jan,NextMonth"))

Output:

{'01': 'Feb', '02': 'Mar', '03': 'Apr', '04': 'May', '05': 'Jun', '06': 'Jul', '07': 'Aug', '08': 'Sep', '09': 'Oct', '10': 'Nov', '11': 'Dec', '12': 'Jan'}
['Feb']
Feb
sootytm1 commented 4 years ago

Thanks for the responses - just discovered Option 4) - that is what I was after. Thanks @rclapp @rkvinoth