Skip to content

Commit

Permalink
Time Intelligence Calculation Group
Browse files Browse the repository at this point in the history
This update includes two measures to work as dynamic labels
  • Loading branch information
bernatagulloesbrina committed Jun 19, 2021
1 parent 9ca6564 commit dbc16f2
Showing 1 changed file with 144 additions and 58 deletions.
202 changes: 144 additions & 58 deletions Time Intelligence Calculation Group Creation.csx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// '2021-05-01 / B.Agullo /
// '2021-05-17 / B.Agullo / added affected measure table
// '2021-06-19 / B.Agullo / data label measures
// by Bernat Agulló
// www.esbrina-ba.com

Expand All @@ -10,11 +11,10 @@
//measure names can also be included in the following array (no need to select them)
string[] preSelectedMeasures = {}; //include measure names in double quotes, like: {"Profit","Total Cost"};

//if no measures are selected and none specified above,
//all measures under the calculation group filter context will be afected
//AT LEAST ONE MEASURE HAS TO BE AFFECTED,
//either by selecting it or typing its name in the preSelectedMeasures Variable


//change the next 6 string variables to fit your model
//change the next string variables to fit your model

//add the name of your calculation group here
string calcGroupName = "Time Intelligence";
Expand All @@ -28,7 +28,8 @@ string factTableDateColumnName = "Order Date";

//add the name for date table of the model
string dateTableName = "Date";
string dateTableDateColumnName = "Date";
string dateTableDateColumnName = "Date";
string dateTableYearColumnName = "CalendarYear";

//add the measure and calculated column names you want or leave them as they are
string ShowValueForDatesMeasureName = "ShowValueForDates";
Expand All @@ -37,14 +38,17 @@ string dateWithSalesColumnName = "DateWithSales";
string affectedMeasuresTableName = "Time Intelligence Affected Measures";
string affectedMeasuresColumnName = "Measure";


string labelAsValueMeasureName = "Label as Value Measure";
string labelAsFormatStringMeasureName = "Label as format string";



//
// ----- do not modify script below this line -----
//




string calcItemProtection = "<CODE>"; //default value if user has selected no measures
string calcItemFormatProtection = "<CODE>"; //default value if user has selected no measures

Expand All @@ -71,15 +75,15 @@ if (Selected.Measures.Count != 0) {
}else{
affectedMeasures = affectedMeasures + ",\"" + m.Name + "\"" ;
};
};



};
};





//check that by either method at least one measure is affected
if(affectedMeasures == "{") {
Error("No measures affected by calc group");
return;
};

