Skip to content
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

format strings and prefix/suffix management #29

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions Basic/Add Prefix to Selected Measure Names.csx
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
#r "Microsoft.VisualBasic"

/*
* Title: Add Prefix to Selected Measures
* Title: Add prefix to selected measures
*
* Author: Mike Carlo, https://PowerBI.tips
* Author: Adam Grisdale
*
* This script loops through all the selected measures and adds a Prefix String to the name.
* Only Selected measures will be modified.
* This script prompts the user for a string, then loops through all the selected measures and adds that string as a prefix to the measures name.
* The code adds a space between the new prefix and the current measures name. Modify the code below if this does not suit your needs.
* If no string is provided, or the user clicks cancel, nothing will happen.
*
*/

/* Cycle over all Selected measures in model */
foreach (var m in Selected.Measures)
// Ask the user to provide a prefix to add to the measure name.
string NewPrefix = Microsoft.VisualBasic.Interaction.InputBox("Enter the text you wish to add to the beginning of the selected measure names.\n\nA space will automatically be added between the text entered below and the existing measure name.", "Add prefix to selected measures", "");

// Check if a string has been entered
if (!string.IsNullOrEmpty(NewPrefix))
{
// Loop through selected measures
foreach (var m in Selected.Measures)
{
/* Grab the current name as a variable, prepend some text */
m.Name = "Test " + m.Name;
}
// Grab the current name as a variable and append the prefix entered
m.Name = NewPrefix + " " + m.Name;
}
}
26 changes: 26 additions & 0 deletions Basic/Add Suffix to Selected Measure Names.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#r "Microsoft.VisualBasic"

/*
* Title: Add suffix to selected measures
*
* Author: Adam Grisdale
*
* This script prompts the user for a string, then loops through all the selected measures and adds that string as a suffix to the measures name.
* The code adds a space between the current measures name and the new suffix. Modify the code below if this does not suit your needs.
* If no string is provided, or the user clicks cancel, nothing will happen.
*
*/

// Ask the user to provide a suffix to add to the measure name.
string NewSuffix = Microsoft.VisualBasic.Interaction.InputBox("Enter the text you wish to add to the end of the selected measure names.\n\nA space will automatically be added between the existing measure name and the text entered below.", "Add suffix to selected measures", "");

// Check if a string has been entered
if (!string.IsNullOrEmpty(NewSuffix))
{
// Loop through selected measures
foreach (var m in Selected.Measures)
{
// Grab the current name as a variable and append the suffix entered
m.Name = m.Name + " " + NewSuffix;
}
}
24 changes: 24 additions & 0 deletions Basic/Format Measure as Decimal Number (1 decimal place).csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Title: Format Measure as a decimal number to 1 decimal place
*
* Author: Adam Grisdale
*
* Loops through the selected measures and formats them as a decimal number to 1 decimal place with a thousands separator.
* Designed to be used as a macro action added to the MacroActions json file: %LocalAppData%\TabularEditor\MacroActions.json
*
* {
* "Name": "Formatting\\Decimal Number\\One Decimal Place",
* "Enabled": "true",
* "Execute": "foreach ( var m in Selected.Measures)\r\n{\r\n m.FormatString = \"#,0.0\";\r\n};",
* "Tooltip": "Formats the selected measures to one decimal place.",
* "ValidContexts": "Measure"
* }
*
*/

// Loop through all selected measures
foreach ( var m in Selected.Measures)
{
// Set the measures format string
m.FormatString = "#,0.0";
};
24 changes: 24 additions & 0 deletions Basic/Format Measure as Decimal Number (2 decimal places).csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Title: Format Measure as a decimal number to 2 decimal places
*
* Author: Adam Grisdale
*
* Loops through the selected measures and formats them as a decimal number to 1 decimal places with a thousands separator.
* Designed to be used as a macro action added to the MacroActions json file: %LocalAppData%\TabularEditor\MacroActions.json
*
* {
* "Name": "Formatting\\Decimal Number\\One Decimal Place",
* "Enabled": "true",
* "Execute": "foreach ( var m in Selected.Measures)\r\n{\r\n m.FormatString = \"#,0.00\";\r\n};",
* "Tooltip": "Formats the selected measures to two decimal places.",
* "ValidContexts": "Measure"
* }
*
*/

// Loop through all selected measures
foreach ( var m in Selected.Measures)
{
// Set the measures format string
m.FormatString = "#,0.00";
};
24 changes: 24 additions & 0 deletions Basic/Format Measure as Percentage (1 decimal place).csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Title: Format Measure as a percentage to 1 decimal place
*
* Author: Adam Grisdale
*
* Loops through the selected measures and formats them as a percentage to 1 decimal place with a thousands separator.
* Designed to be used as a macro action added to the MacroActions json file: %LocalAppData%\TabularEditor\MacroActions.json
*
* {
* "Name": "Formatting\\Percentage\\One Decimal Place",
* "Enabled": "true",
* "Execute": "foreach ( var m in Selected.Measures)\r\n{\r\n m.FormatString = \"#,0.0 %\";\r\n};",
* "Tooltip": "Formats the selected measures to one decimal place.",
* "ValidContexts": "Measure"
* }
*
*/

// Loop through all selected measures
foreach ( var m in Selected.Measures)
{
// Set the measures format string
m.FormatString = "#,0.0 %";
};
24 changes: 24 additions & 0 deletions Basic/Format Measure as Percentage (2 decimal places).csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Title: Format Measure as a percentage to 2 decimal places
*
* Author: Adam Grisdale
*
* Loops through the selected measures and formats them as a percentage to 2 decimal places with a thousands separator.
* Designed to be used as a macro action added to the MacroActions json file: %LocalAppData%\TabularEditor\MacroActions.json
*
* {
* "Name": "Formatting\\Percentage\\Two Decimal Places",
* "Enabled": "true",
* "Execute": "foreach ( var m in Selected.Measures)\r\n{\r\n m.FormatString = \"#,0.00 %\";\r\n};",
* "Tooltip": "Formats the selected measures to one decimal place.",
* "ValidContexts": "Measure"
* }
*
*/

// Loop through all selected measures
foreach ( var m in Selected.Measures)
{
// Set the measures format string
m.FormatString = "#,0.00 %";
};
24 changes: 24 additions & 0 deletions Basic/Format Measure as Whole Number.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Title: Format Measure as a whole number
*
* Author: Adam Grisdale
*
* Loops through the selected measures and formats them as a whole number with a thousands separator.
* Designed to be used as a macro action added to the MacroActions json file: %LocalAppData%\TabularEditor\MacroActions.json
*
* {
* "Name": "Formatting\\Whole Number",
* "Enabled": "true",
* "Execute": "foreach ( var m in Selected.Measures)\r\n{\r\n m.FormatString = \"#,0\";\r\n};",
* "Tooltip": "Formats the selected measures to one decimal place.",
* "ValidContexts": "Measure"
* }
*
*/

// Loop through all selected measures
foreach ( var m in Selected.Measures)
{
// Set the measures format string
m.FormatString = "#,0";
};