diff --git a/Basic/Add Prefix to Selected Measure Names.csx b/Basic/Add Prefix to Selected Measure Names.csx index 4bb769e..781006e 100644 --- a/Basic/Add Prefix to Selected Measure Names.csx +++ b/Basic/Add Prefix to Selected Measure Names.csx @@ -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; - } \ No newline at end of file + // Grab the current name as a variable and append the prefix entered + m.Name = NewPrefix + " " + m.Name; + } +} \ No newline at end of file diff --git a/Basic/Add Suffix to Selected Measure Names.csx b/Basic/Add Suffix to Selected Measure Names.csx new file mode 100644 index 0000000..328d612 --- /dev/null +++ b/Basic/Add Suffix to Selected Measure Names.csx @@ -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; + } +} \ No newline at end of file diff --git a/Basic/Format Measure as Decimal Number (1 decimal place).csx b/Basic/Format Measure as Decimal Number (1 decimal place).csx new file mode 100644 index 0000000..89eb358 --- /dev/null +++ b/Basic/Format Measure as Decimal Number (1 decimal place).csx @@ -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"; +}; \ No newline at end of file diff --git a/Basic/Format Measure as Decimal Number (2 decimal places).csx b/Basic/Format Measure as Decimal Number (2 decimal places).csx new file mode 100644 index 0000000..89e38f2 --- /dev/null +++ b/Basic/Format Measure as Decimal Number (2 decimal places).csx @@ -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"; +}; \ No newline at end of file diff --git a/Basic/Format Measure as Percentage (1 decimal place).csx b/Basic/Format Measure as Percentage (1 decimal place).csx new file mode 100644 index 0000000..4ca0ca9 --- /dev/null +++ b/Basic/Format Measure as Percentage (1 decimal place).csx @@ -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 %"; +}; \ No newline at end of file diff --git a/Basic/Format Measure as Percentage (2 decimal places).csx b/Basic/Format Measure as Percentage (2 decimal places).csx new file mode 100644 index 0000000..dd36b62 --- /dev/null +++ b/Basic/Format Measure as Percentage (2 decimal places).csx @@ -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 %"; +}; \ No newline at end of file diff --git a/Basic/Format Measure as Whole Number.csx b/Basic/Format Measure as Whole Number.csx new file mode 100644 index 0000000..89fdfcb --- /dev/null +++ b/Basic/Format Measure as Whole Number.csx @@ -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"; +}; \ No newline at end of file