//if there where selected or preselected measures, prepare protection code for expresion and formatstring
if(affectedMeasures != "{") {

Expand All @@ -97,27 +101,32 @@ if(affectedMeasures != "{") {

affectedMeasureTable.IsHidden = true;



string affectedMeasuresValues = "VALUES('" + affectedMeasuresTableName + "'[" + affectedMeasuresColumnName + "])";

calcItemProtection =
"IF(" +
"SWITCH(" +
" TRUE()," +
" SELECTEDMEASURENAME() IN " + affectedMeasuresValues + "," +
" <CODE> ," +
" ISSELECTEDMEASURE([" + labelAsValueMeasureName + "])," +
" <LABELCODE> ," +
" SELECTEDMEASURE() " +
")";


calcItemFormatProtection =
"IF(" +
"SWITCH(" +
" TRUE() ," +
" SELECTEDMEASURENAME() IN " + affectedMeasuresValues + "," +
" <CODE> ," +
" ISSELECTEDMEASURE([" + labelAsFormatStringMeasureName + "])," +
" <LABELCODEFORMATSTRING> ," +
" SELECTEDMEASUREFORMATSTRING() " +
")";
};

string dateColumnWithTable = "'" + dateTableName + "'[" + dateTableDateColumnName + "]";
string dateColumnWithTable = "'" + dateTableName + "'[" + dateTableDateColumnName + "]";
string yearColumnWithTable = "'" + dateTableName + "'[" + dateTableYearColumnName + "]";
string factDateColumnWithTable = "'" + factTableName + "'[" + factTableDateColumnName + "]";
string dateWithSalesWithTable = "'" + dateTableName + "'[" + dateWithSalesColumnName + "]";
string calcGroupColumnWithTable = "'" + calcGroupName + "'[" + columnName + "]";
Expand All @@ -137,6 +146,13 @@ if (calcGroup.SourceType.ToString() != "CalculationGroup") {
Error("Table exists in Model but is not a Calculation Group. Rename the existing table or choose an alternative name for your Calculation Group.");
return;
};

//adds the two measures that will be used for label as value, label as format string
var labelAsValueMeasure = calcGroup.AddMeasure(labelAsValueMeasureName,"0");
labelAsValueMeasure.Description = "Use this measure to show the year evaluated in tables";

var labelAsFormatStringMeasure = calcGroup.AddMeasure(labelAsFormatStringMeasureName,"0");
labelAsFormatStringMeasure.Description = "Use this measure to show the year evaluated in charts";

//by default the calc group has a column called Name. If this column is still called Name change this in line with specfied variable
if (calcGroup.Columns.Contains("Name")) {
Expand Down Expand Up @@ -173,14 +189,12 @@ var ShowValueForDatesMeasure = dateTable.AddMeasure(ShowValueForDatesMeasureName
ShowValueForDatesMeasure.FormatDax();







string CY =
"/*CY*/ " +
"SELECTEDMEASURE()";

string CYlabel =
"SELECTEDVALUE(" + yearColumnWithTable + ")";


string PY =
Expand All @@ -195,7 +209,21 @@ string PY =
" ) " +
" ) " +
") ";



string PYlabel =
"/*PY*/ " +
"IF (" +
" [" + ShowValueForDatesMeasureName + "], " +
" CALCULATE ( " +
" "+ CYlabel + ", " +
" CALCULATETABLE ( " +
" DATEADD ( " + dateColumnWithTable + " , -1, YEAR ), " +
" " + dateWithSalesWithTable + " = TRUE " +
" ) " +
" ) " +
") ";


string YOY =
"/*YOY*/ " +
Expand All @@ -207,22 +235,46 @@ string YOY =
" ValueCurrentPeriod - ValuePreviousPeriod" +
" ) " +
"RETURN " +
" Result ";
" Result ";

string YOYlabel =
"/*YOY*/ " +
"VAR ValueCurrentPeriod = " + CYlabel + " " +
"VAR ValuePreviousPeriod = " + PYlabel + " " +
"VAR Result = " +
"IF ( " +
" NOT ISBLANK ( ValueCurrentPeriod ) && NOT ISBLANK ( ValuePreviousPeriod ), " +
" ValueCurrentPeriod & \" vs \" & ValuePreviousPeriod" +
" ) " +
"RETURN " +
" Result ";

string YOYpct =
"/*YOY%*/ " +
"VAR ValueCurrentPeriod = " + CY + " " +
"VAR ValuePreviousPeriod = " + PY + " " +
"VAR CurrentMinusPreviousPeriod = " +
"IF ( " +
" NOT ISBLANK ( ValueCurrentPeriod ) && NOT ISBLANK ( ValuePreviousPeriod ), " +
" ValueCurrentPeriod - ValuePreviousPeriod" +
" ) " +
"VAR Result = " +
"DIVIDE ( " +
" CurrentMinusPreviousPeriod," +
" ValuePreviousPeriod" +
") " +
"RETURN " +
" Result";

string YOYpct =
string YOYpctLabel =
"/*YOY%*/ " +
"VAR ValueCurrentPeriod = " + CY + " " +
"VAR ValuePreviousPeriod = " + PY + " " +
"VAR CurrentMinusPreviousPeriod = " +
"VAR ValueCurrentPeriod = " + CYlabel + " " +
"VAR ValuePreviousPeriod = " + PYlabel + " " +
"VAR Result = " +
"IF ( " +
" NOT ISBLANK ( ValueCurrentPeriod ) && NOT ISBLANK ( ValuePreviousPeriod ), " +
" ValueCurrentPeriod - ValuePreviousPeriod" +
" ValueCurrentPeriod & \" vs \" & ValuePreviousPeriod & \" (%)\"" +
" ) " +
"VAR Result = " +
"DIVIDE ( " +
" CurrentMinusPreviousPeriod," +
" ValuePreviousPeriod" +
") " +
"RETURN " +
" Result";

Expand All @@ -236,7 +288,10 @@ string YTD =
" )" +
") ";



string YTDlabel = CYlabel + "& \" YTD\"";


string PYTD =
"/*PYTD*/" +
"IF ( " +
Expand All @@ -249,13 +304,14 @@ string PYTD =
" )" +
" )" +
") ";


string PYTDlabel = PYlabel + "& \" YTD\"";


string YOYTD =
"/*YOYTD*/" +
"VAR ValueCurrentPeriod = " + YTD +
"VAR ValuePreviousPeriod = " + PYTD +
"VAR ValueCurrentPeriod = " + YTD + " " +
"VAR ValuePreviousPeriod = " + PYTD + " " +
"VAR Result = " +
"IF ( " +
" NOT ISBLANK ( ValueCurrentPeriod ) && NOT ISBLANK ( ValuePreviousPeriod ), " +
Expand All @@ -264,6 +320,20 @@ string YOYTD =
"RETURN " +
" Result ";


string YOYTDlabel =
"/*YOYTD*/" +
"VAR ValueCurrentPeriod = " + YTDlabel + " " +
"VAR ValuePreviousPeriod = " + PYTDlabel + " " +
"VAR Result = " +
"IF ( " +
" NOT ISBLANK ( ValueCurrentPeriod ) && NOT ISBLANK ( ValuePreviousPeriod ), " +
" ValueCurrentPeriod & \" vs \" & ValuePreviousPeriod" +
" ) " +
"RETURN " +
" Result ";



string YOYTDpct =
"/*YOYTD%*/" +
Expand All @@ -281,6 +351,19 @@ string YOYTDpct =
") " +
"RETURN " +
" Result";


string YOYTDpctLabel =
"/*YOY%*/ " +
"VAR ValueCurrentPeriod = " + YTDlabel + " " +
"VAR ValuePreviousPeriod = " + PYTDlabel + " " +
"VAR Result = " +
"IF ( " +
" NOT ISBLANK ( ValueCurrentPeriod ) && NOT ISBLANK ( ValuePreviousPeriod ), " +
" ValueCurrentPeriod & \" vs \" & ValuePreviousPeriod & \" (%)\"" +
" ) " +
"RETURN " +
" Result";


string defFormatString = "SELECTEDMEASUREFORMATSTRING()";
Expand All @@ -290,35 +373,36 @@ string pctFormatString = "\"#,##0.#%\"";
//the order in the array also determines the ordinal position of the item
string[ , ] calcItems =
{
{"CY", CY, defFormatString, "Current year"},
{"PY", PY, defFormatString, "Previous year"},
{"YOY", YOY, defFormatString, "Year-over-year" },
{"YOY%", YOYpct, pctFormatString, "Year-over-year%"},
{"YTD", YTD, defFormatString, "Year-to-date"},
{"PYTD", PYTD, defFormatString, "Previous year-to-date"},
{"YOYTD", YOYTD, defFormatString, "Year-over-year-to-date"},
{"YOYTD%", YOYTDpct, pctFormatString, "Year-over-year-to-date%"},
{"CY", CY, defFormatString, "Current year", CYlabel},
{"PY", PY, defFormatString, "Previous year", PYlabel},
{"YOY", YOY, defFormatString, "Year-over-year", YOYlabel},
{"YOY%", YOYpct, pctFormatString, "Year-over-year%", YOYpctLabel},
{"YTD", YTD, defFormatString, "Year-to-date", YTDlabel},
{"PYTD", PYTD, defFormatString, "Previous year-to-date", PYTDlabel},
{"YOYTD", YOYTD, defFormatString, "Year-over-year-to-date", YOYTDlabel},
{"YOYTD%", YOYTDpct, pctFormatString, "Year-over-year-to-date%", YOYTDpctLabel},
};


int j = 0;





//create calculation items for each calculation with formatstring and description
foreach(var cg in Model.CalculationGroups) {
if (cg.Name == calcGroupName) {
for (j = 0; j < calcItems.GetLength(0); j++) {

string itemName = calcItems[j,0];
string itemExpression = calcItemProtection.Replace("<CODE>",calcItems[j,1]);
string itemFormatExpression = defFormatString;

string itemExpression = calcItemProtection.Replace("<CODE>",calcItems[j,1]);
itemExpression = itemExpression.Replace("<LABELCODE>",calcItems[j,4]);

string itemFormatExpression = calcItemFormatProtection.Replace("<CODE>",calcItems[j,2]);
itemFormatExpression = itemFormatExpression.Replace("<LABELCODEFORMATSTRING>","\"\"\"\" & " + calcItems[j,4] + " & \"\"\"\"");

if(calcItems[j,2] != defFormatString) {
itemFormatExpression = calcItemFormatProtection.Replace("<CODE>",calcItems[j,2]);
};
//if(calcItems[j,2] != defFormatString) {
// itemFormatExpression = calcItemFormatProtection.Replace("<CODE>",calcItems[j,2]);
//};

string itemDescription = calcItems[j,3];

Expand All @@ -330,6 +414,8 @@ foreach(var cg in Model.CalculationGroups) {
nCalcItem.Description = itemDescription;

};
};
};


};
};

0 comments on commit dbc16f2

Please sign in to comment.