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

added support for calc tables #510

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/sempy_labs/_model_bpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ def translate_using_spark(rule_file):
lambda obj: format_dax_object_name(obj.Parent.Name, obj.Name),
),
"Table": (tom.model.Tables, lambda obj: obj.Name),
"Calculated Table": (tom.all_calculated_tables(), lambda obj: obj.Name),
"Role": (tom.model.Roles, lambda obj: obj.Name),
"Model": (tom.model, lambda obj: obj.Model.Name),
"Calculation Item": (
Expand Down Expand Up @@ -322,6 +323,10 @@ def translate_using_spark(rule_file):
x = [nm(obj) for obj in tom.all_hierarchies() if expr(obj, tom)]
elif scope == "Table":
x = [nm(obj) for obj in tom.model.Tables if expr(obj, tom)]
elif scope == "Calculated Table":
x = [
nm(obj) for obj in tom.all_calculated_tables() if expr(obj, tom)
]
elif scope == "Relationship":
x = [nm(obj) for obj in tom.model.Relationships if expr(obj, tom)]
elif scope == "Role":
Expand Down
4 changes: 2 additions & 2 deletions src/sempy_labs/_model_bpa_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def model_bpa_rules(
),
(
"DAX Expressions",
"Measure",
["Measure", "Calculated Table"],
"Error",
"Column references should be fully qualified",
lambda obj, tom: any(
Expand All @@ -576,7 +576,7 @@ def model_bpa_rules(
),
(
"DAX Expressions",
"Measure",
["Measure", "Calculated Table"],
"Error",
"Measure references should be unqualified",
lambda obj, tom: any(
Expand Down
34 changes: 29 additions & 5 deletions src/sempy_labs/tom/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3281,9 +3281,14 @@ def depends_on(self, object, dependencies: pd.DataFrame):

if objType == TOM.ObjectType.Table:
objParentName = objName
object_types = ["Table", "Calc Table"]
elif objType == TOM.ObjectType.Column:
object_types = ["Column", "Calc Column"]
else:
object_types = [str(objType)]

fil = dependencies[
(dependencies["Object Type"] == str(objType))
(dependencies["Object Type"].isin(object_types))
& (dependencies["Table Name"] == objParentName)
& (dependencies["Object Name"] == objName)
]
Expand Down Expand Up @@ -3389,11 +3394,20 @@ def fully_qualified_measures(
dependencies["Object Name"] == dependencies["Parent Node"]
]

if object.ObjectType == TOM.ObjectType.Measure:
expr = object.Expression
elif object.ObjectType == TOM.ObjectType.Table:
part = next(p for p in object.Partitions)
if part.SourceType != TOM.PartitionSourceType.Calculated:
return
expr = part.Source.Expression
else:
return

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 (
format_dax_object_name(obj.Parent.Name, obj.Name)
in object.Expression
if (f"{obj.Parent.Name}[{obj.Name}]" in expr) or (
format_dax_object_name(obj.Parent.Name, obj.Name) in expr
):
yield obj

Expand Down Expand Up @@ -3427,6 +3441,16 @@ def create_pattern(tableList, b):
combined_pattern = "".join(patterns) + re.escape(f"[{b}]")
return combined_pattern

if object.ObjectType == TOM.ObjectType.Measure:
expr = object.Expression
elif object.ObjectType == TOM.ObjectType.Table:
part = next(p for p in object.Partitions)
if part.SourceType != TOM.PartitionSourceType.Calculated:
return
expr = part.Source.Expression
else:
return

for obj in self.depends_on(object=object, dependencies=dependencies):
if obj.ObjectType == TOM.ObjectType.Column:
tableList = []
Expand All @@ -3436,7 +3460,7 @@ def create_pattern(tableList, b):
if (
re.search(
create_pattern(tableList, re.escape(obj.Name)),
object.Expression,
expr,
)
is not None
):
Expand Down