Skip to content

Commit

Permalink
Add "Output object details in grid" script (#133)
Browse files Browse the repository at this point in the history
cleaned up links in Script library. Added "Output object details in grid" script
  • Loading branch information
otykier authored Dec 13, 2024
1 parent 9c50c9c commit 0c9a243
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 21 deletions.
64 changes: 64 additions & 0 deletions common/CSharpScripts/Advanced/script-output-things.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
uid: script-output-things
title: Output Object Details in a Grid
author: Daniel Otykier
updated: 2024-12-13
applies_to:
versions:
- version: 2.x
- version: 3.x
---
# Output Object Details in a Grid

## Script Purpose
Another way to get an overview of objects in the model, and how they are configured, is to output them in a grid using the C# [`DataTable`](https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable?view=net-8.0) class. This is a very flexible technique, as you can add only the information you are interested in, as columns of the `DataTable`. Moreover, when passing a `DataTable` to the `Output()` method, Tabular Editor will automatically display it in a grid view, which is very convenient for inspecting the data.

## Script

### Show measure complexity details
```csharp
// This script displays a grid with details about each measure in the model.
using System.Data;

var result = new DataTable();
result.Columns.Add("Name");
result.Columns.Add("Table");
result.Columns.Add("Expression token count", typeof(int));
result.Columns.Add("Expression line count", typeof(int));
result.Columns.Add("Description line count", typeof(int));
result.Columns.Add("Format String");

foreach(var m in Model.AllMeasures)
{
var row = new object[]
{
m.DaxObjectName, // Name
m.Table.Name, // Table
m.Tokenize().Count, // Token count
m.Expression.Split(new []{'\n'}, StringSplitOptions.RemoveEmptyEntries).Length,
m.Description.Split(new []{'\n'}, StringSplitOptions.RemoveEmptyEntries).Length,
m.FormatStringExpression ?? m.FormatString
};
result.Rows.Add(row);
}

Output(result);
```
### Explanation
This snippet first configures a `DataTable` object with the columns we want to display in the grid. We explicitly specify the `typeof(int)` for some of the columns, to ensure that sorting works correctly. We then iterate over all measures in the model, and for each measure, we create a new row in the `DataTable` with the desired information. Finally, we pass the `DataTable` to the `Output()` method, which will display the grid.

The columns displayed are:

- **Name**: The name of the measure.
- **Table**: The name of the table the measure belongs to.
- **Expression token count**: The number of tokens in the measure expression. This is a rough measure of DAX complexity.
- **Expression line count**: The number of lines in the measure expression, not counting empty lines.
- **Description line count**: The number of lines in the measure description, not counting empty lines.
- **Format String**: The measure's format string expresssion or format string, if any.

## Example Output

<figure style="padding-top: 15px;">
<img class="noscale" src="~/images/script-output-things-example.png" alt="Example of the dialog pop-up that displays the grid." style="width: 550px;"/>
<figcaption style="font-size: 12px; padding-top: 10px; padding-bottom: 15px; padding-left: 75px; padding-right: 75px; color:#00766e"><strong>Figure 1:</strong> Example of the dialog pop-up that displays the grid. Both Tabular Editor 2 and Tabular Editor 3 will let you sort the grid columns as well as copy the output to the clipboard. However, Tabular Editor 3 also has additional features for grouping, filtering, and searching within the grid.</figcaption>
</figure>
13 changes: 8 additions & 5 deletions common/CSharpScripts/csharp-script-library-advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ These are more advanced scripts with sophisticated functionalities requiring a m

| <div style="width:250px">Script Name</div> | Purpose | Use-case |
| --- | --- | --- |
| [Create Date Table](Advanced/script-create-date-table.md) | Creates a formatted Date table based on selected Date columns in the model. | When you need to create a new date table based on a template. |
| [Create M Parameter (Auto-Replace)](Advanced/script-create-and-replace-M-parameter.md) | Creates a new M Parameter and automatically adds it to M Partitions. | When you want to replace strings in multiple partitions (i.e. connection strings) with a dynamic M Parameter. |
| [Format Power Query](Advanced/script-format-power-query.md) | Formats the Power Query of a selected M Partition by using the powerqueryformatter.com API. | When you have complex Power Query and need to make it more readable for reading or making changes. |
| [Implement Incremental Refresh](Advanced/script-implement-incremental-refresh.md) | Configures Incremental Refresh automatically using parameters from a UI dialogue box. | When you need to implement incremental refresh but aren't comfortable with the configuration in the table settings. |
| [Remove Measures with Errors](Advanced/script-remove-measures-with-error.md) | Creates a new M Parameter and automatically adds it to M Partitions. | When you want to replace strings in multiple partitions (i.e. connection strings) with a dynamic M Parameter. |
| [Count Model Objects](xref:script-count-things) | Counts all the different objects by type in a model. | When you need an overview of the model contents or want to count objects by type. |
| [Output Object Details in a Grid](xref:script-output-things) | Outputs object details in a grid view. | When you need to output object details in a grid view for inspection. |
| [Create Date Table](xref:script-create-date-table) | Creates a formatted Date table based on selected Date columns in the model. | When you need to create a new date table based on a template. |
| [Create M Parameter (Auto-Replace)](xref:script-create-and-replace-parameter) | Creates a new M Parameter and automatically adds it to M Partitions. | When you want to replace strings in multiple partitions (i.e. connection strings) with a dynamic M Parameter. |
| [Format Power Query](xref:script-format-power-query) | Formats the Power Query of a selected M Partition by using the powerqueryformatter.com API. | When you have complex Power Query and need to make it more readable for reading or making changes. |
| [Implement Incremental Refresh](xref:script-implement-incremental-refresh) | Configures Incremental Refresh automatically using parameters from a UI dialogue box. | When you need to implement incremental refresh but aren't comfortable with the configuration in the table settings. |
| [Remove Measures with Errors](xref:script-remove-measures-with-error) | Creates a new M Parameter and automatically adds it to M Partitions. | When you want to replace strings in multiple partitions (i.e. connection strings) with a dynamic M Parameter. |
| [Find & Replace in Selected Measures](xref:script-find-replace) | Searches for a substring in the DAX of selected measures, replacing with another substring. | When you need to quickly find/replace values in multiple DAX measures (i.e. `CALCULATE` filter or broken object references). |
23 changes: 10 additions & 13 deletions common/CSharpScripts/csharp-script-library-beginner.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ These are more basic scripts that are easy to understand or modify. They have a

| <div style="width:250px">Script Name</div> | Purpose | Use-case |
| --- | --- | --- |
| [Count Table Rows](Beginner/script-count-rows.md) | Evaluates a COUNTROWS ( 'Table' ) of a selected table. | When you want to check how many rows are in a table, or if it's been loaded. |
| [Count Model Objects](Beginner/script-count-things.md) | Counts all the different objects by type in a model. | When you need an overview of the model contents or want to count objects by type. |
| [Create Sum Measures from Columns](Beginner/script-create-sum-measures-from-columns.md) | Create SUM ( 'Table'[Column] ) measures from any selected column. | When you have many columns in a new table / model and must make many measures at once. |
| [Create M Parameter](Beginner/script-create-m-parameter.md) | Create a new M Parameter in 'Shared Expressions' | When you want to create a parameter to use in other Power Query queries (M Partitions / Shared Expressions). |
| [Edit Hidden Partitions](Beginner/script-edit-hidden-partitions.md) | Reveals the properties of hidden partitions in Calc. Groups & Calc. Tables | When you need to see or edit the TOM properties of these hidden partitions. |
| [Find & Replace in Selected Measures](Beginner/script-find-replace-selected-measures.md) | Searches for a substring in the DAX of selected measures, replacing with another substring. | When you need to quickly find/replace values in multiple DAX measures (i.e. `CALCULATE` filter or broken object references). |
| [Create Measure Table](Beginner/script-create-measure-table.md) | Create a measure table | When you want to create an empty table to use as an organizing measure table|
| [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 |


| [Count Table Rows](xref:script-count-rows) | Evaluates a COUNTROWS ( 'Table' ) of a selected table. | When you want to check how many rows are in a table, or if it's been loaded. |
| [Create Sum Measures from Columns](xref:script-create-sum-measures-from-columns) | Create SUM ( 'Table'[Column] ) measures from any selected column. | When you have many columns in a new table / model and must make many measures at once. |
| [Create Measure Table](xref:script-create-measure-table) | Create a measure table | When you want to create an empty table to use as an organizing measure table|
| [Create Table Groups](xref:script-create-table-groups) | 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 |
| [Create M Parameter](xref:script-create-m-parameter) | Create a new M Parameter in 'Shared Expressions' | When you want to create a parameter to use in other Power Query queries (M Partitions / Shared Expressions). |
| [Edit Hidden Partitions](xref:script-edit-hidden-partitions) | Reveals the properties of hidden partitions in Calc. Groups & Calc. Tables | When you need to see or edit the TOM properties of these hidden partitions. |
| [Format Numeric Measures](xref:script-format-numeric-measures) | Formats the chosen measures | When you want to quickly apply a format string to the currently selected measures |
| [Show Data Source Dependencies](xref:script-show-data-source-dependencies) | 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](xref:create-field-parameter) | 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 |
| [Display Unique Column Values](xref:script-display-unique-column-values) | Display unique values in a column | When you want to see the unique values in the currently selected column |
8 changes: 5 additions & 3 deletions common/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@

### @script-library-beginner
#### @script-count-rows
#### @script-edit-hidden-partitions
#### @script-create-m-parameter
#### @script-create-sum-measures-from-columns
#### @script-create-measure-table
#### @script-format-numeric-measures
#### @script-create-table-groups
#### @script-create-m-parameter
#### @script-edit-hidden-partitions
#### @script-format-numeric-measures
#### @script-show-data-source-dependencies
#### @create-field-parameter
#### @script-display-unique-column-values


### @script-library-advanced
#### @script-count-things
#### @script-output-things
#### @script-create-date-table
#### @script-create-and-replace-parameter
#### @script-format-power-query
Expand Down
Binary file added images/script-output-things-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0c9a243

Please sign in to comment.