### Quick Commands - Install and Test Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Commands for installing the package and running tests. ```bash pip install -e . pytest -s tests/ pytest -s tests/ -k test_name ``` -------------------------------- ### Docstring example Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Example of a docstring referencing an API. ```python """ This is a wrapper function for the following API: `Items - List Items `_. """ ``` -------------------------------- ### Parameter Documentation Format Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Example of how to document parameters in docstrings using numpydoc style. ```python Parameters ---------- item_type : str, default=None Filter by item type. If None, returns all item types. 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. ``` -------------------------------- ### Writing Tests Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Example of a basic test function using pytest. ```python import pytest def test_my_function(): """Test my_function basic behavior.""" from sempy_labs import my_function result = my_function(input_value) assert isinstance(result, pd.DataFrame) assert not result.empty ``` -------------------------------- ### With pagination Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Example of a request that uses pagination, returning a list of dictionaries. ```python responses = _base_api( request=url, uses_pagination=True, client="fabric_sp", ) ``` -------------------------------- ### Pip Install Recommendation Source: https://github.com/microsoft/semantic-link-labs/wiki/Frequently-Asked-Questions-(FAQ) Recommendation on using %pip install over !pip install in Fabric notebooks. ```shell %pip install ``` -------------------------------- ### Show Tenant Settings Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Example for retrieving tenant settings. ```python from sempy_labs import admin df = admin.list_tenant_settings() display(df) ``` -------------------------------- ### POST with payload Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Example of a POST request with a payload. ```python response = _base_api( request="/v1/workspaces/{workspace_id}/items", method="post", payload={"displayName": name}, status_codes=[201, 202], client="fabric_sp", ) ``` -------------------------------- ### Get the size of a semantic model Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Provides examples for retrieving the size of a single semantic model or all semantic models within a workspace. ```python import sempy_labs as labs import sempy.fabric as fabric dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides # Example 1: For a single semantic model model_size = labs.get_semantic_model_size(dataset=dataset, workspace=workspace) print(model_size) # Example 2: For all semantic models within a workspace model_sizes = {} dfD = fabric.list_datasets(workspace=workspace, mode="rest") for _, r in dfD.iterrows(): d_name = r["Dataset Name"] d_id = r["Dataset Id"] if not labs.is_default_semantic_model(dataset=d_id, workspace=workspace): model_size = labs.get_semantic_model_size(dataset=d_id, workspace=workspace) model_sizes[d_name] = model_size print(model_sizes) ``` -------------------------------- ### Export Function in __init__.py Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Example of how to export a new function from a module into the package's __init__.py. ```python from ._my_module import my_new_function __all__ = [ ..., "my_new_function", ] ``` -------------------------------- ### Show Connections Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Example for listing available connections. ```python import sempy_labs as labs df = labs.list_connections() display(df) ``` -------------------------------- ### Example: Well-Formed Public Function Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md An example of a well-formed public function adhering to the specified conventions. ```python from sempy._utils._log import log from sempy_labs._helper_functions import ( resolve_workspace_name_and_id, _base_api, _create_dataframe, ) import sempy_labs._icons as icons from typing import Optional from uuid import UUID import pandas as pd @log def list_items( item_type: Optional[str] = None, workspace: Optional[str | UUID] = None, ) -> pd.DataFrame: """ Returns a list of items in the specified workspace. This is a wrapper function for the following API: `Items - List Items `_. Service Principal Authentication is supported (see `here `_ for examples). Parameters ---------- item_type : str, default=None Filter by item type. If None, returns all item types. 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 the items in the workspace. """ (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) columns = { "Item Id": "string", "Item Name": "string", "Item Type": "string", } df = _create_dataframe(columns=columns) responses = _base_api( request=f"/v1/workspaces/{workspace_id}/items", uses_pagination=True, client="fabric_sp", ) # Process responses and build dataframe... return df ``` -------------------------------- ### Show Shortcuts Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Example for listing shortcuts within a specified lakehouse. ```python import sempy_labs.lakehouse as lake lakehouse = None # Enter the name or ID of the lakehouse workspace = None # Enter the name or ID of the workspace in which the lakehouse exists df = lake.list_shortcuts(lakehouse=lakehouse, workspace=workspace) df ``` -------------------------------- ### Install black Source: https://github.com/microsoft/semantic-link-labs/blob/main/README.md Installs the black code formatter with a specific version. ```cli pip install black==25.1.0 ``` -------------------------------- ### Install Build Module Source: https://github.com/microsoft/semantic-link-labs/blob/main/README.md Installs the build module required for creating wheel files. ```bash pip install build ``` -------------------------------- ### DAXLib.org Integration Examples Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples A collection of examples showcasing integration with DAXLib.org for managing DAX packages within a semantic model. ```python from sempy_labs import daxlib dataset = '' # Enter the name or ID of your semantic model) workspace = None # Enter the name or ID of the workspace in which the semantic model resides package_name = 'daxlib.formatstring' # Enter the package name # Example 1 (extract the TMDL of a DAXLib package) tmdl = daxlib.get_package_tmdl(package_name=package_name, version=None) # Example 2 (extracts a list of the functions and their expressions) daxlib.get_package_functions(package_name=package_name, version=None) # Example 3 (adds a package to a semantic model) daxlib.add_package_to_semantic_model(dataset=dataset, package_name=package_name, version=None, workspace=workspace) # Example 4 (updates the package in the semantic model (None defaults to the latest version)) daxlib.update_package_in_semantic_model(dataset=dataset, package_name=package_name, version=None, workspace=workspace) # Example 5 (removes a package from the semantic model) daxlib.remove_package_from_semantic_model(dataset=dataset, package_name=package_name, workspace=workspace) ``` -------------------------------- ### Create a OneLake Shortcut Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Example demonstrating how to create a OneLake shortcut, specifying source and destination lakehouses and workspaces. ```python import sempy_labs.lakehouse as lake table_name = 'MyTable' # Enter the name of the table on which the shortcut will be based source_lakehouse = 'MyLakehouse1' # Enter the name of the lakehouse in which the table exists source_workspace = 'MyLakehouse1Workspace' # Enter the name of the workspace in which the source lakehouse exists destination_lakehouse = 'MyLakehouse2' # Enter the name of the lakehouse in which the shortcut will be created destination_workspace = 'MyLakehouse2Workspace' # Enter the name of the workspace in which the destination lakehouse exists shortcut_name = None # Enter the name of the shortcut which will be created. By default it is named after the table_name lake.create_shortcut_onelake(table_name=table_name, source_lakehouse=source_lakehouse, source_workspace=source_workspace, destination_lakehouse=destination_lakehouse, destination_workspace=destination_workspace, shortcut_name=shortcut_name) ``` -------------------------------- ### Quick Commands - Documentation and Formatting Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Commands for building documentation and formatting code. ```bash cd docs && make html black src/sempy_labs tests ``` -------------------------------- ### Helper Functions - Icons and Messages Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Import statement for icons and example usage for printing messages. ```python import sempy_labs._icons as icons print(f"{icons.green_dot} Success message") print(f"{icons.red_dot} Error message") print(f"{icons.warning} Warning message") print(f"{icons.in_progress} In progress...") ``` -------------------------------- ### Local Documentation Build Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Commands to build documentation locally using Sphinx. ```bash cd docs sphinx-apidoc -f -o source ../src/sempy_labs/ make html ``` -------------------------------- ### Upgrade a report to PBIR format Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Placeholder for the code example to upgrade a report to PBIR format using `upgrade_to_pbir`. ```python import sempy_labs.report ``` -------------------------------- ### Step 1: Choose the Right Module Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Table indicating the appropriate module for different function types. ```python Function Type | Location | --------------|----------| General workspace/item operations | `_helper_functions.py` or dedicated `_*.py` | Admin operations | `admin/` submodule | Report operations | `report/` submodule | Lakehouse operations | `lakehouse/` submodule | Direct Lake operations | `directlake/` submodule | TOM operations | `tom/_model.py` | ``` -------------------------------- ### Get the theme from a report Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Retrieves the theme (base or custom) from a report. Requires the report to be in PBIR format. ```python from sempy_labs.report import connect_report report = '' # Name or ID of the report workspace = None # Name or ID of the workspace in which the report resides theme_type = None # Set this to 'customTheme' to get the custom theme. None defaults to the base theme with connect_report(report=report, workspace=workspace) as rpt: theme = rpt.get_theme(theme_type=theme_type) theme ``` -------------------------------- ### Backup a Semantic Model Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Examples for backing up a semantic model, with options for password protection and compression. ```python import sempy_labs as labs dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides backup_name = '' # Enter the name or path of the backup file # Optional key_vault_uri= '' # Enter the Key Vault URI which contains the secret for the password for the backup key_vault_password='' # Enter the secret name which contains the password for the backup backup_password = notebookutils.credentials.getSecret(key_vault_uri,key_vault_password) # Example 1: No password laps.backup_semantic_model( dataset=dataset, file_path=f'{backup_name}.abf', allow_overwrite=True, # If True, overwrites backup files of the same name. If False, the file you are saving cannot have the same name as a file that already exists in the same location. apply_compression=True, # If True, compresses the backup file. Compressed backup files save disk space, but require slightly higher CPU utilization. workspace=workspace, ) # Example 2: With password laps.backup_semantic_model( dataset=dataset, file_path=f'{backup_name}.abf', password=backup_password, allow_overwrite=True, # If True, overwrites backup files of the same name. If False, the file you are saving cannot have the same name as a file that already exists in the same location. apply_compression=True, # If True, compresses the backup file. Compressed backup files save disk space, but require slightly higher CPU utilization. workspace=workspace, ) ``` -------------------------------- ### Running Tests Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Commands to run tests using pytest. ```bash # Run all tests pytest -s tests/ # Run specific test pytest -s tests/ -k test_name ``` -------------------------------- ### List dataflows Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Example of how to list all dataflows within a specified workspace. ```python import sempy_labs as labs workspace = None df = labs.list_dataflows(workspace=workspace) display(df) ``` -------------------------------- ### Install .whl file in Fabric Notebook Source: https://github.com/microsoft/semantic-link-labs/blob/main/README.md Installs the generated .whl file into a Fabric notebook environment. ```python %pip install "" ``` -------------------------------- ### Search notebooks Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Provides examples of how to search for specific strings within notebooks, either in a single notebook, a single workspace, or multiple workspaces. ```python import sempy_labs as labs search_string = '' # Enter the string to search # Example 1: Search a single notebook labs.search_notebooks(search_string=search_string, notebook='MyNotebook', workspace='Workspace1') # Example 2: Search all notebooks in a single workspace labs.search_notebooks(search_string=search_string, notebook=None, workspace='Workspace1') # Example 3: Search all notebooks in a list of workspaces labs.search_notebooks(search_string=search_string, notebook=None, workspace=['Workspace1', 'Workspace2']) ``` -------------------------------- ### Show Columns within all Tables within a Lakehouse Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Example for retrieving columns from all tables within a specified lakehouse. ```python import sempy_labs.lakehouse as lake lakehouse = None # Enter the name or ID of the lakehouse workspace = None # Enter the name or ID of the workspace in which the lakehouse exists df = lake.get_lakehouse_columns(lakehouse=lakehouse, workspace=workspace) df ``` -------------------------------- ### Query workspace monitoring Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Queries workspace monitoring data using KQL or SQL. ```python import sempy_labs as labs workspace = None # Name or ID of the workspace kql_query = """ SemanticModelLogs | where OperationName == "QueryEnd" | extend ctx = parse_json(dynamic_to_json(ApplicationContext)) | extend DatasetId = tostring(ctx.DatasetId) | extend ReportId = tostring(ctx.Sources[0].ReportId) | extend VisualId = tostring(ctx.Sources[0].VisualId) | project WorkspaceName, ItemName, DurationMs, CpuTimeMs, Status, EventText, ApplicationContext, DatasetId, ReportId, VisualId """ sql_query = """ SELECT TOP 10 WorkspaceName, ItemName, DurationMs, CpuTimeMs, Status, EventText, ApplicationContext FROM SemanticModelLogs WHERE OperationName = 'QueryEnd' """ # Example 1: Using KQL labs.query_workspace_monitoring(query=kql_query, workspace=workspace, language="kql") ``` -------------------------------- ### Show Activity Events Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Example for listing activity events within a specified time range and filtering by activity type. ```python from sempy_labs import admin start_time = "2025-02-15T07:55:00" end_time = "2025-02-15T08:55:00" activity_filter = "viewreport" admin.list_activity_events(start_time=start_time, end_time=end_time, activity_filter=activity_filter) ``` -------------------------------- ### Pre-Commit Checklist Commands Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Commands to run before committing code changes. ```bash # 1. Format code black src/sempy_labs tests # 2. Check style violations flake8 src/sempy_labs tests # 3. Verify type correctness (optional but recommended) mypy src/sempy_labs # 4. Run relevant tests pytest -s tests/ -k # 5. Build documentation (if functions were added or modified) cd docs && sphinx-apidoc -f -o source ../src/sempy_labs/ && make html && cd .. ``` -------------------------------- ### TOM Wrapper Usage Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Example of using the TOM wrapper to connect to a semantic model and access/modify objects. ```python from sempy_labs.tom import connect_semantic_model with connect_semantic_model( dataset="My Model", readonly=False, # Set to True for read-only operations workspace="My Workspace" ) as tom: # Access model objects for measure in tom.all_measures(): print(measure.Name) # Modify model (requires readonly=False) tom.model.Tables["Sales"].Measures["Revenue"].Description = "Total revenue" # Changes are saved when context exits ``` -------------------------------- ### Show Tables within a Lakehouse Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Examples for retrieving tables within a lakehouse, with an option to include extended statistics. ```python import sempy_labs as labs import sempy_labs.lakehouse as lake lakehouse = None # Enter the name or ID of the lakehouse workspace = None # Enter the name or ID of the workspace in which the lakehouse exists # Example 1 df = lake.get_lakehouse_tables(lakehouse=lakehouse, workspace=workspace) # Example 2 df = lake.get_lakehouse_tables(lakehouse=lakehouse, workspace=workspace, extended=True) # Setting extended=True will show additional stats about tables as well as whether they are at risk of falling back to DirectQuery if used in Direct Lake mode display(df) ``` -------------------------------- ### List organizational themes Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples This code snippet shows how to list all organizational themes. ```python from sempy_labs import theme df = theme.list_org_themes() display(df) ``` -------------------------------- ### Install the latest .whl package Source: https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Semantic Model Management.ipynb Installs the semantic-link-labs package using pip. ```python %pip install semantic-link-labs ``` -------------------------------- ### Save a report as a .pbip file Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples This example demonstrates how to save a Power BI report as a .pbip file. It includes options for saving the report and its semantic model, and whether to save it as a live connection or with a local model. ```python import sempy_labs.report as rep report = '' # Name or ID of the report workspace = None # Name or ID of the workspace in which the report resides thick_report = True # If set to True, saves the report and underlying semantic model. If set to False, saves just the report. live_connect = True # If set to True, saves a .pbip live-connected to the workspace in the Power BI / Fabric service. If set to False, saves a .pbip with a local model, independent from the Power BI / Fabric service. lakehouse = None # Enter the name or ID of the lakehouse where you want to save the .pbip file lakehouse_workspace = None # Enter the name or ID of the workspace in which the lakehouse exists rep.save_report_as_pbip(report=report, workspace=workspace, thick_report=thick_report, live_connect=live_connect, lakehouse=lakehouse, lakehouse_workspace=lakehouse_workspace) ``` -------------------------------- ### Upgrade a dataflow to Gen2 CI/CD Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Example for upgrading an existing dataflow to a Gen2 format, suitable for CI/CD pipelines. ```python import sempy_labs as labs dataflow = '' # The name or ID of the dataflow to upgrade workspace = None # The name or ID of the workspace in which the dataflow resides new_dataflow_name = '' # The name of the new dataflow to be created new_dataflow_workspace = None # The workspace name or ID in which the dataflow will be created labs.upgrade_dataflow(dataflow=dataflow, workspace=workspace, new_dataflow_name=new_dataflow_name, new_dataflow_workspace=new_dataflow_workspace) ``` -------------------------------- ### Install the latest .whl package Source: https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Delta Analyzer.ipynb Installs the semantic-link-labs package using pip. ```python %pip install semantic-link-labs ``` -------------------------------- ### Check Fabric Item Recovery Enabled Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Example to check if Fabric item recovery is enabled by inspecting tenant settings. ```python import sempy_labs.admin df = sempy_labs.admin.list_tenant_settings() bool(df[df['Title'] == 'Fabric item recovery']['Enabled'].iloc[0]) ``` -------------------------------- ### Report Best Practice Analyzer Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Example of running the Report Best Practice Analyzer using `run_report_bpa`. It can optionally export results to a delta table. ```python import sempy_labs.report as rep report = '' # Name or ID of the report workspace = None # Name or ID of the workspace in which the report resides rep.run_report_bpa(report=report, workspace=workspace) rep.run_report_bpa(report=report, workspace=workspace, export=True) # Exports the results to a delta table in the lakehouse attached to the notebook ``` -------------------------------- ### Check report formats Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Lists all reports in a workspace and their formats. ```python df = sempy_labs.report.list_reports(workspace=None) display(df) ``` -------------------------------- ### Show incremental refresh policy Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples An example of how to use `show_incremental_refresh_policy` to display the incremental refresh policy for a specified table within a semantic model. ```python from sempy_labs.tom import connect_semantic_model dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides table_name = '' # Enter the name of the table with an incremental refresh policy with connect_semantic_model(dataset=dataset, workspace=workspace, readonly=True) as tom: tom.show_incremental_refresh_policy(table_name=table_name) ``` -------------------------------- ### Send an email Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Demonstrates how to send an email using the send_mail function, with and without attachments. ```python import sempy_labs as labs from sempy_labs import graph key_vault_uri = '' # Enter your key vault URI key_vault_tenant_id = '' # Enter the key vault key to the secret storing your Tenant ID key_vault_client_id = '' # Enter the key vault key to the secret storing your Client ID (Application ID) key_vault_client_secret = '' # Enter the key vault key to the secret storing your Client Secret user = '' # Enter your email address (the sender's email address) subject = '' # Enter the subject of the email content_type = "Text" to_recipients = [] # Enter the email address(es) of the 'to' recipients cc_recipients = None # Enter the email address(es) of the 'cc' recipients' content = '' # Enter the body of the email with labs.service_principal_authentication( key_vault_uri=key_vault_uri, key_vault_tenant_id=key_vault_tenant_id, key_vault_client_id=key_vault_client_id, key_vault_client_secret=key_vault_client_secret): # Example 1 graph.send_mail(user=user, subject=subject, content_type=content_type, to_recipients=to_recipients, cc_recipients=cc_recipients, content=content) # Example 2: With attachment(s) attachments = ["abfss://550e8400-e29b-41d4-a716-446655440000@onelake.dfs.fabric.microsoft.com/8a6a0e0b-59ae-4df5-8751-f0bb0ff64f38/Files/ReportTheme.json"] graph.send_mail(user=user, subject=subject, content_type=content_type, to_recipients=to_recipients, cc_recipients=cc_recipients, content=content, attachments=attachments) ``` -------------------------------- ### List the synonyms in the linguistic metadata Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Shows how to use `list_synonyms` to retrieve synonyms defined in the linguistic metadata of a semantic model. ```python import sempy_labs as labs dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides labs.list_synonyms(dataset=dataset, workspace=workspace) ``` -------------------------------- ### Set the IsAvailableInMdx property Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Provides examples for setting the IsAvailableInMdx property for columns, controlling their visibility in MDX queries. ```python from sempy_labs.tom import connect_semantic_model dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides with connect_semantic_model(dataset=dataset, workspace=workspace, readonly=False) as tom: # Example 1: For a specific column tom.model.Tables['TableName'].Columns['ColumnName'].IsAvailableInMDX = False # Example 2: For all relevant columns (according to the [Model BPA rule](https://github.com/microsoft/semantic-link-labs/blob/b1626d1dc0d041f3b19d1a1c9b38c0039f718382/src/sempy_labs/_model_bpa_rules.py#L110)) if not tom.is_direct_lake(): for c in tom.all_columns(): if c.IsAvailableInMDX and (c.IsHidden or c.Parent.IsHidden) and c.SortByColumn is None and not any(tom.used_in_sort_by(column=c)) and not any(tom.used_in_hierarchies(column=c)): c.IsAvailableInMDX = False ``` -------------------------------- ### Vacuum lakehouse table(s) Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Examples demonstrating how to vacuum lakehouse tables, including vacuuming a single table, multiple tables, or all delta tables, using `vacuum_lakehouse_tables`. ```python import sempy_labs.lakehouse as lake lakehouse = None # Enter the name or ID of the lakehouse workspace = None # Enter the name or ID of the workspace in which the lakehouse exists retain_n_hours = None # The number of hours to retain historical versions of Delta table files # Example 1: Vacuum a single table lake.vacuum_lakehouse_tables(tables='MyTable', lakehouse=lakehouse, workspace=workspace) # Example 2: Vacuum several tables lake.vacuum_lakehouse_tables(tables=['MyTable', 'MySecondTable'], lakehouse=lakehouse, workspace=workspace) # Example 3: Vacuum all delta tables within the lakehouse lake.vacuum_lakehouse_tables(tables=None, lakehouse=lakehouse, workspace=workspace) ``` -------------------------------- ### Suspend a Fabric capacity Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples This code example demonstrates how to suspend a Fabric capacity using service principal authentication. ```python import sempy_labs as labs key_vault_uri = '' # Enter your key vault URI key_vault_tenant_id = '' # Enter the key vault key to the secret storing your Tenant ID key_vault_client_id = '' # Enter the key vault key to the secret storing your Client ID (Application ID) key_vault_client_secret = '' # Enter the key vault key to the secret storing your Client Secret capacity_name = '' # Enter the name of the Fabric capacity azure_subscription_id = '' # Enter the Azure Subscription ID resource_group = '' # Enter the resource group name with labs.service_principal_authentication( key_vault_uri=key_vault_uri, key_vault_tenant_id=key_vault_tenant_id, key_vault_client_id=key_vault_client_id, key_vault_client_secret=key_vault_client_secret): labs.suspend_fabric_capacity(capacity_name=capacity_name, azure_subscription_id=azure_subscription_id, resource_group=resource_group) ``` -------------------------------- ### Scan Workspaces Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Examples demonstrating how to scan workspaces using the Scanner API, including scanning the current workspace, a specified workspace, or a list of workspaces. ```python from sempy_labs import admin data_source_details = False dataset_schema = False dataset_expressions = False lineage = False artifact_users = False # Example 1 (current workspace) x = admin.scan_workspaces(workspace=None, data_source_details=data_source_details, dataset_schema=dataset_schema, dataset_expressions=dataset_expressions, lineage=lineage, artifact_users=artifact_users) # Scans the current workspace x # Example 2 (one specified workspace) df = admin.scan_workspaces(workspace="Workspace1", data_source_details=data_source_details, dataset_schema=dataset_schema, dataset_expressions=dataset_expressions, lineage=lineage, artifact_users=artifact_users) # Scans workspace "Workspace1" display(df) # Example 3 (list of workspaces) df = admin.scan_workspaces(workspace=["Workspace1", "Workspace2"], data_source_details=data_source_details, dataset_schema=dataset_schema, dataset_expressions=dataset_expressions, lineage=lineage, artifact_users=artifact_users) # Scans workspace "Workspace1" and "Workspace2" display(df) ``` -------------------------------- ### Identify if a semantic model is a default semantic model Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Provides an example of using `is_default_semantic_model` to check if a given semantic model is the default one in a workspace. ```python import sempy_labs as labs dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides labs.is_default_semantic_model(dataset=dataset, workspace=workspace) ``` -------------------------------- ### Format Row Level Security Expressions Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples This example demonstrates how to format all row-level security expressions within a semantic model. ```python from sempy_labs.connectors import connect_semantic_model with connect_semantic_model(dataset=dataset, workspace=workspace, readonly=False) as tom: for tp in tom.all_rls(): tom.format_dax(object=tp) ``` -------------------------------- ### Optimize lakehouse table(s) Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Examples for optimizing lakehouse tables, including optimizing a single table, multiple tables, or all delta tables within a lakehouse using `optimize_lakehouse_tables`. ```python import sempy_labs.lakehouse as lake lakehouse = None # Enter the name or ID of the lakehouse workspace = None # Enter the name or ID of the workspace in which the lakehouse exists # Example 1: Optimize a single table lake.optimize_lakehouse_tables(tables='MyTable', lakehouse=lakehouse, workspace=workspace) # Example 2: Optimize several tables lake.optimize_lakehouse_tables(tables=['MyTable', 'MySecondTable'], lakehouse=lakehouse, workspace=workspace) # Example 3: Optimize all delta tables within the lakehouse lake.optimize_lakehouse_tables(tables=None, lakehouse=lakehouse, workspace=workspace) ``` -------------------------------- ### Auto-generate measure descriptions Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Demonstrates how to automatically generate descriptions for measures within a semantic model. ```python from sempy_labs.tom import connect_semantic_model dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides readonly = True # Set this to False to overwrite existing descriptions with connect_semantic_model(dataset=dataset, workspace=workspace, readonly=readonly) as tom: x = tom.generate_measure_descriptions() x ``` -------------------------------- ### Set an expression (a.k.a. parameter) Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Illustrates how to set an expression for a parameter within the semantic model. ```python from sempy_labs.tom import connect_semantic_model dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides parameter_name = '' # Enter the parameter name m_expression = '' # Enter the m expression here with connect_semantic_model(dataset=dataset, workspace=workspace, readonly=False) as tom: tom.model.Expressions[parameter_name].Expression = m_expression ``` -------------------------------- ### Migrate report-level measures to the semantic model Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples This example demonstrates how to migrate report-level measures to the semantic model using `migrate_report_level_measures`. It shows how to migrate all measures, a single measure, or multiple measures. ```python from sempy_labs.report import connect_report report = '' # Name or ID of the report workspace = None # Name or ID of the workspace in which the report resides with connect_report(report=report, workspace=workspace, readonly=False, show_diffs=True) as rpt: # Example 1: migrate all report-level measures rpt.migrate_report_level_measures() # Example 2: migrate a single report-level measure rpt.migrate_report_level_measures(measures='Sales') # Example 3: migrate several report-level measures rpt.migrate_report_level_measures(measures=['Sales', 'Profit']) ``` -------------------------------- ### Using _base_api Helper Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md The _base_api function is the standard way to make REST API calls. ```python from sempy_labs._helper_functions import _base_api # Simple GET request - MUST call .json() to get dict response = _base_api( request="/v1/workspaces/{workspace_id}/items/{item_id}", client="fabric_sp", ).json() # <-- Don't forget .json() ``` -------------------------------- ### Deploy a semantic model to a different location Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Shows how to deploy a semantic model to a different workspace or dataset, with options for refreshing and overwriting. ```python import sempy_labs as labs source_dataset = # Enter the name of the source semantic model source_workspace = None # Enter the name or ID of the workspace in which the source semantic model resides target_dataset = # Enter the name of the target semantic model target_workspace = None # Enter the name or ID of the workspace in which the target semantic model will reside refresh_target_dataset = False # If set to True, refreshes the deployed semantic model overwrite = False # If set to True, overwrites the existing target semantic model if it already exists # Example 1 (standard) labs.deploy_semantic_model(source_dataset=source_dataset, source_workspace=source_workspace, target_dataset=target_dataset, target_workspace=target_workspace, refresh_target_dataset=refresh_target_dataset, overwrite=overwrite) # Example 2 (advanced) perspective = None # To deploy a 'partial' model akin to the [master model](https://www.elegantbi.com/post/mastermodel) technique, enter the name of a perspective in your model. Only the objects within the perspective will be deployed to the target destination (including object dependencies). labs.deploy_semantic_model(source_dataset=source_dataset, source_workspace=source_workspace, target_dataset=target_dataset, target_workspace=target_workspace, refresh_target_dataset=refresh_target_dataset, overwrite=overwrite, perspective=perspective) ``` -------------------------------- ### Set translations Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Demonstrates how to set translations for table names, descriptions, measure names, and column names in different languages. ```python from sempy_labs.tom import connect_semantic_model dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides with connect_semantic_model(dataset=dataset, workspace=workspace, readonly=False) as tom: # Example 1: For a table t = tom.model.Tables['Sales'] tom.set_translation(object=t, language='fr-FR', property="Name", value='Ventes') #tom.set_translation(object=t, language='fr-FR', property="Description", value='C'est le tableau des ventes') # Example 2: For a measure m = tom.model.Tables['Sales'].Measures['Sales Amount'] tom.set_translation(object=m, language='fr-FR', property="Name", value='Montant des ventes') #tom.set_translation(object=m, language='fr-FR', property="Description", value='La somme des ventes') #tom.set_translation(object=m, language='fr-FR', property="Display Folder", value='...') # Example 3: For a column c = tom.model.Tables['Sales'].Columns['SalesAmount'] tom.set_translation(object=c, language='fr-FR', property="Name", value='Montant des ventes') #tom.set_translation(object=c, language='fr-FR', property="Description", value='...') #tom.set_translation(object=c, language='fr-FR', property="Display Folder", value='...') ``` -------------------------------- ### Convert a Direct Lake model to import mode Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Converts an entire Direct Lake semantic model to import mode. ```python from sempy_labs import migration dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides migration.migrate_direct_lake_to_import(dataset=dataset, workspace=workspace) ``` -------------------------------- ### Add a file to a report definition Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Adds a new file, such as a page definition, to a report. ```python from sempy_labs.report import connect_report report = '' # Name or ID of the report workspace = None # Name or ID of the workspace in which the report resides file = 'definition/pages/a4ifjsPek/page.json' # Enter the file path for the new file payload = {} # Enter the definition of the file with connect_report(report=report, workspace=workspace, readonly=False, show_diffs=True) as rpt: rpt.add(file_path='definition/pages/a4ifjsPek/page.json', payload=payload) ``` -------------------------------- ### List variable libraries Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples This code snippet illustrates how to list all available variable libraries within a specified workspace. ```python from sempy_labs import variable_library workspace = None # Enter the workspace name or ID df = variable_library.list_variable_libraries(workspace=workspace) display(df) ``` -------------------------------- ### Restore a semantic model Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Demonstrates how to restore a semantic model from a backup file, with options for password protection and overwriting. ```python import sempy_labs as labs dataset_name = '' # Enter the name of the semantic model to be restored workspace = None # Enter the name or ID of the workspace in which the semantic model will be restored backup_name = '' # Enter the name or path of the backup file # Optional key_vault_uri= '' # Enter the Key Vault URI which contains the secret for the password for the backup key_vault_password='' # Enter the secret name which contains the password for the backup backup_password = notebookutils.credentials.getSecret(key_vault_uri,key_vault_password) # Example 1: No password labs.restore_semantic_model( dataset=dataset_name, file_path=f"{backup_name}.abf", allow_overwrite=True, # If True, overwrites backup files of the same name. If False, the file you are saving cannot have the same name as a file that already exists in the same location. ignore_incompatibilities=True, # If True, ignores incompatibilities between Azure Analysis Services and Power BI Premium. workspace=workspace, force_restore=True, # If True, restores the semantic model with the existing semantic model unloaded and offline. ) # Example 2: With password labs.restore_semantic_model( dataset=dataset_name, file_path=f"{backup_name}.abf", password=backup_password, allow_overwrite=True, # If True, overwrites backup files of the same name. If False, the file you are saving cannot have the same name as a file that already exists in the same location. ignore_incompatibilities=True, # If True, ignores incompatibilities between Azure Analysis Services and Power BI Premium. workspace=workspace, force_restore=True, # If True, restores the semantic model with the existing semantic model unloaded and offline. ) ``` -------------------------------- ### Get dataflow definition Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples This snippet shows how to retrieve the definition of a specific dataflow. ```python import sempy_labs as labs dataflow = '' # Enter the name or ID of the dataflow workspace = None # Enter the name or ID of the workspace x = labs.get_dataflow_definition(dataflow=dataflow, workspace=workspace, decode=True) x ``` -------------------------------- ### Upgrade multiple reports Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Upgrades multiple specified reports to the PBIR format. ```python df = sempy_labs.report.upgrade_to_pbir(report=['Report1', 'Report2'], workspace=None) display(df) ``` -------------------------------- ### Create a Field Parameter Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Shows how to create a field parameter, which allows users to dynamically change the measures or dimensions used in visuals. ```python from sempy_labs.tom import connect_semantic_model dataset = '' # Enter the name or ID of your semantic model workspace = None # Enter the name or ID of the workspace in which the semantic model resides table_name = '' # Enter the name of the Field Parameter to be created # Example 1 with connect_semantic_model(dataset=dataset, readonly=False, workspace=workspace) as tom: tom.add_field_parameter(table_name=table_name, objects=["'Product'[Color]", "[Sales Amount]", "'Geography'[Country]"]) # Example 2 (with simulated hierarchies) with connect_semantic_model(dataset=dataset, readonly=False, workspace=workspace) as tom: tom.add_field_parameter(table_name=table_name, objects=["'Product'[Category]", "'Product'[SubCategory]", "'Geography'[Continent]", "'Geography'[Country]"], hierarchy_names=["Product Hierarchy", "Product Hierarchy", "Geography Hierarchy", "Geography Hierarchy"]) ``` -------------------------------- ### Helper Functions - Workspace/Item Resolution Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Import statements for workspace and item resolution helper functions. ```python from sempy_labs._helper_functions import ( resolve_workspace_id, resolve_workspace_name_and_id, resolve_dataset_id, resolve_dataset_name_and_id, resolve_item_id, resolve_item_name, ) ``` -------------------------------- ### Create the Power Query Template file Source: https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Migration to Direct Lake.ipynb Generates a Power Query Template (.pqt) file that encapsulates the semantic model's Power Query logic. ```python migration.create_pqt_file(dataset = dataset_name, workspace = workspace_name) ``` -------------------------------- ### Service Principal Authentication Context Manager Source: https://github.com/microsoft/semantic-link-labs/wiki/Frequently-Asked-Questions-(FAQ) Example of using the service_principal_authentication context manager for functions that support it. ```python from sempy_labs import service_principal_authentication with service_principal_authentication(): # Call functions that use APIs supporting service principal authentication here ``` -------------------------------- ### Get files/json within the report Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Retrieves files or specific JSON elements from a report definition. ```python from sempy_labs.report import connect_report report = '' # Name or ID of the report workspace = None # Name or ID of the workspace in which the report resides # Example 1: Get the definition of a file with connect_report(report=report, workspace=workspace, readonly=True, show_diffs=True) as rpt: file = rpt.get(file_path='definition/report.json') file # Example 2: Use the json_path parameter to extract a specific element from the definition file with connect_report(report=report, workspace=workspace, readonly=True, show_diffs=True) as rpt: active_page = rpt.get(file_path='definition/pages/pages.json', json_path="$.activePageName") active_page ``` -------------------------------- ### Resume a Fabric capacity Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples This code snippet demonstrates how to resume a Fabric capacity using service principal authentication. ```python import sempy_labs as labs key_vault_uri = '' # Enter your key vault URI key_vault_tenant_id = '' # Enter the key vault key to the secret storing your Tenant ID key_vault_client_id = '' # Enter the key vault key to the secret storing your Client ID (Application ID) key_vault_client_secret = '' # Enter the key vault key to the secret storing your Client Secret capacity_name = '' # Enter the name of the Fabric capacity azure_subscription_id = '' # Enter the Azure Subscription ID resource_group = '' # Enter the resource group name with labs.service_principal_authentication( key_vault_uri=key_vault_uri, key_vault_tenant_id=key_vault_tenant_id, key_vault_client_id=key_vault_client_id, key_vault_client_secret=key_vault_client_secret): labs.resume_fabric_capacity(capacity_name=capacity_name, azure_subscription_id=azure_subscription_id, resource_group=resource_group) ``` -------------------------------- ### Set page visibility Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Sets the visibility of a page in a report. Requires the report to be in PBIR format. ```python from sempy_labs.report import connect_report report = '' # Name or ID of the report workspace = None # Name or ID of the workspace in which the report resides with connect_report(report=report, workspace=workspace, readonly=False, show_diffs=True) as rpt: rpt.set_page_visibility(page_name='TooltipPage', hidden=True) ``` -------------------------------- ### Set a new order for the pages/tabs of a report Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Reorders the pages or tabs within a report. ```python from sempy_labs.report import connect_report report = '' # Name or ID of the report workspace = None # Name or ID of the workspace in which the report resides with connect_report(report=report, workspace=workspace, readonly=False) as rpt: rpt.set_page_order(order=['Page 2', 'Page 1', 'Page 3']) ``` -------------------------------- ### Long-running operation Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Example of a long-running operation that returns a dictionary directly upon completion. ```python result = _base_api( request=url, method="post", lro_return_json=True, # Returns JSON when LRO completes client="fabric_sp", ) ``` -------------------------------- ### Helper Functions - DataFrame Utilities Source: https://github.com/microsoft/semantic-link-labs/blob/main/AGENTS.md Import statements for DataFrame utility helper functions. ```python from sempy_labs._helper_functions import ( _create_dataframe, # Create empty DataFrame with typed columns _update_dataframe_datatypes, # Update column types ) ``` -------------------------------- ### Refresh SQL Endpoint Metadata Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Shows how to refresh the metadata for a SQL endpoint using the refresh_sql_endpoint_metadata function. ```python import sempy_labs as labs item = 'MyLake' # Enter the name or ID of the Fabric item type = 'Lakehouse' # Enter the item type workspace = None # Enter the name or ID of the workspace x = labs.refresh_sql_endpoint_metadata(item=item, type=type, workspace=workspace) display(x) ``` -------------------------------- ### Set the active page Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Sets the active page of a report. Requires the report to be in PBIR format. ```python from sempy_labs.report import connect_report report = '' # Name or ID of the report workspace = None # Name or ID of the workspace in which the report resides with connect_report(report=report, workspace=workspace, readonly=False, show_diffs=True) as rpt: rpt.set_active_page(page_name='MainPage') ``` -------------------------------- ### Upgrade all eligible reports in multiple workspaces Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Upgrades all eligible reports across multiple specified workspaces to the PBIR format. ```python df = sempy_labs.report.upgrade_to_pbir(report=None, workspace=['WorkspaceA', 'WorkspaceB']) display(df) ``` -------------------------------- ### Upgrade all eligible reports in a workspace Source: https://github.com/microsoft/semantic-link-labs/wiki/Code-Examples Upgrades all eligible reports within a specified workspace to the PBIR format. ```python df = sempy_labs.report.upgrade_to_pbir(report=None, workspace=None) display(df) ```