简体   繁体   English

使用事实表中的相同列在MDX中为不同度量构造计算度量

[英]Constructing Calculated Measures in MDX for different measures using same columns in a fact table

i have a fact table with 2 columns corresponding to dimensions Dim1, Dim2. 我有一个事实表,其中有2列对应于维度Dim1,Dim2。 In the same table i have 4 other columns Value_Type(int), INT_VALUE(int), FLOAT_VALUE(float), TEXT_VALUE(string). 在同一张表中,我还有其他4个列:Value_Type(int),INT_VALUE(int),FLOAT_VALUE(float),TEXT_VALUE(string)。 There are a number of measures which are identified by Value_Type and depending on their nature could be written in one of the 3 columns (INT_VALUE(int), FLOAT_VALUE(float), TEXT_VALUE(string)) Let's say Measure1 with Measure_Type=1 is age, 2 is account balance and 3 is Name for clarity. 有很多由Value_Type标识的度量,根据它们的性质,可以将其写在3列之一中(INT_VALUE(int),FLOAT_VALUE(float),TEXT_VALUE(string)),假设Measure1和Measure_Type = 1的Measure1是年龄,为清楚起见,2是帐户余额,3是Name。 There could be other measure types that use these 3 same columns for data. 可能还有其他度量类型使用这3个相同的列作为数据。 So the sample fact table looks like this 所以样本事实表如下所示

Dim1    Dim2    Measure_Type      INT_VALUE       FLOAT_VALUE    TEXT_VALUE
10      10      1                 25         
10      10      2                                  2000,34   
10      10      3                                                John
10      20      1                 28         
10      20      2                                  3490,23   
10      20      3                                                Frank

My task is to write an MDX query for each Dim1, Dim2 combination which returns all 3 measures in the same row. 我的任务是为每个Dim1,Dim2组合编写一个MDX查询,该查询将返回同一行中的所有3个小节。 The idea is to construct a calculated member for each Measure that returns value from the right field. 这个想法是为每个从右字段返回值的度量构造一个计算成员。 For example for Measure1 we take INT_VALUE with measure_type=1. 例如,对于Measure1,我们采用INT_VALUE和measure_type = 1。 The problem is i don't know how to construct MDX query for these calculated members. 问题是我不知道如何为这些计算所得的成员构造MDX查询。 Can you please help me? 你能帮我么?

So my final goal is to write an MDX query that returns all measures in one row for each set of Dim1, Dim2 因此,我的最终目标是编写一个MDX查询,以针对每一组Dim1,Dim2返回所有度量。

SELECT [Measure1], [Measure2], [Measure3] ON COLUMNS,
NON EMPTY [Dim1].[Dim1].[Dim1].Members*[Dim2].[Dim2].[Dim2].Members ON ROWS
FROM [Cube]

Dim1    Dim2    Measure1    Measure2    Measure3
10      10      25          2000,34     John
10      20      28          3490,23     Frank

As floats and ints can just be summed, I do not think there is anything special needed for Measure1 and Measure2 . 随着花车和整数正好可以总结,我不觉得有什么特别需要的Measure1Measure2 Assuming that the empty fields in you sample table are null s, you even do not need the measure_type column for anything, as summing across nulls is fine, ie you could collaps your fact table to one third of its size by coalescing the three records for different measure types to one, and omitting the null values. 假设样本表中的空字段为null ,那么您甚至不需要measure_type列,因为对null进行求和就可以了,即,您可以通过合并三个记录以将事实表折叠为它的大小的三分之一将不同的量度类型归一,并省略null值。

Thus, we are left with the aggregation of the string values. 因此,我们剩下的是字符串值的聚合。 As strings cannot be used as physical measures, we must put this column in an attribute and implement the aggregation as a calculated measure. 由于字符串不能用作物理量度,因此我们必须将此列放在属性中,并将汇总实现为计算的量度。 To do this, you can proceed as follows: 为此,您可以按照以下步骤操作:

  • Create a dimension table with just a numeric primary key column and the distinct values from the text value column. 创建仅具有数字主键列和文本值列中不同值的尺寸表。 I would suggest to also add a record with a special text like '<n/a>' to this table, for cases where there is no text_value for a combination of dim1 and dim2. 对于不存在dim1和dim2组合的text_value的情况,我建议向该表中添加一条带有特殊文本的记录,例如'<n/a>' It is generally a good idea to avoid null attribute values and null foreign keys in Analysis Services. 通常,在Analysis Services中避免使用空的属性值和空的外键是一个好主意。
  • Add a foreign key to the fact table referencing this dimension from each record. 向每个记录中引用此维度的事实表添加外键。
  • In BIDS, create the dimension, let's name it text and I am assuming that the attribute is called text value . 在BIDS中,创建维度,将其命名为text然后假设该属性称为text value Set the reference between the measure group and the dimension in Cube Editor. 在多维数据集编辑器中设置度量值组和维度之间的参考。
  • Define a calculated measure for Measure3 with the following expression: 使用以下表达式为Measure3定义计算的度量:

.

Generate( (EXISTING [text].[text value].[text value].members )
                    - { [text].[text value].[<n/a>] } 
          as a,
          a.Current.Name,
          ', '
        )
  • Make the dimension or the attribute invisible. 使尺寸或属性不可见。

Of course, you need not create the dimension table and the foreign keys in the fact table physically.You can as well generate them as views or named query in the Data Source View. 当然,您无需物理地在事实表中创建维度表和外键,也可以在数据源视图中将它们生成为视图或命名查询。

And you can use a different delimiter than the comma and space which I used, this is the third argument to the Generate MDX function . 您可以使用与我使用的逗号和空格不同的定界符,这是Generate MDX函数的第三个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM