Skip to content

Commit 162f542

Browse files
committed
update the function to support tables
1 parent d51c8c5 commit 162f542

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/sempy_labs/directlake/_generate_shared_expression.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
_base_api,
44
resolve_lakehouse_name_and_id,
55
resolve_item_name_and_id,
6+
_get_fabric_context_setting,
67
)
78
from typing import Optional
89
import sempy_labs._icons as icons
@@ -85,4 +86,7 @@ def generate_shared_expression(
8586
return f"{start_expr}{mid_expr}{end_expr}"
8687
else:
8788
# Build DL/OL expression
88-
return f"""let\n\tSource = AzureStorage.DataLake("onelake.dfs.fabric.microsoft.com/{workspace_id}/{item_id}")\nin\n\tSource"""
89+
env = _get_fabric_context_setting("spark.trident.pbienv").lower()
90+
env = "" if env == "prod" else f"{env}-"
91+
92+
return f"""let\n\tSource = AzureStorage.DataLake("https://{env}onelake.dfs.fabric.microsoft.com/{workspace_id}/{item_id}")\nin\n\tSource"""

src/sempy_labs/directlake/_update_directlake_model_lakehouse_connection.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
)
88
from sempy._utils._log import log
99
from sempy_labs.tom import connect_semantic_model
10-
from typing import Optional
10+
from typing import Optional, List
1111
import sempy_labs._icons as icons
1212
from uuid import UUID
1313
import re
@@ -19,7 +19,9 @@ def _extract_expression_list(expression):
1919
"""
2020

2121
pattern_sql = r'Sql\.Database\s*\(\s*"([^"]+)"\s*,\s*"([^"]+)"\s*\)'
22-
pattern_no_sql = r'AzureStorage\.DataLake\(".*?/([0-9a-fA-F\-]{36})/([0-9a-fA-F\-]{36})"'
22+
pattern_no_sql = (
23+
r'AzureStorage\.DataLake\(".*?/([0-9a-fA-F\-]{36})/([0-9a-fA-F\-]{36})"'
24+
)
2325

2426
match_sql = re.search(pattern_sql, expression)
2527
match_no_sql = re.search(pattern_no_sql, expression)
@@ -102,7 +104,7 @@ def update_direct_lake_model_connection(
102104
source_type: str = "Lakehouse",
103105
source_workspace: Optional[str | UUID] = None,
104106
use_sql_endpoint: bool = True,
105-
tables: Optional[str] = None,
107+
tables: Optional[str | List[str]] = None,
106108
):
107109
"""
108110
Remaps a Direct Lake semantic model's SQL Endpoint connection to a new lakehouse/warehouse.
@@ -127,6 +129,10 @@ def update_direct_lake_model_connection(
127129
use_sql_endpoint : bool, default=True
128130
If True, the SQL Endpoint will be used for the connection.
129131
If False, Direct Lake over OneLake will be used.
132+
tables : str | List[str], default=None
133+
The name(s) of the table(s) to update in the Direct Lake semantic model.
134+
If None, all tables will be updated (if there is only one expression).
135+
If multiple tables are specified, they must be provided as a list.
130136
"""
131137
if use_sql_endpoint:
132138
icons.sll_tags.append("UpdateDLConnection_SQL")
@@ -192,11 +198,16 @@ def update_direct_lake_model_connection(
192198
)
193199
else:
194200
import sempy
201+
195202
sempy.fabric._client._utils._init_analysis_services()
196203
import Microsoft.AnalysisServices.Tabular as TOM
204+
197205
expr_list = _extract_expression_list(shared_expression)
198206

199-
expr_name = next((name for name, exp in expression_dict.items() if exp == expr_list), None)
207+
expr_name = next(
208+
(name for name, exp in expression_dict.items() if exp == expr_list),
209+
None,
210+
)
200211

201212
# If the expression does not already exist, create it
202213
def generate_unique_name(existing_names):
@@ -218,15 +229,9 @@ def generate_unique_name(existing_names):
218229
f"{icons.red_dot} The table '{t_name}' does not exist in the '{dataset_name}' semantic model within the '{workspace_name}' workspace."
219230
)
220231
p = next(p for p in tom.model.Tables[t_name].Partitions)
221-
entity_name = p.EntityName
222-
schema_name = p.SchemaName
223232
if p.Mode != TOM.ModeType.DirectLake:
224233
raise ValueError(
225234
f"{icons.red_dot} The table '{t_name}' in the '{dataset_name}' semantic model within the '{workspace_name}' workspace is not in Direct Lake mode. This function is only applicable to Direct Lake tables."
226235
)
227236

228-
ep = TOM.EntityPartitionSource()
229-
ep.Source.EntityName = entity_name
230-
ep.ExpressionSource = tom.model.Expressions[expr_name]
231-
ep.Source.SchemaName = schema_name
232-
p.Source = ep
237+
p.Source.ExpressionSource = tom.model.Expressions[expr_name]

0 commit comments

Comments
 (0)