Skip to content

Commit

Permalink
added list_semantic_model_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kovalsky committed Dec 16, 2024
1 parent b32b3a1 commit 0960a44
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/sempy_labs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
list_lakehouses,
list_sql_endpoints,
update_item,
list_semantic_model_errors,
)
from sempy_labs._helper_functions import (
convert_to_friendly_case,
Expand Down Expand Up @@ -458,4 +459,5 @@
"update_vnet_gateway",
"update_on_premises_gateway",
"get_semantic_model_definition",
"list_semantic_model_errors",
]
69 changes: 69 additions & 0 deletions src/sempy_labs/_list_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,3 +1575,72 @@ def list_semantic_model_object_report_usage(
final_df.reset_index(drop=True, inplace=True)

return final_df

def list_semantic_model_errors(dataset: str | UUID, workspace: Optional[str | UUID]) -> pd.DataFrame:
"""
Shows a list of a semantic model's errors and their error messages (if they exist).
Parameters
----------
dataset : str | UUID
Name or ID of the semantic model.
workspace : str | UUID, default=None
The Fabric workspace name or ID.
Defaults to None which resolves to the workspace of the attached lakehouse
or if no lakehouse attached, resolves to the workspace of the notebook.
Returns
-------
pandas.DataFrame
A pandas dataframe showing a list of the errors and error messages for a given semantic model.
"""

from sempy_labs.tom import connect_semantic_model

(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
(dataset_name, dataset_id) = resolve_dataset_name_and_id(
dataset, workspace=workspace_id
)

error_rows = []

# Helper function to add a new error row
def add_error_row(object_type, table_name, object_name, error_message):
error_rows.append({
"Object Type": object_type,
"Table Name": table_name,
"Object Name": object_name,
"Error Message": error_message,
})

with connect_semantic_model(dataset=dataset_id, workspace=workspace_id, readonly=True) as tom:
# Define mappings of TOM objects to object types and attributes
error_checks = [
("Column", tom.all_columns, lambda o: o.ErrorMessage),
("Partition", tom.all_partitions, lambda o: o.ErrorMessage),
("Partition - Data Coverage Expression",
tom.all_partitions, lambda o: o.DataCoverageDefinition.ErrorMessage if o.DataCoverageDefinition else ""),
("Row Level Security", tom.all_rls, lambda o: o.ErrorMessage),
("Calculation Item", tom.all_calculation_items, lambda o: o.ErrorMessage),
("Measure", tom.all_measures, lambda o: o.ErrorMessage),
("Measure - Detail Rows Expression",
tom.all_measures, lambda o: o.DetailRowsDefinition.ErrorMessage if o.DetailRowsDefinition else ""),
("Measure - Format String Expression",
tom.all_measures, lambda o: o.FormatStringDefinition.ErrorMessage if o.FormatStringDefinition else ""),
("Calculation Group - Multiple or Empty Selection Expression",
tom.all_calculation_groups, lambda o: o.CalculationGroup.MultipleOrEmptySelectionExpression.ErrorMessage
if o.CalculationGroup.MultipleOrEmptySelectionExpression else ""),
("Calculation Group - No Selection Expression",
tom.all_calculation_groups, lambda o: o.CalculationGroup.NoSelectionExpression.ErrorMessage
if o.CalculationGroup.NoSelectionExpression else "")
]

# Iterate over all error checks
for object_type, getter, error_extractor in error_checks:
for obj in getter():
error_message = error_extractor(obj)
if error_message: # Only add rows if there's an error message
add_error_row(object_type, obj.Parent.Name, obj.Name, error_message)

# Create the DataFrame from collected rows
return pd.DataFrame(error_rows)

0 comments on commit 0960a44

Please sign in to comment.