如何根据 SSAS MDX 中另一个度量的值检索度量值?
How do I retrieve a measure value based on the value of another measure in SSAS MDX?
我有一个复杂的 MDX 计算度量问题。我有两个度量和一个小时-分钟层次结构。在每小时级别,MeasureA 汇总到基础分钟值的最大值。 MeasureB 应该汇总到 MeasureA 的 Max 的相应值。在下面的示例中,8 是 MeasureA 的最大值。因此,MeasureB 应该按小时累计到 2。我在计算 MeasureB 时遇到问题。我有什么想法可以在 MDX 计算的度量中做到这一点吗?
MeasureA Measure B
Hour 8 2
Minute1 2 3
Minute2 1 2
Minute3 5 1
Minute4 6 3
Minute5 8 2
除上述之外....我有两个层次结构 "Year-Month-Day" 和 "Hour-Minute"。这非常适用于小时-分钟层次结构。但是,当我还希望相同的逻辑从小时到天滚动时,即使它们位于不同的层次结构中。我有下面的工作 hour/minute 键和非工作日键的代码。你有没有看到我做错了什么。
测量 A = "Avg Used Resources" 和测量 B = "Total Resources"
// Total Resources Calculation
---Initializing keys
CREATE MEMBER CURRENTCUBE.[Measures].[Avg Used Resources Key Minutes]
AS null,
VISIBLE = 1, ASSOCIATED_MEASURE_GROUP = 'NSS Resource Utilization';
CREATE MEMBER CURRENTCUBE.[Measures].[Avg Used Resources Key Hours]
AS null,
VISIBLE = 1, ASSOCIATED_MEASURE_GROUP = 'NSS Resource Utilization';
CREATE MEMBER CURRENTCUBE.[Measures].[Avg Used Resources Key Days]
AS null,
VISIBLE = 1, ASSOCIATED_MEASURE_GROUP = 'NSS Resource Utilization';
---
-- Minute Key
SCOPE ([Time].[Hour-Minute].[Time Name],[Measures].[Avg Used Resources Key Minutes]);
THIS = IIF(([Time].[Hour-Minute].CurrentMember,[Measures].[Avg Used Resources])
= ([Time].[Hour-Minute].CurrentMember.Parent,[Measures]. [Avg Used Resources])
, [Time].[Hour-Minute].CurrentMember.Member_Key, null);
END SCOPE;
-- Hour Key
SCOPE ([Measures].[Avg Used Resources Key Hours]);
THIS = MAX([Time].[Hour-Minute].CurrentMember.Children,[Measures].[Avg Used Resources Key Minutes]);
END SCOPE;
-- Day Key
SCOPE ([Measures].[Avg Used Resources Key Days]);
THIS = IIF(([Time].[Hour-Minute].CurrentMember,[Measures].[Avg Used Resources])
= ([Time].[Hour-Minute].CurrentMember.Parent, [Measures].[Avg Used Resources])
, [Time].[Hour-Minute].CurrentMember.Member_Key, null);
END SCOPE;
-- Hour Value
SCOPE ([Time].[Hour-Minute].[Hour 24],[Measures].[Total Resources]);
THIS = (StrToMember("[Time].[Hour-Minute].[Time Name].&["+CStr([Measures]. [Avg Used Resources Key Hours])+"]")
,[Measures].[Total Resources]);
END SCOPE;
--Date Value
SCOPE ([Time].[Hour-Minute].[ALL],[Measures].[Total Resources]);
THIS = (StrToMember("[Time].[Hour-Minute].[Hour 24].&["+CStr([Measures].[Avg Used Resources Key Days])+"]")
,[Measures].[Total Resources]);
END SCOPE;
我有一个非常愚蠢的解决方案,但它有效!主要思想是将必要分钟成员的密钥发送到小时级别。
创建额外的空度量来存储最大分钟值的键:
CREATE MEMBER CURRENTCUBE.[Measures].[Measure A Key Minutes]
AS null,
VISIBLE = 0;
CREATE MEMBER CURRENTCUBE.[Measures].[Measure A Key Hours]
AS null,
VISIBLE = 0;
比添加范围来计算它们:
SCOPE ([Date].[Date].[Minutes],[Measures].[Measure A Key Minutes]);
THIS = IIF(([Date].[Date].CurrentMember,[Measures].[Measure A]) = ([Date].[Date].CurrentMember.Parent,[Measures].[Measure A])
, [Date].[Date].CurrentMember.Member_Key, null);
END SCOPE;
SCOPE ([Measures].[Measure A Key Hours]);
THIS = MAX([Date].[Date].CurrentMember.Children,[Measures].[Measure A Key Minutes]);
END SCOPE;
如果分钟级别的值等于小时级别,则第一个范围将成员的键放入新度量。第二个将此值移动到小时级别。现在我们在小时级别上有最大分钟值的键。
然后添加最后一个范围来更新措施 B:
SCOPE ([Date].[Date].[Hours],[Measures].[Measure B]);
THIS = (StrToMember("[Date].[Date].&["+CStr([Measures].[Measure A Key Hours])+"]")
,[Measures].[Measure B]);
END SCOPE;
对于大维度来说,这是绝对缓慢的解决方案(服务器肯定会切换到逐个单元模式!),它是严格而严格的,但是......它有效。刚刚测试。
欢迎任何关于如何加速的补充。但最好还是使用另一种方法。
更新:(重新编辑,因为错误)
不幸的是,没有简单的方法来添加新级别。但你可以这样做:
- 为新的 HOUR-DAY 关系添加另外两个度量。
- 首先将从每个 HOUR 成员中找到最高键。
- 第二个会将其移至 DAY 级别。
- 再次使用 StrToMember 添加作用域,但持续数天。
不幸的是,这是唯一可用的解决方案。
所以第一个度量就像 "Minute Key on Minutes Level",第二个是 "Minute Key on Hours Level"。比您需要添加 "Hour Key on Hours level" 和 "Hour Key on Days Level" 等度量,以及天级别的另一个范围。
CREATE MEMBER CURRENTCUBE.[Measures].[Measure A Key Hour - Hour Level]
AS null,
VISIBLE = 0;
CREATE MEMBER CURRENTCUBE.[Measures].[Measure A Key Hour - Day Level]
AS null,
VISIBLE = 0;
SCOPE ([Date].[Date].[Hour],[Measures].[Measure A Key Hour - Hour Level]);
THIS = IIF(([Date].[Date].CurrentMember,[Measures].[Measure A]) = ([Date].[Date].CurrentMember.Parent,[Measures].[Measure A])
, [Date].[Date].CurrentMember.Member_Key, null);
END SCOPE;
SCOPE ([Date].[Date].[Day],[Measures].[Measure A Key Hour - Day Level]);
THIS = MAX([Date].[Date].CurrentMember.Children,[Measures].[Measure A Key Hour - Hour Level]);
END SCOPE;
SCOPE ([Date].[Date].[Day],[Measures].[Measure B]);
THIS = (StrToMember("[Date].[Date].&["+CStr([Measures].[Measure A Key Hour - Day Level])+"]")
,[Measures].[Measure B]);
END SCOPE;
我相信您的第一个度量的逻辑是连续几天每小时的 MAX 或类似的东西。
我有一个复杂的 MDX 计算度量问题。我有两个度量和一个小时-分钟层次结构。在每小时级别,MeasureA 汇总到基础分钟值的最大值。 MeasureB 应该汇总到 MeasureA 的 Max 的相应值。在下面的示例中,8 是 MeasureA 的最大值。因此,MeasureB 应该按小时累计到 2。我在计算 MeasureB 时遇到问题。我有什么想法可以在 MDX 计算的度量中做到这一点吗?
MeasureA Measure B
Hour 8 2
Minute1 2 3
Minute2 1 2
Minute3 5 1
Minute4 6 3
Minute5 8 2
除上述之外....我有两个层次结构 "Year-Month-Day" 和 "Hour-Minute"。这非常适用于小时-分钟层次结构。但是,当我还希望相同的逻辑从小时到天滚动时,即使它们位于不同的层次结构中。我有下面的工作 hour/minute 键和非工作日键的代码。你有没有看到我做错了什么。
测量 A = "Avg Used Resources" 和测量 B = "Total Resources"
// Total Resources Calculation
---Initializing keys
CREATE MEMBER CURRENTCUBE.[Measures].[Avg Used Resources Key Minutes]
AS null,
VISIBLE = 1, ASSOCIATED_MEASURE_GROUP = 'NSS Resource Utilization';
CREATE MEMBER CURRENTCUBE.[Measures].[Avg Used Resources Key Hours]
AS null,
VISIBLE = 1, ASSOCIATED_MEASURE_GROUP = 'NSS Resource Utilization';
CREATE MEMBER CURRENTCUBE.[Measures].[Avg Used Resources Key Days]
AS null,
VISIBLE = 1, ASSOCIATED_MEASURE_GROUP = 'NSS Resource Utilization';
---
-- Minute Key
SCOPE ([Time].[Hour-Minute].[Time Name],[Measures].[Avg Used Resources Key Minutes]);
THIS = IIF(([Time].[Hour-Minute].CurrentMember,[Measures].[Avg Used Resources])
= ([Time].[Hour-Minute].CurrentMember.Parent,[Measures]. [Avg Used Resources])
, [Time].[Hour-Minute].CurrentMember.Member_Key, null);
END SCOPE;
-- Hour Key
SCOPE ([Measures].[Avg Used Resources Key Hours]);
THIS = MAX([Time].[Hour-Minute].CurrentMember.Children,[Measures].[Avg Used Resources Key Minutes]);
END SCOPE;
-- Day Key
SCOPE ([Measures].[Avg Used Resources Key Days]);
THIS = IIF(([Time].[Hour-Minute].CurrentMember,[Measures].[Avg Used Resources])
= ([Time].[Hour-Minute].CurrentMember.Parent, [Measures].[Avg Used Resources])
, [Time].[Hour-Minute].CurrentMember.Member_Key, null);
END SCOPE;
-- Hour Value
SCOPE ([Time].[Hour-Minute].[Hour 24],[Measures].[Total Resources]);
THIS = (StrToMember("[Time].[Hour-Minute].[Time Name].&["+CStr([Measures]. [Avg Used Resources Key Hours])+"]")
,[Measures].[Total Resources]);
END SCOPE;
--Date Value
SCOPE ([Time].[Hour-Minute].[ALL],[Measures].[Total Resources]);
THIS = (StrToMember("[Time].[Hour-Minute].[Hour 24].&["+CStr([Measures].[Avg Used Resources Key Days])+"]")
,[Measures].[Total Resources]);
END SCOPE;
我有一个非常愚蠢的解决方案,但它有效!主要思想是将必要分钟成员的密钥发送到小时级别。
创建额外的空度量来存储最大分钟值的键:
CREATE MEMBER CURRENTCUBE.[Measures].[Measure A Key Minutes]
AS null,
VISIBLE = 0;
CREATE MEMBER CURRENTCUBE.[Measures].[Measure A Key Hours]
AS null,
VISIBLE = 0;
比添加范围来计算它们:
SCOPE ([Date].[Date].[Minutes],[Measures].[Measure A Key Minutes]);
THIS = IIF(([Date].[Date].CurrentMember,[Measures].[Measure A]) = ([Date].[Date].CurrentMember.Parent,[Measures].[Measure A])
, [Date].[Date].CurrentMember.Member_Key, null);
END SCOPE;
SCOPE ([Measures].[Measure A Key Hours]);
THIS = MAX([Date].[Date].CurrentMember.Children,[Measures].[Measure A Key Minutes]);
END SCOPE;
如果分钟级别的值等于小时级别,则第一个范围将成员的键放入新度量。第二个将此值移动到小时级别。现在我们在小时级别上有最大分钟值的键。
然后添加最后一个范围来更新措施 B:
SCOPE ([Date].[Date].[Hours],[Measures].[Measure B]);
THIS = (StrToMember("[Date].[Date].&["+CStr([Measures].[Measure A Key Hours])+"]")
,[Measures].[Measure B]);
END SCOPE;
对于大维度来说,这是绝对缓慢的解决方案(服务器肯定会切换到逐个单元模式!),它是严格而严格的,但是......它有效。刚刚测试。
欢迎任何关于如何加速的补充。但最好还是使用另一种方法。
更新:(重新编辑,因为错误) 不幸的是,没有简单的方法来添加新级别。但你可以这样做:
- 为新的 HOUR-DAY 关系添加另外两个度量。
- 首先将从每个 HOUR 成员中找到最高键。
- 第二个会将其移至 DAY 级别。
- 再次使用 StrToMember 添加作用域,但持续数天。
不幸的是,这是唯一可用的解决方案。
所以第一个度量就像 "Minute Key on Minutes Level",第二个是 "Minute Key on Hours Level"。比您需要添加 "Hour Key on Hours level" 和 "Hour Key on Days Level" 等度量,以及天级别的另一个范围。
CREATE MEMBER CURRENTCUBE.[Measures].[Measure A Key Hour - Hour Level]
AS null,
VISIBLE = 0;
CREATE MEMBER CURRENTCUBE.[Measures].[Measure A Key Hour - Day Level]
AS null,
VISIBLE = 0;
SCOPE ([Date].[Date].[Hour],[Measures].[Measure A Key Hour - Hour Level]);
THIS = IIF(([Date].[Date].CurrentMember,[Measures].[Measure A]) = ([Date].[Date].CurrentMember.Parent,[Measures].[Measure A])
, [Date].[Date].CurrentMember.Member_Key, null);
END SCOPE;
SCOPE ([Date].[Date].[Day],[Measures].[Measure A Key Hour - Day Level]);
THIS = MAX([Date].[Date].CurrentMember.Children,[Measures].[Measure A Key Hour - Hour Level]);
END SCOPE;
SCOPE ([Date].[Date].[Day],[Measures].[Measure B]);
THIS = (StrToMember("[Date].[Date].&["+CStr([Measures].[Measure A Key Hour - Day Level])+"]")
,[Measures].[Measure B]);
END SCOPE;
我相信您的第一个度量的逻辑是连续几天每小时的 MAX 或类似的东西。