简体   繁体   English

如何基于过滤后的维度创建计算的度量?

[英]How can I create a calculated measure based on a filtered dimension?

I'm relatively new to MDX (in the process of ordering a book just now) and having a crack at creating a calculated measure, which should sum a value from one of my Fact tables, based on a set range of dates (from my Time dimension) which should be filtered by an attribute from another dimension. 我是MDX的新手(目前正在订购一本书),并且在创建计算量度时会遇到一些困难,该量度应基于设定的日期范围(从我的时间维度),该属性应由另一个维度的属性过滤。 Unfortunately, I seem to have gotten myself in a bit of a situation and can't see a clear way out. 不幸的是,我似乎陷入了某种困境,看不到明确的出路。 I looked through other similar questions but didn't see any trying to do this same thing, only single dimension measures. 我浏览了其他类似的问题,但没有发现尝试做同一件事的任何尝试,只是单维度测量。

I have a Measure Group, which contains the physical measure SourceMeasure . 我有一个度量值组,其中包含物理度量SourceMeasure

I have a Time dimension, named [Time] , which I want to use to specify my range for the calculation. 我有一个名为[Time]的Time维度,我想使用它指定我的计算范围。 This is related to the above measure group by my Date Key. 这与我的“日期密钥”与上述度量标准组有关。

I have an Account dimension, named [Dim Account] , which contains two date attributes: [Account Start Date] and [Account End Date] . 我有一个名为[Dim Account]的Account维度,其中包含两个日期属性: [Account Start Date][Account End Date] This is related to the above measure group via an Account ID. 这通过帐户ID与上述度量值组相关。

What I'm trying to do, is filter the [Time] dimension to a range based on the [Account Start Date] and [Account End Date] , and return the sum of [SourceMeasure] for the period specified for that account. 我想做的是,根据[Account Start Date][Account End Date][Time]维度过滤到一个范围,然后为该帐户指定的时间段返回[SourceMeasure]的总和。

Obviously, this will be different for each account, so shouldn't be aggregated, except by the [Dim Account] dimension (perhaps returning 0 or null if not applied). 显然,这对每个帐户来说都是不同的,因此,除[Dim Account]维(除非返回未使用的值,可能返回0或null)外,不应该进行汇总。

Example test expression below, this is currently not working due to an error with the FORMAT_STRING syntax (according to MDX Studio). 以下示例测试表达式,由于FORMAT_STRING语法错误(根据MDX Studio),当前无法使用。

WITH 
  MEMBER [Measures].[LifetimeMeasure] AS 
    Sum
    (
      {
          StrToMember
          (
        "[Time].[Date].&[" 
            + Format([Dim Account].[Account Start Date].CurrentMember.MemberValue,"yyyy-MM-ddThh:mm:ss")
            + "]"
          )
        : 
          StrToMember
          (
            "[Time].[Date].&[" 
            + Format([Dim Account].[Account End Date].CurrentMember.MemberValue,"yyyy-MM-ddThh:mm:ss")
            + "]"
          )
      }
     ,[Measures].[SourceMeasure]
    ) 
SELECT 
{
    [Measures].[LifetimeMeasure]
    ,[Measures].[SourceMeasure]
}
ON COLUMNS
,{
    [Dim Account].[Account].[AccountName]
} 
ON ROWS
FROM 
    [MyCube]

I've tried a few different approaches (SCOPE, Filter, IIF) but every time I seem to get back to the same sticking point - how to filter the [Time] dimension based on the value of the [Dim Account] start and end dates. 我尝试了几种不同的方法(SCOPE,过滤器,IIF),但是每次我似乎回到相同的症结时,如何根据[Dim Account]开始和结束的值过滤[Time]维度日期。

Now, it may be that I'm completely misunderstanding how the relationships work between the dimensions and/or how calculated measures are computed by SSAS but I'm not yet at a level where I'm sure what to look for, so I'm going round in circles. 现在,可能是我完全误解了维度之间的关系如何工作和/或SSAS如何计算计算的度量,但是我还没有确定要寻找的水平,所以我我转了一圈。

Is what I'm trying to do possible, and if so, any pointers as to where to look to help me figure out where I'm going wrong? 我正在尝试做的事情是否可能,如果可能,是否有任何指向哪里的指针可以帮助我弄清楚我要去哪里错了? Should I perhaps be looking to use a Calculation Dimension instead? 我是否应该改为使用“计算维数”?

I hope this all makes sense, let me know if I've missed anything or if more detail is required. 我希望这一切都是有道理的,如果我错过了任何事情或需要更多详细信息,请告诉我。 I'm testing using MDX Studio . 我正在使用MDX Studio进行测试。

I found the error by adding the CONSTRAINED flag to the StrToMember function. 我通过向StrToMember函数添加CONSTRAINED标志来发现错误。

It seems that my [Time] dimension's date entries have defaulted to the time "00:00:00" while my [Dim Account] dimension's date entries have defaulted to "12:00:00". 似乎我的[时间]维度的日期条目默认为时间“ 00:00:00”,而我的[假账户]维度的日期条目默认为“ 12:00:00”。 It looks like a mismatch between 24-hour and 12-hour clock. 看来24小时制和12小时制之间不匹配。 My date fields on the [Dim Account] dimension are marked as "Regular", not "Date" due to the fact that the dimension isn't a natural time dimension. 由于该维度不是自然时间维度,因此[Dim Account]维度上的我的日期字段被标记为“常规”,而不是“日期”。 I think it's potentially this that's causing the mismatch. 我认为这可能是导致不匹配的原因。

To workaround the issue for now, I've removed the Time format from my Format expression and just appended the "00:00:00" to the string while I search for the root cause. 要暂时解决此问题,我从“格式”表达式中删除了“时间”格式,并在搜索根本原因时将“ 00:00:00”附加到了字符串上。 I'll update once I've solved it. 解决后,我会更新。

UPDATE: The cause of the mismatch is not in the dimension, but in the format string I've used in the StrToMember expression. 更新:不匹配的原因不在维度上,而是在我在StrToMember表达式中使用的格式字符串中。 It's not documented on MSDN, but the following string outputs 12-hour time format: 它没有在MSDN上记录,但是以下字符串输出12小时时间格式:

"yyyy-mm-ddThh:mm:ss"

While this string outputs 24-hour time format: 虽然此字符串输出24小时时间格式:

"yyyy-mm-ddTHH:mm:ss"

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

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