Describe what did you try to do with TM1py
I am using the ElementService's _get_elements_filtered_by_attribute_ function to filter out list of elements that match certain attribute value. Below is the code I am working with.
#attrName = 'Description' # This works
attrName= 'Code - Description' # This does NOT work
attrValue = 'Region'
elemList = tm1.dimensions.hierarchies.elements.get_elements_filtered_by_attribute (dimName, dimName, attrName, attrValue)
for elem in elemList:
print (elem)
Describe what's not working the way you expect
Didn't get the expected result? Describe:
When the attribute name with special characters is passed to _get_elements_filtered_by_attribute_ function, it fails with error listed in Additional Context. I realize there are other ways to filter the element list to match attribute value, possibly by querying the attribute cube. I am not sure if this is a known thing ... I wanted to raise the observation with _get_elements_filtered_by_attribute_ just in case. Thanks!
Version
Additional context
If you encounter an error, please add the error message and the stack trace
Traceback (most recent call last):
File "PATH/test.py", line 16, in
elemlist = tm1.dimensions.hierarchies.elements.get_elements_filtered_by_attribute (dimName, dimName, attrName, attrValue)
File "VENV_PATH/lib/python3.9/site-packages/TM1py/Services/ElementService.py", line 364, in get_elements_filtered_by_attribute
response = self._rest.GET(url, **kwargs)
File "VENV_PATH/lib/python3.9/site-packages/TM1py/Services/RestService.py", line 77, in wrapper
self.verify_response(response=response)
File "VENV_PATH/lib/python3.9/site-packages/TM1py/Services/RestService.py", line 436, in verify_response
raise TM1pyRestException(response.text,
TM1py.Exceptions.Exceptions.TM1pyRestException: Text: '{"error":{"code":"278","message":"Syntax error at position 15, near \"-\" in $filter: Expecting end of input."}}' - Status Code: 400 - Reason: 'Bad Request' - Headers: {'Date': 'Mon, 19 Apr 2021 14:53:31 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '120', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Cache-Control': 'no-cache', 'OData-Version': '4.0'}
Hi @SachinAnalytics,
thanks for reporting this issue.
Unfortunately, this is a limitation of the TM1 REST API, that TM1py can't workaround.
You can read more about it here:
Attribute Names
TM1 may be case and space insensitive but the OData standards and TM1’s Restful API are not. The TM1 Restful API observes the OData specification which has a limitation of only accepting alpha-numeric Ascii characters and underscore. All special characters and spaces should be avoided.
Therefore, to avoid issues querying attribute values via the Rest API we need to be even more restrictive in naming attributes than in naming elements (even though in fact attribute names are just elements). Attribute names should contain only characters:
A – Z (characters 65 – 90)
a – z (characters 97 – 122)
0 – 9 (characters 48 – 57)
_ (underscore, character 95)
I suggest you use MDX to filter the elements by an attribute. To build the MDX query, you can use mdxpy.
from mdxpy import MdxHierarchySet
from TM1py import TM1Service
with TM1Service(address="", port=12354, user="admin", password="apple", ssl=True) as tm1:
query = MdxHierarchySet.tm1_subset_all(dimension="d1", hierarchy="d1")
query = query.filter_by_attribute(attribute_name="text - text", attribute_values=["A"])
elements = tm1.elements.execute_set_mdx(
mdx=query.to_mdx(),
element_properties=None,
member_properties=['Name'],
parent_properties=None)
print(elements)
Thank you for the prompt response @MariusWirtz ... I will explore the work around you proposed - seems more efficient. Thank you!
Hi Maruis, I've created a PR for this as discussed. Any feedback is appreciated.