Skip to content

Commit

Permalink
Add include_hierarchies arg to execute_mdx_raw
Browse files Browse the repository at this point in the history
Fixes #467
  • Loading branch information
MariusWirtz committed Jan 22, 2021
1 parent 223769d commit 9ce9a6d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion TM1py/Services/CellService.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ def execute_mdx_raw(
skip_consolidated_cells: bool = False,
skip_rule_derived_cells: bool = False,
sandbox_name: str = None,
include_hierarchies: bool = False,
**kwargs) -> Dict:
""" Execute MDX and return the raw data from TM1
Expand All @@ -783,6 +784,7 @@ def execute_mdx_raw(
:param skip_consolidated_cells: skip consolidated cells in cellset
:param skip_rule_derived_cells: skip rule derived cells in cellset
:param sandbox_name: str
:param include_hierarchies: retrieve Hierarchies property on Axes
:return: Raw format from TM1.
"""
cellset_id = self.create_cellset(mdx=mdx, sandbox_name=sandbox_name, **kwargs)
Expand All @@ -799,6 +801,7 @@ def execute_mdx_raw(
skip_consolidated_cells=skip_consolidated_cells,
skip_rule_derived_cells=skip_rule_derived_cells,
sandbox_name=sandbox_name,
include_hierarchies=include_hierarchies,
**kwargs)

def execute_view_raw(
Expand Down Expand Up @@ -1445,6 +1448,7 @@ def extract_cellset_raw(
skip_consolidated_cells: bool = False,
skip_rule_derived_cells: bool = False,
sandbox_name: str = None,
include_hierarchies: bool = False,
**kwargs) -> Dict:
""" Extract full cellset data and return the raw data from TM1
Expand All @@ -1459,6 +1463,7 @@ def extract_cellset_raw(
:param skip_consolidated_cells: skip consolidated cells in cellset
:param skip_rule_derived_cells: skip rule derived cells in cellset
:param sandbox_name: str
:param include_hierarchies: retrieve Hierarchies property on Axes
:return: Raw format from TM1.
"""
if not cell_properties:
Expand Down Expand Up @@ -1501,15 +1506,21 @@ def extract_cellset_raw(

filter_cells = " and ".join(filters)

if include_hierarchies:
expand_hierarchies = "Hierarchies($select=Name;$expand=Dimension($select=Name)),"
else:
expand_hierarchies = ""

url = "/api/v1/Cellsets('{cellset_id}')?$expand=" \
"Cube($select=Name;$expand=Dimensions($select=Name))," \
"Axes({filter_axis}$expand=Tuples($expand=Members({select_member_properties}" \
"Axes({filter_axis}$expand={hierarchies}Tuples($expand=Members({select_member_properties}" \
"{expand_elem_properties}{top_rows})))," \
"Cells($select={cell_properties}{top_cells}{skip_cells}{filter_cells})" \
.format(cellset_id=cellset_id,
top_rows=f";$top={top}" if top and not skip else "",
cell_properties=",".join(cell_properties),
filter_axis=filter_axis,
hierarchies=expand_hierarchies,
select_member_properties=select_member_properties,
expand_elem_properties=expand_elem_properties,
top_cells=f";$top={top}" if top else "",
Expand Down
14 changes: 14 additions & 0 deletions Tests/CellService.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,20 @@ def test_execute_mdx_raw_skip_contexts(self):
axis["Tuples"][0]["Members"][0]["UniqueName"])
self.assertNotEqual(dimension_on_axis, self.dimension_names[2])

def test_execute_mdx_raw_include_hierarchies(self):
mdx = MdxBuilder.from_cube(self.cube_name) \
.add_hierarchy_set_to_column_axis(MdxHierarchySet.member(Member.of(self.dimension_names[0], "Element1"))) \
.add_hierarchy_set_to_row_axis(MdxHierarchySet.member(Member.of(self.dimension_names[1], "Element1"))) \
.add_member_to_where("[" + self.dimension_names[2] + "].[Element1]").to_mdx()

raw_response = self.tm1.cubes.cells.execute_mdx_raw(mdx, include_hierarchies=True)

for axis_counter, axis in enumerate(raw_response['Axes']):
hierarchies = axis['Hierarchies']

self.assertEqual(self.dimension_names[axis_counter], hierarchies[0]['Name'])
self.assertEqual(self.dimension_names[axis_counter], hierarchies[0]['Dimension']['Name'])

def test_execute_mdx_rows_and_values_one_cell(self):
mdx = MdxBuilder.from_cube(self.cube_name) \
.add_hierarchy_set_to_axis(1, MdxHierarchySet.member(Member.of(self.dimension_names[0], "Element1"))) \
Expand Down

0 comments on commit 9ce9a6d

Please sign in to comment.