Bases: PyObject
The main class to work with your columns.
Notice the PyObject
magic method __getattr__()
will search in self._object
if it is unable to find it in the default attributes.
This let's you also easily check the default .Net properties.
See methods for extra functionality.
Source code in pytabular/column.py
| class PyColumn(PyObject):
"""The main class to work with your columns.
Notice the `PyObject` magic method `__getattr__()` will search in `self._object`
if it is unable to find it in the default attributes.
This let's you also easily check the default .Net properties.
See methods for extra functionality.
"""
def __init__(self, object, table) -> None:
"""Init that connects your column to parent table.
It will also build custom rows for your `rich`
display table.
Args:
object (Column): .Net column object.
table (Table): .Net table object.
"""
super().__init__(object)
self.Table = table
self._display.add_row(
"Description", str(self._object.Description), end_section=True
)
self._display.add_row("DataType", str(self._object.DataType))
self._display.add_row("EncodingHint", str(self._object.EncodingHint))
self._display.add_row("IsAvailableInMDX", str(self._object.IsAvailableInMDX))
self._display.add_row("IsHidden", str(self._object.IsHidden))
self._display.add_row("IsKey", str(self._object.IsKey))
self._display.add_row("IsNullable", str(self._object.IsNullable))
self._display.add_row("State", str(self._object.State))
self._display.add_row("DisplayFolder", str(self._object.DisplayFolder))
def get_dependencies(self) -> pd.DataFrame:
"""Returns the dependant columns of a measure."""
dmv_query = f"select * from $SYSTEM.DISCOVER_CALC_DEPENDENCY where [OBJECT] = \
'{self.Name}' and [TABLE] = '{self.Table.Name}'"
return self.Table.Model.query(dmv_query)
def get_sample_values(self, top_n: int = 3) -> pd.DataFrame:
"""Get sample values of column."""
column_to_sample = f"'{self.Table.Name}'[{self.Name}]"
try:
# adding temporary try except. TOPNSKIP will not work for directquery mode.
# Need an efficient way to identify if query is direct query or not.
dax_query = f"""EVALUATE
TOPNSKIP(
{top_n},
0,
FILTER(
VALUES({column_to_sample}),
NOT ISBLANK({column_to_sample})
&& LEN({column_to_sample}) > 0
),
1
)
ORDER BY {column_to_sample}
"""
return self.Table.Model.query(dax_query)
except Exception:
# This is really tech debt anyways and should be replaced...
dax_query = f"""
EVALUATE
TOPN(
{top_n},
FILTER(
VALUES({column_to_sample}),
NOT ISBLANK({column_to_sample}) && LEN({column_to_sample}) > 0
)
)
"""
return self.Table.Model.query(dax_query)
def distinct_count(self, no_blank=False) -> int:
"""Get the `DISTINCTCOUNT` of a column.
Args:
no_blank (bool, optional): If `True`, will call `DISTINCTCOUNTNOBLANK`.
Defaults to `False`.
Returns:
int: Number of Distinct Count from column.
If `no_blank == True` then will return number of distinct count no blanks.
"""
func = "DISTINCTCOUNT"
if no_blank:
func += "NOBLANK"
return self.Table.Model.Adomd.query(
f"EVALUATE {{{func}('{self.Table.Name}'[{self.Name}])}}"
)
def values(self) -> pd.DataFrame:
"""Get single column DataFrame of values in column.
Similar to `get_sample_values()` but will return **all**.
Returns:
pd.DataFrame: Single column DataFrame of values.
"""
return self.Table.Model.Adomd.query(
f"EVALUATE VALUES('{self.Table.Name}'[{self.Name}])"
)
|
__init__(object, table)
Init that connects your column to parent table.
It will also build custom rows for your rich
display table.
Parameters:
Name |
Type |
Description |
Default |
object |
Column
|
|
required
|
table |
Table
|
|
required
|
Source code in pytabular/column.py
| def __init__(self, object, table) -> None:
"""Init that connects your column to parent table.
It will also build custom rows for your `rich`
display table.
Args:
object (Column): .Net column object.
table (Table): .Net table object.
"""
super().__init__(object)
self.Table = table
self._display.add_row(
"Description", str(self._object.Description), end_section=True
)
self._display.add_row("DataType", str(self._object.DataType))
self._display.add_row("EncodingHint", str(self._object.EncodingHint))
self._display.add_row("IsAvailableInMDX", str(self._object.IsAvailableInMDX))
self._display.add_row("IsHidden", str(self._object.IsHidden))
self._display.add_row("IsKey", str(self._object.IsKey))
self._display.add_row("IsNullable", str(self._object.IsNullable))
self._display.add_row("State", str(self._object.State))
self._display.add_row("DisplayFolder", str(self._object.DisplayFolder))
|
get_dependencies()
Returns the dependant columns of a measure.
Source code in pytabular/column.py
| def get_dependencies(self) -> pd.DataFrame:
"""Returns the dependant columns of a measure."""
dmv_query = f"select * from $SYSTEM.DISCOVER_CALC_DEPENDENCY where [OBJECT] = \
'{self.Name}' and [TABLE] = '{self.Table.Name}'"
return self.Table.Model.query(dmv_query)
|
get_sample_values(top_n=3)
Get sample values of column.
Source code in pytabular/column.py
| def get_sample_values(self, top_n: int = 3) -> pd.DataFrame:
"""Get sample values of column."""
column_to_sample = f"'{self.Table.Name}'[{self.Name}]"
try:
# adding temporary try except. TOPNSKIP will not work for directquery mode.
# Need an efficient way to identify if query is direct query or not.
dax_query = f"""EVALUATE
TOPNSKIP(
{top_n},
0,
FILTER(
VALUES({column_to_sample}),
NOT ISBLANK({column_to_sample})
&& LEN({column_to_sample}) > 0
),
1
)
ORDER BY {column_to_sample}
"""
return self.Table.Model.query(dax_query)
except Exception:
# This is really tech debt anyways and should be replaced...
dax_query = f"""
EVALUATE
TOPN(
{top_n},
FILTER(
VALUES({column_to_sample}),
NOT ISBLANK({column_to_sample}) && LEN({column_to_sample}) > 0
)
)
"""
return self.Table.Model.query(dax_query)
|
distinct_count(no_blank=False)
Get the DISTINCTCOUNT
of a column.
Parameters:
Name |
Type |
Description |
Default |
no_blank |
bool
|
If True , will call DISTINCTCOUNTNOBLANK .
Defaults to False .
|
False
|
Returns:
Name | Type |
Description |
int |
int
|
Number of Distinct Count from column.
If no_blank == True then will return number of distinct count no blanks.
|
Source code in pytabular/column.py
| def distinct_count(self, no_blank=False) -> int:
"""Get the `DISTINCTCOUNT` of a column.
Args:
no_blank (bool, optional): If `True`, will call `DISTINCTCOUNTNOBLANK`.
Defaults to `False`.
Returns:
int: Number of Distinct Count from column.
If `no_blank == True` then will return number of distinct count no blanks.
"""
func = "DISTINCTCOUNT"
if no_blank:
func += "NOBLANK"
return self.Table.Model.Adomd.query(
f"EVALUATE {{{func}('{self.Table.Name}'[{self.Name}])}}"
)
|
values()
Get single column DataFrame of values in column.
Similar to get_sample_values()
but will return all.
Returns:
Type |
Description |
DataFrame
|
pd.DataFrame: Single column DataFrame of values.
|
Source code in pytabular/column.py
| def values(self) -> pd.DataFrame:
"""Get single column DataFrame of values in column.
Similar to `get_sample_values()` but will return **all**.
Returns:
pd.DataFrame: Single column DataFrame of values.
"""
return self.Table.Model.Adomd.query(
f"EVALUATE VALUES('{self.Table.Name}'[{self.Name}])"
)
|