计算列表示形式(表格)
计算列是在表中创建新列并且获取的值存储于该表中的 DAX 表达式。 每次处理表时,都会对计算列表达式进行计算。
计算列表示形式
AMO 中的计算列
在使用 AMO 管理表格模型表时,对于 AMO 中的计算列没有一对一的对象匹配。 计算列由 Dimension 中的属性以及 MeasureGroup 中的属性表示。
下面的代码段演示如何向现有表格模型添加计算列。 代码中假定您具有 AMO 数据库对象 newDatabase 和 AMO 多维数据集对象 modelCube。
private void addCalculatedColumn(
AMO.Database newDatabase
, AMO.Cube modelCube
, String ccTableName
, String ccName
, String newCalculatedColumnExpression
)
{
if (string.IsNullOrEmpty(ccName) || string.IsNullOrWhiteSpace(ccName))
{
MessageBox.Show(String.Format("Calculated Column name is not defined."), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(newCalculatedColumnExpression) || string.IsNullOrWhiteSpace(newCalculatedColumnExpression))
{
MessageBox.Show(String.Format("Calculated Column expression is not defined."), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (newDatabase.Dimensions[ccTableName].Attributes.Contains(ccName))
{
MessageBox.Show(String.Format("Calculated Column name already defined."), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//Add CC attribute to the Dimension
AMO.Dimension dim = newDatabase.Dimensions[ccTableName];
AMO.DimensionAttribute currentAttribute = dim.Attributes.Add(ccName, ccName);
currentAttribute.Usage = AMO.AttributeUsage.Regular;
currentAttribute.KeyUniquenessGuarantee = false;
currentAttribute.KeyColumns.Add(new AMO.DataItem(ccTableName, ccName, OleDbType.Empty));
currentAttribute.KeyColumns[0].Source = new AMO.ExpressionBinding(newCalculatedColumnExpression);
currentAttribute.KeyColumns[0].NullProcessing = AMO.NullProcessing.Preserve;
currentAttribute.NameColumn = new AMO.DataItem(ccTableName, ccName, System.Data.OleDb.OleDbType.WChar);
currentAttribute.NameColumn.Source = new AMO.ExpressionBinding(newCalculatedColumnExpression);
currentAttribute.NameColumn.NullProcessing = AMO.NullProcessing.ZeroOrBlank;
currentAttribute.OrderBy = AMO.OrderBy.Key;
AMO.AttributeRelationship currentAttributeRelationship = dim.Attributes["RowNumber"].AttributeRelationships.Add(currentAttribute.ID);
currentAttributeRelationship.Cardinality = AMO.Cardinality.Many;
currentAttributeRelationship.OverrideBehavior = AMO.OverrideBehavior.None;
//Add CC as attribute to the MG
AMO.MeasureGroup mg = modelCube.MeasureGroups[ccTableName];
AMO.DegenerateMeasureGroupDimension currentMGDim = (AMO.DegenerateMeasureGroupDimension)mg.Dimensions[ccTableName];
AMO.MeasureGroupAttribute mga = new AMO.MeasureGroupAttribute(ccName);
mga.KeyColumns.Add(new AMO.DataItem(ccTableName, ccName, OleDbType.Empty));
mga.KeyColumns[0].Source = new AMO.ExpressionBinding(newCalculatedColumnExpression);
currentMGDim.Attributes.Add(mga);
try
{
//Update Dimension, CubeDimension and MG in server
newDatabase.Update(AMO.UpdateOptions.ExpandFull, AMO.UpdateMode.UpdateOrCreate);
}
catch (AMO.OperationException amoOpXp)
{
MessageBox.Show(String.Format("Calculated Column 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;
}
}