From dbc16f263b4542eb098500562e21ec6ada556017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernat=20Agull=C3=B3=20Rosell=C3=B3?= <40455069+bernatagulloesbrina@users.noreply.github.com> Date: Sun, 20 Jun 2021 00:08:28 +0200 Subject: [PATCH] Time Intelligence Calculation Group This update includes two measures to work as dynamic labels --- ...ntelligence Calculation Group Creation.csx | 202 +++++++++++++----- 1 file changed, 144 insertions(+), 58 deletions(-) diff --git a/Time Intelligence Calculation Group Creation.csx b/Time Intelligence Calculation Group Creation.csx index 3f080fb..54915d2 100644 --- a/Time Intelligence Calculation Group Creation.csx +++ b/Time Intelligence Calculation Group Creation.csx @@ -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 @@ -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"; @@ -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"; @@ -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 = ""; //default value if user has selected no measures string calcItemFormatProtection = ""; //default value if user has selected no measures @@ -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 != "{") { @@ -97,27 +101,32 @@ if(affectedMeasures != "{") { affectedMeasureTable.IsHidden = true; - - string affectedMeasuresValues = "VALUES('" + affectedMeasuresTableName + "'[" + affectedMeasuresColumnName + "])"; calcItemProtection = - "IF(" + + "SWITCH(" + + " TRUE()," + " SELECTEDMEASURENAME() IN " + affectedMeasuresValues + "," + " ," + + " ISSELECTEDMEASURE([" + labelAsValueMeasureName + "])," + + " ," + " SELECTEDMEASURE() " + ")"; calcItemFormatProtection = - "IF(" + + "SWITCH(" + + " TRUE() ," + " SELECTEDMEASURENAME() IN " + affectedMeasuresValues + "," + " ," + + " ISSELECTEDMEASURE([" + labelAsFormatStringMeasureName + "])," + + " ," + " 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 + "]"; @@ -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")) { @@ -173,14 +189,12 @@ var ShowValueForDatesMeasure = dateTable.AddMeasure(ShowValueForDatesMeasureName ShowValueForDatesMeasure.FormatDax(); - - - - - string CY = "/*CY*/ " + "SELECTEDMEASURE()"; + +string CYlabel = + "SELECTEDVALUE(" + yearColumnWithTable + ")"; string PY = @@ -195,7 +209,21 @@ string PY = " ) " + " ) " + ") "; - + + +string PYlabel = + "/*PY*/ " + + "IF (" + + " [" + ShowValueForDatesMeasureName + "], " + + " CALCULATE ( " + + " "+ CYlabel + ", " + + " CALCULATETABLE ( " + + " DATEADD ( " + dateColumnWithTable + " , -1, YEAR ), " + + " " + dateWithSalesWithTable + " = TRUE " + + " ) " + + " ) " + + ") "; + string YOY = "/*YOY*/ " + @@ -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"; @@ -236,7 +288,10 @@ string YTD = " )" + ") "; - + +string YTDlabel = CYlabel + "& \" YTD\""; + + string PYTD = "/*PYTD*/" + "IF ( " + @@ -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 ), " + @@ -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%*/" + @@ -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()"; @@ -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("",calcItems[j,1]); - string itemFormatExpression = defFormatString; + + string itemExpression = calcItemProtection.Replace("",calcItems[j,1]); + itemExpression = itemExpression.Replace("",calcItems[j,4]); + + string itemFormatExpression = calcItemFormatProtection.Replace("",calcItems[j,2]); + itemFormatExpression = itemFormatExpression.Replace("","\"\"\"\" & " + calcItems[j,4] + " & \"\"\"\""); - if(calcItems[j,2] != defFormatString) { - itemFormatExpression = calcItemFormatProtection.Replace("",calcItems[j,2]); - }; + //if(calcItems[j,2] != defFormatString) { + // itemFormatExpression = calcItemFormatProtection.Replace("",calcItems[j,2]); + //}; string itemDescription = calcItems[j,3]; @@ -330,6 +414,8 @@ foreach(var cg in Model.CalculationGroups) { nCalcItem.Description = itemDescription; }; - }; + }; + + }; };