Skip to content

PyTable

Bases: PyObject

The main PyTable class to interact with the tables in model.

Attributes:

Name Type Description
Name str

Name of table.

IsHidden bool

Is the table hidden.

Description str

The description of the table.

Model Tabular

The parent Tabular() class.

Partitions PyPartitions

The PyPartitions() in the table.

Columns PyColumns

The PyColumns() in the table.

Measures PyMeasures

The PyMeasures() in the table.

Example
Passing through PyTable to PyPartition
model.Tables[0].Partitions['Last Year'].refresh() # (1)
  1. This shows the ability to travel through your model to a specific partition and then running a refresh for that specific partition. model -> PyTables -> PyTable (1st index) -> PyPartitions -> PyPartition (.Name == 'Last Year') -> .refresh()
Source code in pytabular/table.py
class PyTable(PyObject):
    """The main PyTable class to interact with the tables in model.

    Attributes:
        Name (str): Name of table.
        IsHidden (bool): Is the table hidden.
        Description (str): The description of the table.
        Model (Tabular): The parent `Tabular()` class.
        Partitions (PyPartitions): The `PyPartitions()` in the table.
        Columns (PyColumns): The `PyColumns()` in the table.
        Measures (PyMeasures): The `PyMeasures()` in the table.

    Example:
        ```python title="Passing through PyTable to PyPartition"

        model.Tables[0].Partitions['Last Year'].refresh() # (1)
        ```

        1. This shows the ability to travel through your model
        to a specific partition and then running a refresh
        for that specific partition.
        `model` -> `PyTables` -> `PyTable` (1st index) -> `PyPartitions`
        -> `PyPartition` (.Name == 'Last Year') -> `.refresh()`
    """

    def __init__(self, object, model) -> None:
        """Init extends from `PyObject` class.

        Also adds a few specific rows to the `rich`
        table.

        Args:
            object (Table): The actual .Net table.
            model (Tabular): The model that the table is in.
        """
        super().__init__(object)
        self.Model = model
        self.Partitions: PyPartitions = PyPartitions(
            [
                PyPartition(partition, self)
                for partition in self._object.Partitions.GetEnumerator()
            ]
        )
        self.Columns: PyColumns = PyColumns(
            [PyColumn(column, self) for column in self._object.Columns.GetEnumerator()]
        )
        self.Measures: PyMeasures = PyMeasures(
            [
                PyMeasure(measure, self)
                for measure in self._object.Measures.GetEnumerator()
            ],
            self,
        )
        self._display.add_row("# of Partitions", str(len(self.Partitions)))
        self._display.add_row("# of Columns", str(len(self.Columns)))
        self._display.add_row(
            "# of Measures", str(len(self.Measures)), end_section=True
        )
        self._display.add_row("Description", self._object.Description, end_section=True)
        self._display.add_row("DataCategory", str(self._object.DataCategory))
        self._display.add_row("IsHidden", str(self._object.IsHidden))
        self._display.add_row("IsPrivate", str(self._object.IsPrivate))
        self._display.add_row(
            "ModifiedTime",
            ticks_to_datetime(self._object.ModifiedTime.Ticks).strftime(
                "%m/%d/%Y, %H:%M:%S"
            ),
        )

    def row_count(self) -> int:
        """Method to return count of rows.

        Simple Dax Query: `EVALUATE {COUNTROWS('Table Name')}`.

        Returns:
            int: Number of rows using `COUNTROWS`.

        Example:
            ```python
            model.Tables['Table Name'].row_count()
            ```
        """
        return self.Model.Adomd.query(f"EVALUATE {{COUNTROWS('{self.Name}')}}")

    def refresh(self, *args, **kwargs) -> pd.DataFrame:
        """Use this to refresh the PyTable.

        Returns:
            pd.DataFrame: Returns pandas dataframe with some refresh details.

        Example:
            ```python
            model.Tables['Table Name'].refresh()

            model.Tables['Table Name'].refresh(trace = None) # (1)
            ```

            1. You can pass through arguments to `PyRefresh`, like removing trace.
        """
        return self.Model.refresh(self, *args, **kwargs)

    def last_refresh(self) -> datetime:
        """Will query each partition for the last refresh time.

        Then will select the max value to return.

        Returns:
            datetime: Last refresh time in datetime format
        """
        partition_refreshes = [
            partition.last_refresh() for partition in self.Partitions
        ]
        return max(partition_refreshes)

    def related(self):
        """Returns tables with a relationship with the table in question."""
        return self.Model.Relationships.related(self)

