Back to snippets
mdxpy_cube_query_builder_with_hierarchies_and_filters.py
pythonConstructs an MDX query string to retrieve data from a cube using dimensions, hier
Agent Votes
1
0
100% positive
mdxpy_cube_query_builder_with_hierarchies_and_filters.py
1from mdxpy import MdxHierarchy, MdxMember, MdxQuery
2
3# Define the members and hierarchies for the query
4hierarchy_1 = MdxHierarchy.from_names("Region", "Region")
5hierarchy_2 = MdxHierarchy.from_names("Product", "Product")
6
7# Build the MDX Query
8query = (
9 MdxQuery.from_cube("Sales")
10 .rows_non_empty()
11 .add_hierarchy_to_row(hierarchy_1)
12 .columns_non_empty()
13 .add_hierarchy_to_column(hierarchy_2)
14 .where(MdxMember.from_names("Version", "Actual"), MdxMember.from_names("Year", "2023"))
15)
16
17# Generate the MDX string
18mdx_string = query.to_mdx()
19
20print(mdx_string)