-
Notifications
You must be signed in to change notification settings - Fork 190
[pbi-fixer] Add add_measures_from_columns: auto-create explicit measures from numeric columns based on SummarizeBy #1216
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
Open
KornAlexander
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
KornAlexander:feature/add-measures-from-columns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
src/sempy_labs/semantic_model/_Add_MeasuresFromColumns.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # Auto-create measures from columns based on SummarizeBy property. | ||
| # For each column where SummarizeBy != "None", creates a measure using | ||
| # the appropriate aggregation function and hides the source column. | ||
| # Ported from Tabular Editor macro: "Selected Measures based on Summarize By Property" | ||
|
|
||
| from typing import Optional | ||
| from uuid import UUID | ||
| from sempy._utils._log import log | ||
| import sempy_labs._icons as icons | ||
|
|
||
|
|
||
| @log | ||
| def add_measures_from_columns( | ||
| dataset: str | UUID, | ||
| workspace: Optional[str | UUID] = None, | ||
| target_table: Optional[str] = None, | ||
| scan_only: bool = False, | ||
| ): | ||
| """ | ||
| Creates measures from columns based on their SummarizeBy property. | ||
|
|
||
| For each column where SummarizeBy is not "None", a measure is created | ||
| using the appropriate aggregation (SUM, COUNT, MIN, MAX, etc.). | ||
| The source column is hidden after measure creation. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| dataset : str | uuid.UUID | ||
| Name or ID of the semantic model. | ||
| workspace : str | uuid.UUID, default=None | ||
| The Fabric workspace name or ID. | ||
| target_table : str, default=None | ||
| Table to place new measures in. If None, measures are added to | ||
| the same table as the source column. | ||
| scan_only : bool, default=False | ||
| If True, only reports what would be created without making changes. | ||
|
|
||
| Returns | ||
| ------- | ||
| int | ||
| Number of measures created (or that would be created in scan mode). | ||
| """ | ||
| from sempy_labs.tom import connect_semantic_model | ||
|
|
||
| created = 0 | ||
|
|
||
| with connect_semantic_model( | ||
| dataset=dataset, readonly=scan_only, workspace=workspace | ||
| ) as tom: | ||
| # Resolve target table if specified | ||
| measures_table = None | ||
| if target_table: | ||
| measures_table = tom.model.Tables.Find(target_table) | ||
| if measures_table is None: | ||
| print(f"{icons.red_dot} Target table '{target_table}' not found.") | ||
| return 0 | ||
| else: | ||
| # Auto-detect measure table by name | ||
| for t in tom.model.Tables: | ||
| if "measure" in t.Name.lower(): | ||
| measures_table = t | ||
| print(f"{icons.info} Auto-detected measure table: '{t.Name}'") | ||
| break | ||
|
|
||
| for table in tom.model.Tables: | ||
| for col in table.Columns: | ||
| summarize_by = str(col.SummarizeBy) if hasattr(col, "SummarizeBy") else "None" | ||
| if summarize_by == "None" or summarize_by == "Default": | ||
| continue | ||
|
|
||
| agg_fn = summarize_by.upper() | ||
| measure_name = col.Name | ||
| dax_expr = f"{agg_fn}('{table.Name}'[{col.Name}])" | ||
|
KornAlexander marked this conversation as resolved.
|
||
| dest_table = measures_table or table | ||
|
|
||
| # Check if measure already exists | ||
| existing = dest_table.Measures.Find(measure_name) | ||
| if existing is not None: | ||
|
KornAlexander marked this conversation as resolved.
|
||
| continue | ||
|
|
||
| if scan_only: | ||
| print( | ||
| f"{icons.yellow_dot} Would create: [{measure_name}] = {dax_expr} " | ||
| f"in '{dest_table.Name}'" | ||
| ) | ||
| created += 1 | ||
| continue | ||
|
|
||
| tom.add_measure( | ||
| table_name=dest_table.Name, | ||
| measure_name=measure_name, | ||
| expression=dax_expr, | ||
| format_string="0.0", | ||
| description=( | ||
| f"Auto-created {agg_fn} measure from column " | ||
| f"'{table.Name}'[{col.Name}]" | ||
| ), | ||
| display_folder=table.Name, | ||
| ) | ||
| col.IsHidden = True | ||
| created += 1 | ||
| print( | ||
| f"{icons.green_dot} Created [{measure_name}] = {dax_expr} " | ||
| f"in '{dest_table.Name}'" | ||
| ) | ||
|
|
||
| if not scan_only and created > 0: | ||
| tom.model.SaveChanges() | ||
|
|
||
|
KornAlexander marked this conversation as resolved.
Outdated
|
||
| action = "Would create" if scan_only else "Created" | ||
| print(f"{icons.info} {action} {created} measure(s) from columns.") | ||
| return created | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.