__init__(object, model)

Init extends from PyObject class.

Also adds a few specific rows to the rich table.

Parameters:

Name Type Description Default
object Table

The actual .Net table.

required
model Tabular

The model that the table is in.

required
Source code in pytabular/table.py
def __init__(self, object, model) -> None:
    """Init extends from `PyObject` class.

    Also adds a few specific rows to the `rich`
    table.

    Args:
        object (Table): The actual .Net table.
        model (Tabular): The model that the table is in.
    """
    super().__init__(object)
    self.Model = model
    self.Partitions: PyPartitions = PyPartitions(
        [
            PyPartition(partition, self)
            for partition in self._object.Partitions.GetEnumerator()
        ]
    )
    self.Columns: PyColumns = PyColumns(
        [PyColumn(column, self) for column in self._object.Columns.GetEnumerator()]
    )
    self.Measures: PyMeasures = PyMeasures(
        [
            PyMeasure(measure, self)
            for measure in self._object.Measures.GetEnumerator()
        ],
        self,
    )
    self._display.add_row("# of Partitions", str(len(self.Partitions)))
    self._display.add_row("# of Columns", str(len(self.Columns)))
    self._display.add_row(
        "# of Measures", str(len(self.Measures)), end_section=True
    )
    self._display.add_row("Description", self._object.Description, end_section=True)
    self._display.add_row("DataCategory", str(self._object.DataCategory))
    self._display.add_row("IsHidden", str(self._object.IsHidden))
    self._display.add_row("IsPrivate", str(self._object.IsPrivate))
    self._display.add_row(
        "ModifiedTime",
        ticks_to_datetime(self._object.ModifiedTime.Ticks).strftime(
            "%m/%d/%Y, %H:%M:%S"
        ),
    )

row_count()

Method to return count of rows.

Simple Dax Query: EVALUATE {COUNTROWS('Table Name')}.

Returns:

Name Type Description
int int

Number of rows using COUNTROWS.

Example
model.Tables['Table Name'].row_count()
Source code in pytabular/table.py
def row_count(self) -> int:
    """Method to return count of rows.

    Simple Dax Query: `EVALUATE {COUNTROWS('Table Name')}`.

    Returns:
        int: Number of rows using `COUNTROWS`.

    Example:
        ```python
        model.Tables['Table Name'].row_count()
        ```
    """
    return self.Model.Adomd.query(f"EVALUATE {{COUNTROWS('{self.Name}')}}")

refresh(*args, **kwargs)

Use this to refresh the PyTable.

Returns:

Type Description
DataFrame

pd.DataFrame: Returns pandas dataframe with some refresh details.

Example
model.Tables['Table Name'].refresh()

model.Tables['Table Name'].refresh(trace = None) # (1)
  1. You can pass through arguments to PyRefresh, like removing trace.
Source code in pytabular/table.py
def refresh(self, *args, **kwargs) -> pd.DataFrame:
    """Use this to refresh the PyTable.

    Returns:
        pd.DataFrame: Returns pandas dataframe with some refresh details.

    Example:
        ```python
        model.Tables['Table Name'].refresh()

        model.Tables['Table Name'].refresh(trace = None) # (1)
        ```

        1. You can pass through arguments to `PyRefresh`, like removing trace.
    """
    return self.Model.refresh(self, *args, **kwargs)

last_refresh()

Will query each partition for the last refresh time.

Then will select the max value to return.

Returns:

Name Type Description
datetime datetime

Last refresh time in datetime format

Source code in pytabular/table.py
def last_refresh(self) -> datetime:
    """Will query each partition for the last refresh time.

    Then will select the max value to return.

    Returns:
        datetime: Last refresh time in datetime format
    """
    partition_refreshes = [
        partition.last_refresh() for partition in self.Partitions
    ]
    return max(partition_refreshes)

related()

Returns tables with a relationship with the table in question.

Source code in pytabular/table.py
def related(self):
    """Returns tables with a relationship with the table in question."""
    return self.Model.Relationships.related(self)