Skip to content

Commit

Permalink
fixed dependencies and unqualified_columns
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kovalsky committed Dec 19, 2024
1 parent 06d1423 commit 5d55393
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/sempy_labs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from sempy_labs._job_scheduler import (
list_item_job_instances
)
from sempy_labs._job_scheduler import list_item_job_instances
from sempy_labs._gateways import (
list_gateway_members,
list_gateway_role_assigments,
Expand Down
43 changes: 38 additions & 5 deletions src/sempy_labs/_model_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,18 @@ def get_measure_dependencies(

@log
def get_model_calc_dependencies(
dataset: str | UUID, workspace: Optional[str] = None
dataset: str | UUID,
workspace: Optional[str] = None,
) -> pd.DataFrame:
"""
Shows all dependencies for all objects in a semantic model.
Parameters
----------
dataset : str | UUID
dataset : str | uuid.UUID
Name or ID of the semantic model.
workspace : str, default=None
The Fabric workspace name.
workspace : str | uuid.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.
Expand Down Expand Up @@ -189,12 +190,24 @@ def get_model_calc_dependencies(
dep["Referenced Table"], dep["Referenced Object"]
)
dep["Parent Node"] = dep["Object Name"]

# Initialize dependency DataFrame with 'Done' status
df = dep.copy()
objs = {"Measure", "Calc Column", "Calculation Item", "Calc Table"}
df["Done"] = (
df["Referenced Object Type"].apply(lambda x: x not in objs).astype(bool)
)

# Set to track visited dependencies to prevent circular references
visited = set(
zip(
df["Full Object Name"],
df["Referenced Full Object Name"],
df["Object Type"],
df["Referenced Object Type"],
)
)

# Expand dependencies iteratively
while not df["Done"].all():
incomplete_rows = df[df["Done"] == False]
Expand All @@ -208,11 +221,24 @@ def get_model_calc_dependencies(
# Expand dependencies and update 'Done' status as needed
new_rows = []
for _, dependency in dep_filt.iterrows():
# Check if the dependency has already been visited
dependency_pair = (
row["Full Object Name"],
dependency["Referenced Full Object Name"],
row["Object Type"],
dependency["Referenced Object Type"],
)
if dependency_pair in visited:
continue # Skip already visited dependencies

visited.add(dependency_pair) # Mark as visited

is_done = dependency["Referenced Object Type"] not in objs
new_row = {
"Table Name": row["Table Name"],
"Object Name": row["Object Name"],
"Object Type": row["Object Type"],
"Expression": row["Expression"],
"Referenced Table": dependency["Referenced Table"],
"Referenced Object": dependency["Referenced Object"],
"Referenced Object Type": dependency["Referenced Object Type"],
Expand All @@ -224,7 +250,14 @@ def get_model_calc_dependencies(
"Parent Node": row["Referenced Object"],
}
new_rows.append(new_row)
df = pd.concat([df, pd.DataFrame(new_rows)], ignore_index=True)

if new_rows:
new_rows_df = pd.DataFrame(new_rows)
new_rows_df = new_rows_df.dropna(
axis=1, how="all"
) # Drop empty columns
df = pd.concat([df, new_rows_df], ignore_index=True)

df.loc[df.index == row.name, "Done"] = True
# Finalize DataFrame and yield result
df = df.drop(columns=["Done"])
Expand Down
12 changes: 10 additions & 2 deletions src/sempy_labs/tom/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3287,6 +3287,10 @@ def fully_qualified_measures(
"""
import Microsoft.AnalysisServices.Tabular as TOM

dependencies = dependencies[
dependencies["Object Name"] == dependencies["Parent Node"]
]

for obj in self.depends_on(object=object, dependencies=dependencies):
if obj.ObjectType == TOM.ObjectType.Measure:
if (f"{obj.Parent.Name}[{obj.Name}]" in object.Expression) or (
Expand All @@ -3313,12 +3317,16 @@ def unqualified_columns(self, object: "TOM.Column", dependencies: pd.DataFrame):
"""
import Microsoft.AnalysisServices.Tabular as TOM

dependencies = dependencies[
dependencies["Object Name"] == dependencies["Parent Node"]
]

def create_pattern(tableList, b):
patterns = [
r"(?<!" + re.escape(table) + r"\[)(?<!" + re.escape(table) + r"'\[)"
r"(?<!" + re.escape(table) + r")(?<!'" + re.escape(table) + r"')"
for table in tableList
]
combined_pattern = "".join(patterns) + re.escape(b)
combined_pattern = "".join(patterns) + re.escape(f"[{b}]")
return combined_pattern

for obj in self.depends_on(object=object, dependencies=dependencies):
Expand Down

0 comments on commit 5d55393

Please sign in to comment.