Skip to content

Commit

Permalink
Field Parameter Script (#85)
Browse files Browse the repository at this point in the history
* Add field parameter script

* Update whatsnew with field parameter GIAC video

* Fix typo

* Remove web authoring warning
  • Loading branch information
mlonsk authored Feb 5, 2024
1 parent 9bc93c4 commit 5498c79
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
68 changes: 68 additions & 0 deletions common/CSharpScripts/Beginner/script-create-field-parameter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
uid: create-field-parameter
title: Create Field Parameter
author: Daniel Otykier
updated: 2024-01-27
applies_to:
versions:
- version: 2.x
- version: 3.x
---
# Create Field Parameters in

## Script Purpose
If you want to create field parameters in a Power BI model using Tabular Editor or in a Direct Lake model.

> [!TIP]
> Want to see the script in action, check out this [Guy in a Cube video](https://www.youtube.com/watch?v=Cg6zRhwF-Ro) where Patrick LeBlanc explains how to use it step by step.

## Script

### Select Columns or Measures to create a field parameter table
```csharp
// Before running the script, select the measures or columns that you
// would like to use as field parameters (hold down CTRL to select multiple
// objects). Also, you may change the name of the field parameter table
// below. NOTE: If used against Power BI Desktop, you must enable unsupported
// features under File > Preferences (TE2) or Tools > Preferences (TE3).
var name = "Parameter";

if(Selected.Columns.Count == 0 && Selected.Measures.Count == 0) throw new Exception("No columns or measures selected!");

// Construct the DAX for the calculated table based on the current selection:
var objects = Selected.Columns.Any() ? Selected.Columns.Cast<ITabularTableObject>() : Selected.Measures;
var dax = "{\n " + string.Join(",\n ", objects.Select((c,i) => string.Format("(\"{0}\", NAMEOF('{1}'[{0}]), {2})", c.Name, c.Table.Name, i))) + "\n}";

// Add the calculated table to the model:
var table = Model.AddCalculatedTable(name, dax);

// In TE2 columns are not created automatically from a DAX expression, so
// we will have to add them manually:
var te2 = table.Columns.Count == 0;
var nameColumn = te2 ? table.AddCalculatedTableColumn(name, "[Value1]") : table.Columns["Value1"] as CalculatedTableColumn;
var fieldColumn = te2 ? table.AddCalculatedTableColumn(name + " Fields", "[Value2]") : table.Columns["Value2"] as CalculatedTableColumn;
var orderColumn = te2 ? table.AddCalculatedTableColumn(name + " Order", "[Value3]") : table.Columns["Value3"] as CalculatedTableColumn;

if(!te2) {
// Rename the columns that were added automatically in TE3:
nameColumn.IsNameInferred = false;
nameColumn.Name = name;
fieldColumn.IsNameInferred = false;
fieldColumn.Name = name + " Fields";
orderColumn.IsNameInferred = false;
orderColumn.Name = name + " Order";
}
// Set remaining properties for field parameters to work
// See: https://twitter.com/markbdi/status/1526558841172893696
nameColumn.SortByColumn = orderColumn;
nameColumn.GroupByColumns.Add(fieldColumn);
fieldColumn.SortByColumn = orderColumn;
fieldColumn.SetExtendedProperty("ParameterMetadata", "{\"version\":3,\"kind\":2}", ExtendedPropertyType.Json);
fieldColumn.IsHidden = true;
orderColumn.IsHidden = true;
```
### Explanation
Before running the script the user has to select the measures or columns in the TOM Explorer they wish to have in their field parameter table.
The selected objects are then inserted into a calculated table which is then configured as a field parameter table automatically.

1 change: 1 addition & 0 deletions common/CSharpScripts/csharp-script-library-beginner.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ These are more basic scripts that are easy to understand or modify. They have a
| [Create Table Groups](Beginner/script-create-table-groups.md) | Organize the model into Table Groups | When you want to have an automatic organization of your tables using the table group feature of Tabular Editor 3 |
| [Format Numeric Measures](Beginner/script-format-numeric-measures.md) | Formats the chosen measures | When you want to quickly apply a format string to the currently selected measures |
| [Show Data Source Dependencies](Beginner/script-show-data-source-dependencies.md) | Shows dependencies for data sources | For explicit (legacy) data sources it can be hard to know exactly where they are used. This script shows you which partition reference the chosen data source |
| [Create Field Parameters](Beginner/script-create-field-parameter.md) | Quickly create a field parameter table | Choose the objects that should be in the field parameter and the script will take care of the rest |


3 changes: 0 additions & 3 deletions common/Datasets/direct-lake-dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ applies_to:
# Direct Lake Semantic Models
Direct Lake semantic models connect directly to data sources stored in [Fabric One Lake](https://learn.microsoft.com/en-us/fabric/onelake/onelake-overview).

> [!IMPORTANT]
> Changing a Direct Lake dataset through the XMLA endpoint will block your ability to change the Direct Lake dataset inside the Fabric Service. The Direct Lake model can then only be opened and edited through the XMLA endpoint. This is one of the current limitations of this preview feature.
Tabular Editor 3 can create and connect to this type of dataset. For a tutorial on this please refer to our blog article: [Direct Lake semantic models: How to use them with Tabular Editor](https://blog.tabulareditor.com/2023/09/26/fabric-direct-lake-with-tabular-editor-part-2-creation/).
Tabular Editor 3 can create direct lake semantic models with both the Lakehouse and Datawarehouse SQL Endpoint.

Expand Down
1 change: 1 addition & 0 deletions common/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#### @script-create-measure-table
#### @script-format-numeric-measures
#### @script-create-table-groups
#### @create-field-parameter


### @script-library-advanced
Expand Down
2 changes: 1 addition & 1 deletion te3/powerbi-xmla-pbix-workaround.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
uid: powerbi-xmla-pbix-workaround
title: Creating PBIX File from XMLA Endoint.
title: Creating PBIX File from XMLA Endpoint.
author: Morten Lønskov
updated: 2023-10-18
applies_to:
Expand Down
9 changes: 7 additions & 2 deletions whats-new/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ <h2>Community</h2>
<p>Ask questions, suggest enhancements and help other TE fans get their answers at our <a href="https://github.com/TabularEditor/TabularEditor3/discussions"> GitHub community</a>:</p>
<h3>Articles</h3>
<ul>
<li><a href="https://blog.tabulareditor.com/2023/12/07/gather-requirements-for-semantic-models/"> Gather Requirements for Semantic Models</a> on the Tabular Editor blog</li>
<li><a href="https://blog.tabulareditor.com/2023/11/17/semantic-models-in-simple-terms/">Semantic models in simple terms</a> on the Tabular Editor blog</li>
<li><a href="https://blog.tabulareditor.com/2024/01/23/connect-to-and-transform-data-for-a-semantic-model/"> Connect to and transform data for a semantic model</a> on the Tabular Editor blog </li>
<li><a href="https://blog.tabulareditor.com/category/power-bi/semantic-model/"> The Semantic Models blog series</a> on the Tabular Editor blog</li>
<li><a href="https://greyskullanalytics.com/why-i-love-tabular-editor/">Why I Love Tabular Editor</a> by Greyskull Analytics aka Johnny Winter</li>
<li><a href="https://blog.tabulareditor.com/2023/09/27/ci-cd-scripts-for-tabular-editor-2s-cli/">CI/CD scripts for Tabular Editor</a> on the Tabular Editor blog</li>
<li><a href="https://www.oliviervs.be/analysis-services/sortbycolumn-property-set-to-an-invalid-column-id/">SortByColumn property set to an invalid column ID</a> by Olivier Van Steenlandt</li>
Expand All @@ -119,6 +119,11 @@ <h3>Articles</h3>
<li><a href="https://data-goblins.com/power-bi/format-power-query-automatically">Format Power Query in Power BI</a> by <a href="https://data-goblins.com/">Kurt Buhler</a></li>
</ul>
<p>Share your favorite blog or SoMe post <a href="https://github.com/TabularEditor/TabularEditor3/discussions/categories/ressources"> with the community</a></p>
<h3> Video</h3>
<ul>
<li><a href="https://www.youtube.com/watch?v=Cg6zRhwF-Ro"> Can you use Field Parameters with Direct Lake in Power BI?</a> Guy in a Cube shows how to use Tabular Editor to create field parameters in Direct Lake models</li>
<li><a href="https://www.youtube.com/watch?v=Fs1A7M3rLHE&t=60s"> Wireframe your Model with Tabular Editor</a> Morten shows you how to create a mockup of your model that you can continue to develop on</li>
</ul>
<h3>Other resources</h3>
<ul>
<li><a href="https://github.com/TabularEditor/TabularEditor3/issues">GitHub issue tracker (TE3)</a></li>
Expand Down

0 comments on commit 5498c79

Please sign in to comment.