Skip to content
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
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies:
- pytest-cov
- pytest-mock
- pip:
- semantic-link-sempy>=0.12.0
- semantic-link-sempy>=0.12.1
- azure-identity==1.7.1
- azure-storage-blob>=12.9.0
- pandas-stubs
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ classifiers = [
license= { text = "MIT License" }

dependencies = [
"semantic-link-sempy>=0.12.0",
"semantic-link-sempy>=0.12.1",
"anytree",
"polib",
"jsonpath_ng",
Expand Down
2 changes: 2 additions & 0 deletions src/sempy_labs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
list_server_properties,
list_semantic_model_errors,
list_synonyms,
list_user_defined_functions,
)
from ._helper_functions import (
get_item_definition,
Expand Down Expand Up @@ -605,4 +606,5 @@
"set_workspace_network_communication_policy",
"get_warehouse_connection_string",
"list_data_access_roles",
"list_user_defined_functions",
]
47 changes: 47 additions & 0 deletions src/sempy_labs/_list_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,53 @@ def update_item(
)


@log
def list_user_defined_functions(
dataset: str | UUID, workspace: Optional[str | UUID] = None
) -> pd.DataFrame:
"""
Shows a list of the user-defined functions within a semantic model.

Parameters
----------
dataset: str | uuid.UUID
Name or UUID of the semantic model.
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.

Returns
-------
pandas.DataFrame
A pandas dataframe showing a list of the user-defined functions within a semantic model.
"""

from sempy_labs.tom import connect_semantic_model

columns = {
"Function Name": "string",
"Expression": "string",
"Lineage Tag": "string",
}
df = _create_dataframe(columns=columns)
rows = []
with connect_semantic_model(dataset=dataset, workspace=workspace) as tom:
for f in tom.model.Functions:
rows.append(
{
"Function Name": f.Name,
"Expression": f.Expression,
"Lineage Tag": f.LineageTag,
}
)

if rows:
df = pd.DataFrame(rows)

return df


@log
def list_relationships(
dataset: str | UUID, workspace: Optional[str | UUID] = None, extended: bool = False
Expand Down
69 changes: 65 additions & 4 deletions src/sempy_labs/tom/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ def __init__(self, dataset, workspace, readonly):

self._table_map = {}
self._column_map = {}
self._compat_level = self.model.Model.Database.CompatibilityLevel
self._compat_level = self.model.Database.CompatibilityLevel

# Max compat level
s = self.model.Server.SupportedCompatibilityLevels
nums = [int(x) for x in s.split(",") if x.strip() != "1000000"]
self._max_compat_level = max(nums)

# Minimum campat level for lineage tags is 1540 (https://learn.microsoft.com/dotnet/api/microsoft.analysisservices.tabular.table.lineagetag?view=analysisservices-dotnet#microsoft-analysisservices-tabular-table-lineagetag)
if self._compat_level >= 1540:
Expand Down Expand Up @@ -760,6 +765,60 @@ def add_role(
obj.Description = description
self.model.Roles.Add(obj)

def set_compatibility_level(self, compatibility_level: int):
"""
Sets compatibility level of the semantic model

Parameters
----------
compatibility_level : int
The compatibility level to set the for the semantic model.
"""
import Microsoft.AnalysisServices.Tabular as TOM

if compatibility_level < 1500 or compatibility_level > self._max_compat_level:
raise ValueError(
f"{icons.red_dot} Compatibility level must be between 1500 and {self._max_compat_level}."
)
if self._compat_level > compatibility_level:
print(
f"{icons.warning} Compatibility level can only be increased, not decreased."
)
return

self.model.Database.CompatibilityLevel = compatibility_level
bim = TOM.JsonScripter.ScriptCreateOrReplace(self.model.Database)
fabric.execute_tmsl(script=bim, workspace=self._workspace_id)

def set_user_defined_function(self, name: str, expression: str):
"""
Sets the definition of a `user-defined <https://learn.microsoft.com/en-us/dax/best-practices/dax-user-defined-functions#using-model-explorer>_` function within the semantic model. This function requires that the compatibility level is at least 1702.

Parameters
----------
name : str
Name of the user-defined function.
expression : str
The DAX expression for the user-defined function.
"""
import Microsoft.AnalysisServices.Tabular as TOM

if self._compat_level < 1702:
raise ValueError(
f"{icons.warning} User-defined functions require a compatibility level of at least 1702. The current compatibility level is {self._compat_level}. Use the 'tom.set_compatibility_level' function to change the compatibility level."
)

existing = [f.Name for f in self.model.Functions]

if name in existing:
self.model.Functions[name].Expression = expression
else:
obj = TOM.Function()
obj.Name = name
obj.Expression = expression
obj.LineageTag = generate_guid()
self.model.Functions.Add(obj)

def set_rls(self, role_name: str, table_name: str, filter_expression: str):
"""
Sets the row level security permissions for a table within a role.
Expand Down Expand Up @@ -1909,6 +1968,8 @@ def remove_object(self, object):
object.Parent.CalculationItems.Remove(object.Name)
elif objType == TOM.ObjectType.TablePermission:
object.Parent.TablePermissions.Remove(object.Name)
elif objType == TOM.ObjectType.Function:
object.Parent.Functions.Remove(object.Name)

def used_in_relationships(self, object: Union["TOM.Table", "TOM.Column"]):
"""
Expand Down Expand Up @@ -4752,8 +4813,8 @@ def set_value_filter_behavior(self, value_filter_behavior: str = "Automatic"):
value_filter_behavior = value_filter_behavior.capitalize()
min_compat = 1606

if self.model.Model.Database.CompatibilityLevel < min_compat:
self.model.Model.Database.CompatibilityLevel = min_compat
if self.model.Database.CompatibilityLevel < min_compat:
self.model.Database.CompatibilityLevel = min_compat

self.model.ValueFilterBehavior = System.Enum.Parse(
TOM.ValueFilterBehaviorType, value_filter_behavior
Expand Down Expand Up @@ -5822,7 +5883,7 @@ def _process_dax_objects(object_type, model_accessor=None):
import Microsoft.AnalysisServices.Tabular as TOM

# ChangedProperty logic (min compat level is 1567) https://learn.microsoft.com/dotnet/api/microsoft.analysisservices.tabular.changedproperty?view=analysisservices-dotnet
if self.model.Model.Database.CompatibilityLevel >= 1567:
if self.model.Database.CompatibilityLevel >= 1567:
for t in self.model.Tables:
if any(
p.SourceType == TOM.PartitionSourceType.Entity
Expand Down