skipcheck

TM1 client · Docs · Reading and writing data

CellsetService

from skipcheck_tm1.services import CellsetService

Used in the walkthroughs Read and write cells, Run a process and read its result.

Cellsets: the server-side results of an MDX query or a view, with their axes and cells.

A cellset lives on the server until it's deleted, or the session ends; one that isn't deleted holds the server's memory. The 1:1 methods (execute, cells, partition, delete) leave that to the caller. stream does it all: runs the query, pages the cells in order, and deletes the cellset when the result completes, fails or is cancelled.

Read the cells of an MDX query, placing each by its ordinal on the axes:

mdx = """
    SELECT {[Month].[Month].[Jan], [Month].[Month].[Feb], [Month].[Month].[Mar]} ON 0,
      {TM1FILTERBYLEVEL({TM1SUBSETALL([Region].[Region])}, 0)} ON 1
    FROM [Sales]
    WHERE ([Version].[Version].[Actual], [Measure].[Measure].[Revenue])"""

def lines(cellset, cells):
    # Axis 0 is the columns, axis 1 the rows; ordinals run along a row
    columns, rows = cellset.axes[0].tuples, cellset.axes[1].tuples
    out = []
    # The cells arrive a page at a time as the loop asks for them
    for cell in cells:
        row, column = rows[cell.ordinal // len(columns)], columns[cell.ordinal % len(columns)]
        out.append(f"{row.members[0].name}, {column.members[0].name}: {cell.value}")
    return out

# The cellset is deleted when the function returns, and what it returned comes back
return tm1.cellsets.stream(mdx, ODataQuery.none().select(CellsetCell.ORDINAL, CellsetCell.VALUE), 10_000, lines)

Write into the cells of a query by ordinal, deleting its cellset after:

# Write by ordinal into the cells of a query, then delete its cellset; the cells
# must be leaves, as TM1 doesn't spread to a consolidation
mdx = (
    "SELECT {[Month].[Month].[Jan], [Month].[Month].[Feb]} ON 0, {[Region].[Region].[North]} ON 1"
    " FROM [Sales] WHERE ([Version].[Version].[Budget], [Measure].[Measure].[Revenue])"
)
cellset = tm1.cellsets.execute(mdx)
try:
    january = CellsetUpdatePatch.builder().ordinal(0).value(1200).build()
    february = CellsetUpdatePatch.builder().ordinal(1).value(1350).build()
    tm1.cellsets.update_cells(cellset.id, [january, february])
finally:
    tm1.cellsets.delete(cellset.id)

Read the values of a saved view:

# The values of a saved view, read in the same round trip as its cellset
values = ODataQuery.none().expand(Cellset.CELLS, lambda c: c.select(CellsetCell.VALUE))
cellset = tm1.cellsets.execute_view("Sales", ViewScope.PUBLIC, "Revenue by Region", values)
try:
    return [cell.value for cell in cellset.cells]
finally:
    tm1.cellsets.delete(cellset.id)

The asyncio flavor, skipcheck_tm1.aio, has the same methods to await, on AsyncCellsetService.

Members

executeRuns an MDX query, leaving its cellset on the server until delete.
execute_statementRuns an MDX statement for its effect, keeping no cellset.
execute_viewtm1.Execute on a native or MDX view.
listGET Cellsets.
getThe cellset, or a Tm1NotFoundError if it's gone.
deleteFrees the cellset's memory on the server.
axesColumns (0), rows (1), and so on, then the slicer.
hierarchiesThe hierarchies on the axis.
tuplesGET Cellsets('id')/Axes(n)/Tuples, in order.
tuple_membersOne member per hierarchy on the axis.
cellsGET Cellsets('id')/Cells, in ordinal order.
cell_membersThe members that place a cell, one per hierarchy of the cube.
cubeThe cube the cellset is from.
partitionA rectangle of cells.
updateWrites one value to a cell of the cellset, by ordinal; None clears it.
update_cellsWrites many of the cellset's cells, each its own value, by ordinal.
save_view_asSaves the cellset's query as a view of its cube.
streamRuns an MDX query, gives handler the cellset (its ID and expanded Axes) and its cells in ordinal order, then deletes the cellset however the handler finishes.

execute

execute(mdx: str, query: ODataQuery | None = None, **options: Any) -> Cellset

ExecuteMDX: runs an MDX query, leaving its cellset on the server until delete. Expand Axes and Cells (with $top and $select) to get them in the same round trip. The cellset has its ID.

TM1 REST ActionImport ExecuteMDX

execute_statement

execute_statement(mdx: str) -> None

ExecuteMDXStatement: runs an MDX statement for its effect, keeping no cellset. Needs schema 11.8.20. TM1 (11.8) runs a SELECT and returns nothing, and rejects CREATE SET as a syntax error (code 501).

TM1 REST ActionImport ExecuteMDXStatement

execute_view

execute_view(cube: str, scope: ViewScope, view: str, query: ODataQuery | None = None, **options: Any) -> Cellset

tm1.Execute on a native or MDX view: its cellset, with its ID, left on the server until delete.

TM1 REST Action NativeView/Execute Action MDXView/Execute

list

list(query: ODataQuery | None = None, **options: Any) -> list[Cellset]

GET Cellsets. TM1 (11.8) lists none, though each cellset reads by its ID with get: keep the IDs of the cellsets you create.

TM1 REST EntitySet Cellsets

get

get(id: str, query: ODataQuery | None = None, **options: Any) -> Cellset

GET Cellsets('id'): the cellset, or a Tm1NotFoundError if it's gone.

TM1 REST EntitySet Cellsets

delete

delete(id: str) -> None

DELETE Cellsets('id'): frees the cellset's memory on the server.

TM1 REST EntitySet Cellsets

axes

axes(id: str, query: ODataQuery | None = None, **options: Any) -> list[CellsetAxis]

GET Cellsets('id')/Axes: columns (0), rows (1), and so on, then the slicer. TM1 (11.8) adds the slicer even without a WHERE: it holds the cube's other dimensions, such as its virtual Sandboxes, with cardinality 1. Expand, for example, Tuples($expand=Members).

TM1 REST Navigation Cellset/Axes

hierarchies

hierarchies(id: str, axis: int, query: ODataQuery | None = None, **options: Any) -> list[Hierarchy]

GET Cellsets('id')/Axes(n)/Hierarchies: the hierarchies on the axis.

TM1 REST Navigation CellsetAxis/Hierarchies

tuples

tuples(id: str, axis: int, query: ODataQuery | None = None, **options: Any) -> list[CellsetAxisTuple]

GET Cellsets('id')/Axes(n)/Tuples, in order: an axis can be large, so it's read page by page. Expand, for example, Members($select=Name).

TM1 REST Navigation CellsetAxis/Tuples

tuple_members

tuple_members(id: str, axis: int, tuple: int, query: ODataQuery | None = None, **options: Any) -> list[Member]

GET Cellsets('id')/Axes(n)/Tuples(t)/Members: one member per hierarchy on the axis.

TM1 REST Navigation CellsetAxisTuple/Members

cells

cells(id: str, query: ODataQuery | None = None, **options: Any) -> list[CellsetCell]

GET Cellsets('id')/Cells, in ordinal order. Use $top and $skip, or partition, for pages; select Ordinal, Value, …

TM1 REST Navigation Cellset/Cells

cell_members

cell_members(id: str, ordinal: int, query: ODataQuery | None = None, **options: Any) -> list[Member]

GET Cellsets('id')/Cells(n)/Members: the members that place a cell, one per hierarchy of the cube. TM1 (11.8) includes the virtual Sandboxes dimension's, Base, first.

TM1 REST Navigation CellsetCell/Members

cube

cube(id: str, query: ODataQuery | None = None, **options: Any) -> Cube

GET Cellsets('id')/Cube: the cube the cellset is from.

TM1 REST Navigation Cellset/Cube

partition

partition(id: str, begin: int, end: int, query: ODataQuery | None = None, **options: Any) -> list[CellsetCell]

tm1.GetPartition(Begin=b,End=e): a rectangle of cells. Needs schema 10.2.2.3.

TM1 (11.8) reads Begin and End as the top-left and bottom-right cells of a rectangle on the first two axes, not as a range of ordinals: with two columns, partition(id, 2, 4) is column 0 of rows 1 and 2, cells 2 and 4, not 3. A run of whole rows, from a row's first cell to a row's last, is a range. Across pages, keep the query the same.

TM1 REST Function Cellset/GetPartition

update

update(id: str, ordinal: int, value: Any) -> None

tm1.Update: writes one value to a cell of the cellset, by ordinal; None clears it. The action takes a range (BeginOrdinal, EndOrdinal), but TM1 (11.8) writes only the first cell of it, so this takes one ordinal; update_cells writes many.

TM1 REST Action Cellset/Update

update_cells

update_cells(id: str, updates: Sequence[Any]) -> None

tm1.UpdateCells: writes many of the cellset's cells, each its own value, by ordinal: CellsetUpdatePatch.builder().ordinal(n).value(v), or with a reference cube and cell to copy from. Needs schema 11.0.0.

TM1 REST Action Cellset/UpdateCells Navigation CellsetUpdate/ReferenceCube Navigation CellsetUpdate/ReferenceCell

save_view_as

save_view_as(id: str, name: str, overwrite: bool | None = None, make_private: bool | None = None) -> View

tm1.SaveViewAs: saves the cellset's query as a view of its cube. Needs schema 11.0.0. With only a name, a public view, failing if one has the name; with overwrite and make_private, whether to replace one that has it, and whether the view is private.

TM1 REST Action Cellset/SaveViewAs

stream

stream(mdx: str, cell_query: ODataQuery | None, page_size: int, handler: Callable[[Cellset, Iterator[CellsetCell]], Awaitable[Any]]) -> Any

Runs an MDX query, gives handler the cellset (its ID and expanded Axes) and its cells in ordinal order, then deletes the cellset however the handler finishes. cell_query is what to select of each cell; include Ordinal to place them. The handler's result is this method's.

One round trip brings the axes (hierarchies by name, tuples with their members' names and unique names) and the first page_size cells; the rest come in pages of GetPartition as the handler reads them, in ordinal order, so memory stays bounded on both sides however large the cellset. (The Java client fetches two pages ahead; this one fetches each page when it's needed.)

mdx = """
    SELECT {[Month].[Month].[Jan], [Month].[Month].[Feb], [Month].[Month].[Mar]} ON 0,
      {TM1FILTERBYLEVEL({TM1SUBSETALL([Region].[Region])}, 0)} ON 1
    FROM [Sales]
    WHERE ([Version].[Version].[Actual], [Measure].[Measure].[Revenue])"""

def lines(cellset, cells):
    # Axis 0 is the columns, axis 1 the rows; ordinals run along a row
    columns, rows = cellset.axes[0].tuples, cellset.axes[1].tuples
    out = []
    # The cells arrive a page at a time as the loop asks for them
    for cell in cells:
        row, column = rows[cell.ordinal // len(columns)], columns[cell.ordinal % len(columns)]
        out.append(f"{row.members[0].name}, {column.members[0].name}: {cell.value}")
    return out

# The cellset is deleted when the function returns, and what it returned comes back
return tm1.cellsets.stream(mdx, ODataQuery.none().select(CellsetCell.ORDINAL, CellsetCell.VALUE), 10_000, lines)

TM1 REST ActionImport ExecuteMDX Function Cellset/GetPartition EntitySet Cellsets