Skip to content

Commit

Permalink
Updated Scripts and Macros (#75)
Browse files Browse the repository at this point in the history
* Updated note

* Adjusted scripts in Macroactions to fit script library

* Added scripts from MacroActions

* Updated note on SymLink

* Updated table groups script

* Udpated MacroFile to fit script library

* Name change for script files

* Updates table groups script

* Fixed uncomplete sentence

* Fix script remove measures with outdatet method
  • Loading branch information
mlonsk authored Dec 7, 2023
1 parent 76e37c9 commit 974f35e
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 33 deletions.
76 changes: 58 additions & 18 deletions assets/file-types/MacroActions.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ If you want to see all the measures that have errors and have the option to dele
```csharp
// This script scans the model and shows all measures with errors, giving the option to remove them.
//
// .GetSemantics(...) method is only available in TE3
// .GetCachedSemantics(...) method is only available in TE3
using System.Windows.Forms;

// Hide the 'Running Macro' spinbox
ScriptHelper.WaitFormVisible = false;

// Get all the measures that have errors
var measuresWithError = Model.AllMeasures.Where(m => m.GetSemantics(ExpressionProperty.Expression).HasError).ToList();
var measuresWithError = Model.AllMeasures.Where(m => m.GetCachedSemantics(ExpressionProperty.Expression).HasError).ToList();
//Prior to Tabular Editor 3.12.0 the GetSemantics method must be used.
//var measuresWithError = Model.AllMeasures.Where(m => m.GetSemantics(ExpressionProperty.Expression).HasError).ToList();
// If no measures with errors, end script with error.
if ( measuresWithError.Count == 0 )
Expand Down Expand Up @@ -95,6 +97,7 @@ var _ToDelete = SelectObjects(measuresWithError, measuresWithError, "Select meas
Info ( "No measure selected." );
}
}

```
### Explanation
This snippet gets all the measures that have errors according to the Tabular Editor Semantic Analysis. It then will display them in an output box where you can manually browse them or make changes. Thereafter, measures can be selected for removal. The removed measures can be saved as a back-up .tsv file in case you want to import them, later.
Expand Down
2 changes: 1 addition & 1 deletion common/CSharpScripts/Beginner/script-count-things.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Info ( "In the model, we see the below objects:\n\n"
```
### Explanation
This snippet goes through the model and counts the different object types, displaying them in a hierarchical "node and tree" format that is manually constructed.
You can comment out
You can comment out the parts that you do not need for your purposes.

## Example Output

Expand Down
24 changes: 24 additions & 0 deletions common/CSharpScripts/Beginner/script-create-measure-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
uid: script-create-measure-table
title: Create Measure Table
author: Morten Lønskov
updated: 2023-11-29
applies_to:
versions:
- version: 2.x
- version: 3.x
---
# Create Measure Table

## Script Purpose
The scripts creates a hidden measure table containing one hidden column


## Script

### Create Measure Table
```csharp
// Create a calculated table with a single column which is hidden:
var table = Model.AddCalculatedTable("Model Measures", "{0}");
table.Columns[0].IsHidden = true;
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
uid: create-sum-measures-from-columns
uid: script-create-sum-measures-from-columns
title: Create SUM Measure from Column
author: Morten Lønskov
updated: 2023-02-22
Expand Down
55 changes: 55 additions & 0 deletions common/CSharpScripts/Beginner/script-create-table-groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
uid: script-create-table-groups
title: Create Table Groups
author: Morten Lønskov
updated: 2023-11-29
applies_to:
versions:
- version: 3.x
---
# Create Table Groups

## Script Purpose
This script creates default table groups within Tabular Editor 3.

## Script

### Script Title
```csharp
// Loop through all tables:
foreach(var table in Model.Tables)
{
if (table is CalculationGroupTable)
{
table.TableGroup = "Calculation Groups";
}
else if (!table.UsedInRelationships.Any() && table.Measures.Any(m => m.IsVisible))
{
// Tables containing visible measures, but no relationships to other tables
table.TableGroup = "Measure Groups";
}
else if (table.UsedInRelationships.All(r => r.FromTable == table) && table.UsedInRelationships.Any())
{
// Tables exclusively on the "many" side of relationships:
table.TableGroup = "Facts";
}
else if (!table.UsedInRelationships.Any() && table is CalculatedTable && !table.Measures.Any())
{
// Tables without any relationships, that are Calculated Tables and do not have measures:
table.TableGroup = "Parameter Tables";
}
else if (table.UsedInRelationships.Any(r => r.ToTable == table))
{
// Tables on the "one" side of relationships:
table.TableGroup = "Dimensions";
}
else
{
// All other tables:
table.TableGroup = "Misc";
}
}
```
### Explanation
The scripts loops through all tables in the model assigning a table group according to specific properties.

44 changes: 44 additions & 0 deletions common/CSharpScripts/Beginner/script-format-numeric-measures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
uid: script-format-numeric-measures
title: Format Numeric Measures
author: Morten Lønskov
updated: 2023-11-29
applies_to:
versions:
- version: 2.x
- version: 3.x
---
# Format Numeric Measures

## Script Purpose
Allows you to quickly set default format strings on the measures selected.

<br></br>
> [!NOTE]
> The script uses certain naming standards so you might wish to adjust it to suit yours.
<br></br>
## Script

### Script Title
```csharp
// This script is meant to format all measures with a default formatstring
foreach (var ms in Selected.Measures) {
//Don't set format string on hidden measures
if (ms.IsHidden) continue;
// If the format string is empty continue.
if (!string.IsNullOrWhiteSpace(ms.FormatString)) continue;
//If the data type is int set a whole number format string
if (ms.DataType == DataType.Int64) ms.FormatString = "#,##0";
//If the datatype is double or decimal
if (ms.DataType == DataType.Double || ms.DataType == DataType.Decimal) {
//and the name contains # or QTY then set the format string to a whole number
if (ms.Name.Contains("#")
|| ms.Name.IndexOf("QTY", StringComparison.OrdinalIgnoreCase) >= 0) ms.FormatString = "#,##0";
//otherwise set it a decimal format string.
else ms.FormatString = "#,##0.00";
}
}
```
### Explanation
The script takes each of the selected measures and loops through them to set a default format string according to various conditions.
10 changes: 7 additions & 3 deletions common/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@

### @script-library-beginner
#### @script-count-rows
#### @script-count-things
#### @script-edit-hidden-partitions
#### @script-create-m-parameter
#### @create-sum-measures-from-columns
#### @script-find-replace
#### @script-create-sum-measures-from-columns
#### @script-create-measure-table
#### @script-format-numeric-measures
#### @script-create-table-groups


### @script-library-advanced
#### @script-count-things
#### @script-create-date-table
#### @script-create-and-replace-parameter
#### @script-format-power-query
#### @script-implement-incremental-refresh
#### @script-remove-measures-with-error
#### @script-find-replace

## @script-helper-methods
1 change: 1 addition & 0 deletions te3/features/supported-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ It can be helpful to share these files across a team so that all developers have
> A windows native way of syncing a version controlled file into the "%localappdata%\TabularEditor3" folder is to use [SymLink](https://www.howtogeek.com/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/).
>
> Store the required files in Git or OneDrive and create a Symlink to the "%localappdata%\TabularEditor3" folder, but be aware that this could end up with synchronization issues, if multiple users update the same file version.
> However, this is not supported by Tabular Editor directly, so implement it at your own discretion.

### MacroActions.json
Expand Down
16 changes: 8 additions & 8 deletions te3/features/table-groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ foreach(var table in Model.Tables)
{
table.TableGroup = "Calculation Groups";
}
else if (!table.UsedInRelationships.Any())
else if (!table.UsedInRelationships.Any() && table.Measures.Any(m => m.IsVisible))
{
// Tables without any relationships:
table.TableGroup = "Parameter Tables";
}
else if (table.IsHidden && table.Measures.Any(m => m.IsVisible))
{
// Hidden tables containing visible measures:
// Tables containing visible measures, but no relationships to other tables
table.TableGroup = "Measure Groups";
}
else if (table.UsedInRelationships.All(r => r.FromTable == table))
else if (table.UsedInRelationships.All(r => r.FromTable == table) && table.UsedInRelationships.Any())
{
// Tables exclusively on the "many" side of relationships:
table.TableGroup = "Facts";
}
else if (!table.UsedInRelationships.Any() && table is CalculatedTable && !table.Measures.Any())
{
// Tables without any relationships, that are Calculated Tables and do not have measures:
table.TableGroup = "Parameter Tables";
}
else if (table.UsedInRelationships.Any(r => r.ToTable == table))
{
// Tables on the "one" side of relationships:
Expand Down

0 comments on commit 974f35e

Please sign in to comment.