计算度量值是一种在每次使用时进行计算的命名 DAX 表达式;每次使用时都进行计算的好处之一是根据使用它的上下文动态计算该计算度量值。
计算度量值表示形式
计算度量值是一种可供计算并且还可按其名称在其他 DAX 表达式中引用的一种命名 DAX 表达式。
AMO 中的计算度量值
在使用 AMO 管理表格模型计算度量值时,在逻辑计算度量值对象和 MdxScript 对象的 Command 对象中定义的度量值之间存在一对一的匹配关系;每个不同的“计算度量值”都定义为 Command 对象内的 CREATE MEASURE 表达式并且用分号分隔。 表格模型中的所有计算度量值都对应于 MdxScript 对象中的一个命令对象中的集合 CREATE MEASURE 字符串;此外,每个计算度量值都有一个具有 CalculationProperty 的一对一映射。
下面的代码段演示如何创建计算度量值。
private void addCalculatedMeasure(
AMO.Cube modelCube
, string cmTableName
, string cmName
, string newCalculatedMeasureExpression
)
{
//Verify input requirements
if (string.IsNullOrEmpty(cmName) || string.IsNullOrWhiteSpace(cmName))
{
MessageBox.Show(String.Format("Calculated Measure name is not defined."), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(newCalculatedMeasureExpression) || string.IsNullOrWhiteSpace(newCalculatedMeasureExpression))
{
MessageBox.Show(String.Format("Calculated Measure expression is not defined."), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
StringBuilder measuresCommand = new StringBuilder();
AMO.MdxScript mdxScript = modelCube.MdxScripts["MdxScript"];
//ToDo: Verify if measure already exits and ask user what wants to do next
if (mdxScript.Commands.Count == 1)
{
measuresCommand.AppendLine("-------------------------------------------------------------");
measuresCommand.AppendLine("-- Tabular Model measures command (do not modify manually) --");
measuresCommand.AppendLine("-------------------------------------------------------------");
measuresCommand.AppendLine();
measuresCommand.AppendLine();
mdxScript.Commands.Add(new AMO.Command(measuresCommand.ToString()));
}
else
{
measuresCommand.Append(mdxScript.Commands[1].Text);
}
measuresCommand.AppendLine(string.Format("CREATE MEASURE '{0}'[{1}]={2};", cmTableName, cmName, newCalculatedMeasureExpression));
mdxScript.Commands[1].Text = measuresCommand.ToString();
if (!mdxScript.CalculationProperties.Contains(cmName))
{
AMO.CalculationProperty cp = new AMO.CalculationProperty(cmName, AMO.CalculationType.Member);
cp.FormatString = ""; // ToDo: Get formatting attributes for the member
cp.Visible = true;
mdxScript.CalculationProperties.Add(cp);
}
try
{
modelCube.Update(AMO.UpdateOptions.ExpandFull, AMO.UpdateMode.UpdateOrCreate);
MessageBox.Show(String.Format("Calculated Measure successfully defined."), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (AMO.OperationException amoOpXp)
{
MessageBox.Show(String.Format("Calculated Measure expression contains syntax errors, or references undefined or missspelled elements.\nError message: {0}", amoOpXp.Message), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
catch (AMO.AmoException amoXp)
{
MessageBox.Show(String.Format("AMO exception accessing the server.\nError message: {0}", amoXp.Message), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
catch (Exception)
{
throw;
}
}
AMO2Tabular 示例
为了理解如何使用 AMO 创建和操作计算度量值表示形式,请参阅 AMO 到表格示例中的源代码;具体来讲,请查看以下源文件:AddCalculatedMeasure.cs。 该示例在 Codeplex 上提供。 有关该代码的重要说明:提供该代码只是为了支持本文介绍的逻辑概念,不应用于生产环境中;也不应用于除教学之外的其他用途。