diff --git a/src/sempy_labs/semantic_model/_Fix_DoNotSummarize.py b/src/sempy_labs/semantic_model/_Fix_DoNotSummarize.py new file mode 100644 index 000000000..eea8a0993 --- /dev/null +++ b/src/sempy_labs/semantic_model/_Fix_DoNotSummarize.py @@ -0,0 +1,61 @@ +# Fix "Do not summarize numeric columns" — standalone BPA fixer. +# Sets SummarizeBy to None on all numeric data columns. + +from typing import Optional +from uuid import UUID +from sempy._utils._log import log + + +@log +def fix_do_not_summarize( + dataset: str | UUID, + workspace: Optional[str | UUID] = None, + scan_only: bool = False, +) -> int: + """ + Sets SummarizeBy to None on numeric data columns. + + Parameters + ---------- + dataset : str | UUID + Name or ID of the semantic model. + workspace : str | uuid.UUID, default=None + The Fabric workspace name or ID. + scan_only : bool, default=False + If True, only reports what would be fixed without making changes. + + Returns + ------- + int + Number of items fixed. + """ + from sempy_labs.tom import connect_semantic_model + + fixed = 0 + with connect_semantic_model(dataset=dataset, readonly=scan_only, workspace=workspace) as tom: + import Microsoft.AnalysisServices.Tabular as TOM + + for table in tom.model.Tables: + for col in table.Columns: + if col.Type != TOM.ColumnType.Data: + continue + if col.DataType not in (TOM.DataType.Int64, TOM.DataType.Double, TOM.DataType.Decimal): + continue + if str(col.SummarizeBy) == "None": + continue + if scan_only: + print(f" Would fix: '{table.Name}'[{col.Name}] SummarizeBy={col.SummarizeBy} → None") + else: + none_attr = getattr(TOM.AggregateFunction, "None_", None) + if none_attr is None: + raise RuntimeError( + f"Could not set SummarizeBy=None on column \"{col.Name}\": " + f"TOM.AggregateFunction.None_ is not available in this environment." + ) + col.SummarizeBy = none_attr + print(f" Fixed: '{table.Name}'[{col.Name}] → SummarizeBy=None") + fixed += 1 + + action = "Would fix" if scan_only else "Fixed" + print(f" {action} {fixed} column(s).") + return fixed diff --git a/src/sempy_labs/semantic_model/__init__.py b/src/sempy_labs/semantic_model/__init__.py index 7c117f392..32eddc7dd 100644 --- a/src/sempy_labs/semantic_model/__init__.py +++ b/src/sempy_labs/semantic_model/__init__.py @@ -6,10 +6,12 @@ from ._caching import ( enable_query_caching, ) +from ._Fix_DoNotSummarize import fix_do_not_summarize __all__ = [ "approved_for_copilot", "set_endorsement", "make_discoverable", "enable_query_caching", + "fix_do_not_summarize", ]