var templateDax = @"/* ----- DAX template begin ----- */ VAR yearLoic = COUNTROWS ( DISTINCT ( FILTER ( ALLSELECTED ( YOA_selections[Value] ), YOA_selections[Value] = %YEAR% ) ) ) = 1 VAR logicTest = COUNTROWS ( DISTINCT ( FILTER ( ALLSELECTED ( 'ULR Selections'[Selection] ), 'ULR Selections'[Selection] = %Type% ) ) ) = 1 RETURN IF ( logicTest && yearLoic, CALCULATE ( [BE_ULR], pbi_data[year_of_account] = %YEAR% ), BLANK () ) /* ----- DAX template end ----- */"; var years = new[] { "2016", "2017", "2018","2019","2020" }; foreach(var year in years) { var measureName = "BE " + year + " ULR"; Selected.Table.AddMeasure( measureName, templateDax.Replace("%YEAR%", '"'+year+'"' ).Replace("%Type%","\"Best Estimates\"") ); }