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

Refactor get_measure_dependencies function #337

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
135 changes: 49 additions & 86 deletions src/sempy_labs/_model_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,109 +30,69 @@ def get_measure_dependencies(
"""

workspace = fabric.resolve_workspace_name(workspace)

dep = fabric.evaluate_dax(
dataset=dataset,
workspace=workspace,
dax_string="""
SELECT
[TABLE] AS [Table Name]
,[OBJECT] AS [Object Name]
,[OBJECT_TYPE] AS [Object Type]
,[REFERENCED_TABLE] AS [Referenced Table]
,[REFERENCED_OBJECT] AS [Referenced Object]
,[REFERENCED_OBJECT_TYPE] AS [Referenced Object Type]
[TABLE] AS [Table Name],
[OBJECT] AS [Object Name],
[OBJECT_TYPE] AS [Object Type],
[REFERENCED_TABLE] AS [Referenced Table],
[REFERENCED_OBJECT] AS [Referenced Object],
[REFERENCED_OBJECT_TYPE] AS [Referenced Object Type]
FROM $SYSTEM.DISCOVER_CALC_DEPENDENCY
WHERE [OBJECT_TYPE] = 'MEASURE'
""",
)

# Format data columns
dep["Object Type"] = dep["Object Type"].str.capitalize()
dep["Referenced Object Type"] = dep["Referenced Object Type"].str.capitalize()

dep["Full Object Name"] = format_dax_object_name(
dep["Table Name"], dep["Object Name"]
)
dep["Referenced Full Object Name"] = format_dax_object_name(
dep["Referenced Table"], dep["Referenced Object"]
)
dep["Parent Node"] = dep["Object Name"]

df = dep

df["Done"] = df.apply(
lambda row: False if row["Referenced Object Type"] == "Measure" else True,
axis=1,
)

while any(df["Done"] == False):
for i, r in df.iterrows():
rObjFull = r["Referenced Full Object Name"]
rObj = r["Referenced Object"]
if r["Done"] == False:
dep_filt = dep[dep["Full Object Name"] == rObjFull]

for index, dependency in dep_filt.iterrows():
d = True
if dependency.iloc[5] == "Measure":
d = False
df = pd.concat(
[
df,
pd.DataFrame(
[
{
"Table Name": r["Table Name"],
"Object Name": r["Object Name"],
"Object Type": r["Object Type"],
"Referenced Object": dependency.iloc[4],
"Referenced Table": dependency.iloc[3],
"Referenced Object Type": dependency.iloc[
5
],
"Done": d,
"Full Object Name": r["Full Object Name"],
"Referenced Full Object Name": dependency.iloc[
7
],
"Parent Node": rObj,
}
]
),
],
ignore_index=True,
)
else:
df = pd.concat(
[
df,
pd.DataFrame(
[
{
"Table Name": r["Table Name"],
"Object Name": r["Object Name"],
"Object Type": r["Object Type"],
"Referenced Object": dependency.iloc[4],
"Referenced Table": dependency.iloc[3],
"Referenced Object Type": dependency.iloc[
5
],
"Done": d,
"Full Object Name": r["Full Object Name"],
"Referenced Full Object Name": dependency.iloc[
7
],
"Parent Node": rObj,
}
]
),
],
ignore_index=True,
)

df.loc[i, "Done"] = True

df = df.drop(["Done", "Full Object Name", "Referenced Full Object Name"], axis=1)
# Initialize dependency DataFrame with 'Done' status
df = dep.copy()
df["Done"] = df["Referenced Object Type"].apply(
lambda x: x != "Measure"
).astype(bool)
# Expand dependencies iteratively
while not df["Done"].all():
incomplete_rows = df[df["Done"] == False]
for _, row in incomplete_rows.iterrows():
referenced_full_name = row["Referenced Full Object Name"]
referenced_object_type = row["Referenced Object Type"]
dep_filt = dep[
(dep["Full Object Name"] == referenced_full_name)
& (dep["Object Type"] == referenced_object_type)
]
# Expand dependencies and update 'Done' status as needed
new_rows = []
for _, dependency in dep_filt.iterrows():
is_done = dependency["Referenced Object Type"] != "Measure"
new_row = {
"Table Name": row["Table Name"],
"Object Name": row["Object Name"],
"Object Type": row["Object Type"],
"Referenced Table": dependency["Referenced Table"],
"Referenced Object": dependency["Referenced Object"],
"Referenced Object Type": dependency["Referenced Object Type"],
"Done": is_done,
"Full Object Name": row["Full Object Name"],
"Referenced Full Object Name": dependency[
"Referenced Full Object Name"
],
"Parent Node": row["Referenced Object"],
}
new_rows.append(new_row)
df = pd.concat([df, pd.DataFrame(new_rows)], ignore_index=True)
df.loc[df.index == row.name, "Done"] = True
# Finalize DataFrame and yield result
df = df.drop(columns=["Done", "Full Object Name", "Referenced Full Object Name"])

return df

Expand Down Expand Up @@ -211,6 +171,7 @@ def get_model_calc_dependencies(
"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 @@ -222,7 +183,9 @@ 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)
new_rows = pd.DataFrame(new_rows)
new_rows = new_rows.dropna(axis=1, how="all") # Drop empty columns
df = pd.concat([df, new_rows], ignore_index=True)
df.loc[df.index == row.name, "Done"] = True
# Finalize DataFrame and yield result
df = df.drop(columns=["Done"])
Expand Down
Loading