Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed dependencies and unqualified_columns #372

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
m-kovalsky marked this conversation as resolved.
Show resolved Hide resolved
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
Loading