-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathadd-format-strings.csx
538 lines (462 loc) · 13.9 KB
/
add-format-strings.csx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Apply format strings
//
// Script author: Kurt Buhler; Data Goblins
// Script created: Sept 16, 2024
// Script supports: Tabular Editor 3.X (Tabular Editor 2.X untested)
// Disclaimer: Script created with help from Chat-GPT 1o (productivity enhancement and some C# patterns)
//
//
//// Script instructions: Use this script when connected with any Power BI or AAS/SSAS semantic model.
//
// 1. Select the measures you want to format
// 2. Run the script
// 3. Select the format that you want to apply in the dialog
// 4. Validate the format string and test the measure in reports
// Namespaces
using System.Windows.Forms;
using System.Drawing;
// Don't show the script execution dialog or cursor
ScriptHelper.WaitFormVisible = false;
Application.UseWaitCursor = false;
// Init vars
string _BaseFormat = "#,##0";
// Create a new form
var _Form = new Form()
{
Text = "Format Options",
Padding = new Padding(10),
StartPosition = FormStartPosition.CenterParent,
FormBorderStyle = FormBorderStyle.FixedDialog,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
MaximizeBox = false,
MinimizeBox = false,
ShowIcon = false,
MinimumSize = new Size(700, 0)
};
// Format options container
var _FormatOptionsPanel = new TableLayoutPanel()
{
ColumnCount = 2,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
Dock = DockStyle.Top,
Padding = new Padding(0, 0, 0, 12),
CellBorderStyle = TableLayoutPanelCellBorderStyle.None
};
// Add column styles
_FormatOptionsPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
_FormatOptionsPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
// Symbol select
var _SymbolLabel = new Label()
{
Text = "Symbol:",
AutoSize = true,
Padding = new Padding(0, 5, 0, 5)
};
// Create radio buttons for symbol selection
var _SymbolOption0 = new RadioButton()
{
Text = "None",
AutoSize = true,
Checked = true
};
var _SymbolOption1 = new RadioButton()
{
Text = "+/-",
AutoSize = true
};
var _SymbolOption2 = new RadioButton()
{
Text = "↓/↑",
AutoSize = true
};
var _SymbolOption3 = new RadioButton()
{
Text = "▲/▼",
AutoSize = true
};
var _SymbolOption4 = new RadioButton()
{
Text = "↗ / ↘",
AutoSize = true
};
// Radio container
var _SymbolOptionsPanel = new FlowLayoutPanel()
{
FlowDirection = FlowDirection.LeftToRight,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
WrapContents = false // Prevent wrapping to a new line
};
// Add radio buttons to the panel
_SymbolOptionsPanel.Controls.Add(_SymbolOption0);
_SymbolOptionsPanel.Controls.Add(_SymbolOption1);
_SymbolOptionsPanel.Controls.Add(_SymbolOption2);
_SymbolOptionsPanel.Controls.Add(_SymbolOption3);
_SymbolOptionsPanel.Controls.Add(_SymbolOption4);
// Option for symbol position
var _SymbolPositionLabel = new Label()
{
Text = "Symbol Position:",
AutoSize = true,
Padding = new Padding(0, 5, 0, 5)
};
var _SymbolPositionOption1 = new RadioButton()
{
Text = "Beginning",
AutoSize = true,
Checked = true
};
var _SymbolPositionOption2 = new RadioButton()
{
Text = "End",
AutoSize = true
};
// Symbol position
var _SymbolPositionPanel = new FlowLayoutPanel()
{
FlowDirection = FlowDirection.LeftToRight,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
WrapContents = false
};
// Add radio buttons to the panel
_SymbolPositionPanel.Controls.Add(_SymbolPositionOption1);
_SymbolPositionPanel.Controls.Add(_SymbolPositionOption2);
// Spacing options
var _SpacingLabel = new Label()
{
Text = "Symbol Spacing:",
AutoSize = true,
Padding = new Padding(0, 5, 0, 5)
};
var _SpacingOption1 = new RadioButton()
{
Text = "No Space",
AutoSize = true,
Checked = true
};
var _SpacingOption2 = new RadioButton()
{
Text = "Space at Start",
AutoSize = true
};
var _SpacingOption3 = new RadioButton()
{
Text = "Space at End",
AutoSize = true
};
// Spacing options layout
var _SpacingOptionsPanel = new FlowLayoutPanel()
{
FlowDirection = FlowDirection.LeftToRight,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
WrapContents = false
};
// Add radio buttons to the panel
_SpacingOptionsPanel.Controls.Add(_SpacingOption1);
_SpacingOptionsPanel.Controls.Add(_SpacingOption2);
_SpacingOptionsPanel.Controls.Add(_SpacingOption3);
// Nr decimal places
var _DecimalPlacesLabel = new Label()
{
Text = "Decimal Places:",
AutoSize = true,
Padding = new Padding(0, 5, 0, 5)
};
var _DecimalPlacesComboBox = new ComboBox()
{
DropDownStyle = ComboBoxStyle.DropDownList,
Width = 100
};
_DecimalPlacesComboBox.Items.AddRange(new string[] { "0", "1", "2", "3", "4", "Over 9000" });
_DecimalPlacesComboBox.SelectedIndex = 0;
// Percentage checkbox
var _PercentageLabel = new Label()
{
Text = "Percentage:",
AutoSize = true,
Padding = new Padding(0, 5, 0, 5)
};
var _PercentageCheckBox = new CheckBox()
{
AutoSize = true
};
// Adding a suffix
var _SuffixLabel = new Label()
{
Text = "Suffix:",
AutoSize = true,
Padding = new Padding(0, 5, 0, 5)
};
var _SuffixTextBox = new TextBox()
{
Width = 100
};
// Adding a prefix
var _PrefixLabel = new Label()
{
Text = "Prefix:",
AutoSize = true,
Padding = new Padding(0, 5, 0, 5)
};
var _PrefixTextBox = new TextBox()
{
Width = 100
};
// Sample formatted number label
var _SampleLabel = new Label()
{
Text = "Sample Output:",
AutoSize = true,
Padding = new Padding(0, 5, 0, 5)
};
var _SampleValueLabel = new Label()
{
Text = "",
AutoSize = true
};
// Add controls to the format options panel
int rowIndex = 0;
_FormatOptionsPanel.Controls.Add(_SymbolLabel, 0, rowIndex);
_FormatOptionsPanel.Controls.Add(_SymbolOptionsPanel, 1, rowIndex);
rowIndex++;
_FormatOptionsPanel.Controls.Add(_SymbolPositionLabel, 0, rowIndex);
_FormatOptionsPanel.Controls.Add(_SymbolPositionPanel, 1, rowIndex);
rowIndex++;
_FormatOptionsPanel.Controls.Add(_SpacingLabel, 0, rowIndex);
_FormatOptionsPanel.Controls.Add(_SpacingOptionsPanel, 1, rowIndex);
rowIndex++;
_FormatOptionsPanel.Controls.Add(_DecimalPlacesLabel, 0, rowIndex);
_FormatOptionsPanel.Controls.Add(_DecimalPlacesComboBox, 1, rowIndex);
rowIndex++;
_FormatOptionsPanel.Controls.Add(_PercentageLabel, 0, rowIndex);
_FormatOptionsPanel.Controls.Add(_PercentageCheckBox, 1, rowIndex);
rowIndex++;
_FormatOptionsPanel.Controls.Add(_SuffixLabel, 0, rowIndex);
_FormatOptionsPanel.Controls.Add(_SuffixTextBox, 1, rowIndex);
rowIndex++;
_FormatOptionsPanel.Controls.Add(_PrefixLabel, 0, rowIndex);
_FormatOptionsPanel.Controls.Add(_PrefixTextBox, 1, rowIndex);
rowIndex++;
_FormatOptionsPanel.Controls.Add(_SampleLabel, 0, rowIndex);
_FormatOptionsPanel.Controls.Add(_SampleValueLabel, 1, rowIndex);
rowIndex++;
// Create panel for buttons
var _ButtonPanel = new FlowLayoutPanel()
{
FlowDirection = FlowDirection.RightToLeft,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
Dock = DockStyle.Bottom,
Padding = new Padding(0, 10, 0, 0) // Padding top
};
// Create OK and Cancel buttons
var _OkButton = new Button()
{
Text = "Apply Format",
DialogResult = DialogResult.OK,
AutoSize = true
};
var _CancelButton = new Button()
{
Text = "Cancel",
DialogResult = DialogResult.Cancel,
AutoSize = true
};
// Add buttons to the panel
_ButtonPanel.Controls.Add(_CancelButton);
_ButtonPanel.Controls.Add(_OkButton);
// Set form's Accept and Cancel buttons
_Form.AcceptButton = _OkButton;
_Form.CancelButton = _CancelButton;
// Add panels to the form
_Form.Controls.Add(_FormatOptionsPanel);
_Form.Controls.Add(_ButtonPanel);
// Event handler to update sample output
void UpdateSampleOutput(object sender, EventArgs e)
{
// Retrieve format options
string _SymbolPositive = "";
string _SymbolNegative = "";
if (_SymbolOption1.Checked)
{
_SymbolPositive = "+";
_SymbolNegative = "-";
}
else if (_SymbolOption2.Checked)
{
_SymbolPositive = "\u2191"; // Up arrow ↑
_SymbolNegative = "\u2193"; // Down arrow ↓
}
else if (_SymbolOption3.Checked)
{
_SymbolPositive = "\u25B2"; // Black up-pointing triangle ▲
_SymbolNegative = "\u25BC"; // Black down-pointing triangle ▼
}
else if (_SymbolOption4.Checked)
{
_SymbolPositive = "\u2197"; // North East Arrow ↗
_SymbolNegative = "\u2198"; // South East Arrow ↘
}
int _DecimalPlaces;
if (_DecimalPlacesComboBox.SelectedItem.ToString() == "Over 9000")
{
_DecimalPlaces = 9001;
}
else
{
_DecimalPlaces = int.Parse(_DecimalPlacesComboBox.SelectedItem.ToString());
}
bool _IsPercentage = _PercentageCheckBox.Checked;
string _Suffix = _SuffixTextBox.Text;
string _Prefix = _PrefixTextBox.Text;
// Symbol position
bool _SymbolAtBeginning = _SymbolPositionOption1.Checked;
// Spacing option
string _SpacePrefix = "";
string _SpaceSuffix = "";
if (_SpacingOption2.Checked)
{
_SpacePrefix = " ";
}
else if (_SpacingOption3.Checked)
{
_SpaceSuffix = " ";
}
// Generate numeric format string
string _NumericFormat = "#,##0";
if (_DecimalPlaces > 0)
{
_NumericFormat += "." + new string('0', _DecimalPlaces);
}
if (_IsPercentage)
{
_NumericFormat += "%";
}
// Adjust format string based on options
string _PositiveFormat, _NegativeFormat, _ZeroFormat;
if (_SymbolAtBeginning)
{
_PositiveFormat = _Prefix + _SymbolPositive + _SpacePrefix + "{0:" + _NumericFormat + "}" + _Suffix + _SpaceSuffix;
_NegativeFormat = _Prefix + _SymbolNegative + _SpacePrefix + "{0:" + _NumericFormat + "}" + _Suffix + _SpaceSuffix;
}
else
{
_PositiveFormat = _Prefix + "{0:" + _NumericFormat + "}" + _SpaceSuffix + _SymbolPositive + _Suffix;
_NegativeFormat = _Prefix + "{0:" + _NumericFormat + "}" + _SpaceSuffix + _SymbolNegative + _Suffix;
}
_ZeroFormat = _Prefix + "{0:" + _NumericFormat + "}" + _Suffix;
string _FormatString = _PositiveFormat + ";" + _NegativeFormat + ";" + _ZeroFormat;
// Format the sample number
double sampleNumber = 1234.56;
string formattedNumber = string.Format(_FormatString, sampleNumber);
// Update the sample value label
_SampleValueLabel.Text = formattedNumber;
}
// Attach event handlers to controls
_SymbolOption1.CheckedChanged += UpdateSampleOutput;
_SymbolOption2.CheckedChanged += UpdateSampleOutput;
_SymbolOption3.CheckedChanged += UpdateSampleOutput;
_SymbolOption4.CheckedChanged += UpdateSampleOutput;
_SymbolPositionOption1.CheckedChanged += UpdateSampleOutput;
_SymbolPositionOption2.CheckedChanged += UpdateSampleOutput;
_SpacingOption1.CheckedChanged += UpdateSampleOutput;
_SpacingOption2.CheckedChanged += UpdateSampleOutput;
_SpacingOption3.CheckedChanged += UpdateSampleOutput;
_DecimalPlacesComboBox.SelectedIndexChanged += UpdateSampleOutput;
_PercentageCheckBox.CheckedChanged += UpdateSampleOutput;
_SuffixTextBox.TextChanged += UpdateSampleOutput;
_PrefixTextBox.TextChanged += UpdateSampleOutput;
// Initialize the sample output
UpdateSampleOutput(null, null);
// Display the form and capture the result
var _Result = _Form.ShowDialog();
if (_Result == DialogResult.OK)
{
// Retrieve format options
string _SymbolPositive = "";
string _SymbolNegative = "";
if (_SymbolOption1.Checked)
{
_SymbolPositive = "+";
_SymbolNegative = "-";
}
else if (_SymbolOption2.Checked)
{
_SymbolPositive = "\u2191"; // Up arrow ↑
_SymbolNegative = "\u2193"; // Down arrow ↓
}
else if (_SymbolOption3.Checked)
{
_SymbolPositive = "\u25B2"; // Black up-pointing triangle ▲
_SymbolNegative = "\u25BC"; // Black down-pointing triangle ▼
}
else if (_SymbolOption4.Checked)
{
_SymbolPositive = "\u2197"; // North East Arrow ↗
_SymbolNegative = "\u2198"; // South East Arrow ↘
}
int _DecimalPlaces;
if (_DecimalPlacesComboBox.SelectedItem.ToString() == "Over 9000")
{
_DecimalPlaces = 9001;
}
else
{
_DecimalPlaces = int.Parse(_DecimalPlacesComboBox.SelectedItem.ToString());
}
bool _IsPercentage = _PercentageCheckBox.Checked;
string _Suffix = _SuffixTextBox.Text;
string _Prefix = _PrefixTextBox.Text;
// Symbol position
bool _SymbolAtBeginning = _SymbolPositionOption1.Checked;
// Spacing option
string _SpacePrefix = "";
string _SpaceSuffix = "";
if (_SpacingOption2.Checked)
{
_SpacePrefix = " ";
}
else if (_SpacingOption3.Checked)
{
_SpaceSuffix = " ";
}
// Generate numeric format string
string _NumericFormat = "#,##0";
if (_DecimalPlaces > 0)
{
_NumericFormat += "." + new string('0', _DecimalPlaces);
}
if (_IsPercentage)
{
_NumericFormat += "%";
}
// Adjust format string based on options
string _PositiveFormat, _NegativeFormat, _ZeroFormat;
if (_SymbolAtBeginning)
{
_PositiveFormat = _Prefix + _SymbolPositive + _SpacePrefix + _NumericFormat + _Suffix + _SpaceSuffix;
_NegativeFormat = _Prefix + _SymbolNegative + _SpacePrefix + _NumericFormat + _Suffix + _SpaceSuffix;
}
else
{
_PositiveFormat = _Prefix + _NumericFormat + _SpaceSuffix + _SymbolPositive + _Suffix;
_NegativeFormat = _Prefix + _NumericFormat + _SpaceSuffix + _SymbolNegative + _Suffix;
}
_ZeroFormat = _Prefix + _NumericFormat + _Suffix;
string _FormatString = _PositiveFormat + ";" + _NegativeFormat + ";" + _ZeroFormat;
// Apply the format string to selected measures
foreach (var _m in Selected.Measures)
{
_m.FormatString = _FormatString;
}
// Inform the user
Info("Format string applied to " + Selected.Measures.Count() + " measures:\n" + _FormatString);
}
else
{
Error("Script Cancelled. No changes made to the model.");
}