@define()
class Column(CommandMixin, SsasRenameRecord): # pyright: ignore[reportIncompatibleMethodOverride]
"""A column of an SSAS table.
PowerBI spec: [Power BI](https://learn.microsoft.com/en-us/analysis-services/tabular-models/column-properties-ssas-tabular?view=asallproducts-allversions)
SSAS spec: [Microsoft](https://learn.microsoft.com/en-us/openspecs/sql_server_protocols/ms-ssas-t/00a9ec7a-5f4d-4517-8091-b370fe2dc18b)
"""
_commands: RenameCommands = field(default=SsasCommands.column, init=False, repr=False)
def __repr__(self) -> str:
return f"Column({self.id}: {self.full_name()})"
def parents_base(self) -> "frozenset[LinkedEntity]":
"""Returns all columns and measures this Column is dependent on."""
return (
LinkedEntity.from_iter({self.table()}, by="table")
| LinkedEntity.from_iter(
self.parent_columns(),
by="parent_column",
)
| LinkedEntity.from_iter(self.parent_measures(), by="parent_measure")
| LinkedEntity.from_iter({self.sort_by_column()}, by="sort_by_column")
| LinkedEntity.from_iter({self.column_origin()}, by="column_origin")
)
def children_base(self) -> "frozenset[LinkedEntity]":
"""Returns all columns and measures dependent on this Column."""
return (
LinkedEntity.from_iter(self.annotations(), by="annotation")
| LinkedEntity.from_iter({self.attribute_hierarchy()}, by="attribute_hierarchy")
| LinkedEntity.from_iter(
self.child_columns(),
by="child_column",
)
| LinkedEntity.from_iter(self.child_measures(), by="child_measure")
| LinkedEntity.from_iter(
self.origin_columns(),
by="origin_column",
)
| LinkedEntity.from_iter(self.sorting_columns(), by="sorting_column")
| LinkedEntity.from_iter(
self.child_variations(),
by="child_variation",
)
| LinkedEntity.from_iter(
self.child_default_variations(),
by="child_default_variation",
)
| LinkedEntity.from_iter(self.from_relationships(), by="from_relationship")
| LinkedEntity.from_iter(
self.to_relationships(),
by="to_relationship",
)
| LinkedEntity.from_iter(self.perspective_columns(), by="perspective_column")
| LinkedEntity.from_iter({self.format_string_definition()}, by="format_string_definition")
)
def set_name(self, new_name: str, layout: "Layout") -> None:
"""Renames the column and update any dependent expressions to use the new name.
Since measures are referenced by name in DAX expressions, renaming a measure will break any dependent
expressions.
"""
columns = _get_columns_sources(self, layout)
for c in columns:
c.Column.Property = new_name
if c.NativeReferenceName == self.name():
c.NativeReferenceName = new_name
hierarchies = _get_hierarchies_sources(self, layout)
for h in hierarchies:
if isinstance(h.Hierarchy.Expression, SourceRef):
h.Hierarchy.Hierarchy = new_name
elif isinstance(h.Hierarchy.Expression, _PropertyVariationSourceHelper):
h.Hierarchy.Expression.PropertyVariationSource.Property = new_name
else:
h.Hierarchy.Hierarchy = new_name
set_name.fix_dax(self, new_name)
self.explicit_name = new_name