MDX - 从另一个日期维度计算一个日期维度

发布于 2024-12-22 17:06:54 字数 1429 浏览 2 评论 0原文

我有一个事实表,其中有 2 个日期发票日期会计当前日期。为了获得请求的收入值,我需要使用这两个日期的组合。例如,如果我需要 YTD Revenue,我需要像这样选择它:

(注意:我正在编写 SQL 查询,因为我更熟悉它)

SELECT Revenue
FROM
Fact_Revenue
WHERE
Invoice_Date <= '2011-10-22'
and AccountingCurrent >= '2011-01'
and AccountingCurrent <= '2011-10'

除了 Revenue 之外,该事实表还包含我需要的其他信息也需要,但为了计算其他数据,我不需要会计当前日期。所以我的想法是在主 MDX 查询中仅使用 1 个日期(发票日期)(以便我可以通过 1 个查询获取尽可能多的数据),并且为了计算收入,我想使用计算会员,我想将会计当前日期与选定的发票日期关联起来。

例如

SELECT {[Measure].[RevenueYTD], 
[Measure].[RevenueMTD], 
[Measure].[NumberOfInvoices], 
[Measure].[NumberOfPolicies]} 
ON COLUMNS, 
{[People].Members} ON ROWS 
FROM [Cube]
WHERE
[Invoice Date].[Date Hierarchy].[Date].&[2011-10-22]

,在本例中,[Measure].[RevenueYTD][Measure].[RevenueMTD] 需要受到 会计当前日期 的限制并且发票日期必须低于查询的日期。另一方面,我需要 [Measure].[NumberOfInvoices][Measure].[NumberOfPolicies] 来获取特定发票日期(或 MTD 日期,等等),但没有涉及 Accounting Current Date

计算的成员查询应该执行类似这样的操作(这更像是算法):

ROUND(
    SUM(
        YTD([Accounting Current Date].[Date Hierarchy].CurrentMember), 
        [Measures].[Revenue]
        ),
    2)
WHERE [Invoice Current Date].[Date Hierarchy] < [Invoice Current Date].[Date Hierarchy].CurrentMember

I have a fact table that has 2 dates Invoice Date and Accounting Current Date. In order to get requested Revenue value I need to use combination of these two dates. For example, if I need YTD Revenue I need to select it like this:

(Note: I am writing SQL query because I am more familiar with it)

SELECT Revenue
FROM
Fact_Revenue
WHERE
Invoice_Date <= '2011-10-22'
and AccountingCurrent >= '2011-01'
and AccountingCurrent <= '2011-10'

Besides Revenue, this fact tables has other information that I also need, but for calculating this other data I don't need Accounting Current Date. So my idea is to use only 1 date (Invoice Date) in main MDX query (so that I can grab as many data with 1 query as I can) and for calculating Revenue I would like to use Calculated Member and in there I would like to associate Accounting Current Date with selected Invoice Date.

For example

SELECT {[Measure].[RevenueYTD], 
[Measure].[RevenueMTD], 
[Measure].[NumberOfInvoices], 
[Measure].[NumberOfPolicies]} 
ON COLUMNS, 
{[People].Members} ON ROWS 
FROM [Cube]
WHERE
[Invoice Date].[Date Hierarchy].[Date].&[2011-10-22]

In this case, [Measure].[RevenueYTD] and [Measure].[RevenueMTD] need to be limited by Accounting Current Date and Invoice Date must be lower than the date from the query. On the other hand, I need [Measure].[NumberOfInvoices] and [Measure].[NumberOfPolicies] for particual Invoice Date (or MTD Date, whatever), but without involvemenet of Accounting Current Date

Calculated member query should do something like this (this is more like algorithm):

ROUND(
    SUM(
        YTD([Accounting Current Date].[Date Hierarchy].CurrentMember), 
        [Measures].[Revenue]
        ),
    2)
WHERE [Invoice Current Date].[Date Hierarchy] < [Invoice Current Date].[Date Hierarchy].CurrentMember

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏别ゝ 2024-12-29 17:06:54

在 MDX 中,从一个维度导航到另一个维度并不是一件小事。理论上维度是独立的,因此标准语言缺少执行此操作的功能。您可以使用 StrToMember MDX 函数,但它很慢而且有点奇怪。

对于您的过滤器,让我们从第一个开始:

Invoice_Date <= '2011-10-22'

在 MDX 中,我们必须创建一个包含与表达式匹配的成员的集合。这可以使用 Range set 运算符来完成:

NULL:[Invoice Date].[Date Hierarchy].[Date].&[2011-10-22]

另一个过滤器很容易猜测:

AccountingCurrent >= '2011-01' and AccountingCurrent <= '2011-10'

MDX 版本:

[Accounting Date].[Date Hierarchy].[Date].&[2011-01-31]:[Accounting Date].[Date Hierarchy].[Date].&[2011-10-30]

也可以使用 过滤MDX功能如果您需要不同类型的过滤器。

现在我们需要分块并构建查询。一种可能的解决方案是使用设置切片器并在您不希望应用过滤器时覆盖这些值:

 WITH 
   // here we're changing the 'selection' from the where clause
   MEMBER [Measure].[NumberOfInvoices II] AS ([Accounting Date].[Date Hierarchy].defaultmember,[Measure].[NumberOfInvoices])
 SELECT
   .. axis here [Measure].[RevenueYTD] will be applying the filters defined in the where clause
 FROM MyCube
 WHERE {[Accounting Date].[Date Hierarchy].[Date].&[2011-01-31]:[Accounting Date].[Date Hierarchy].[Date].&[2011-10-30]}

Navigating from one dimension to another is not something trivial in MDX. In theory dimensions are independent so standard language is missing functions for doing this. You can use StrToMember MDX function but it's slow and a bit strange.

For your filters, let's start with the first one :

Invoice_Date <= '2011-10-22'

In MDX we'll have to create a set with the members matching the expression. This can be done using the Range set operator :

NULL:[Invoice Date].[Date Hierarchy].[Date].&[2011-10-22]

The other filter is easy to guess :

AccountingCurrent >= '2011-01' and AccountingCurrent <= '2011-10'

MDX version :

[Accounting Date].[Date Hierarchy].[Date].&[2011-01-31]:[Accounting Date].[Date Hierarchy].[Date].&[2011-10-30]

It's also possible using Filter MDX function if your need different type of filters.

Now we need to take the pieces and build the query. One possible solution is using a set slicer and overwritting the values when you don't want the filter to be applied :

 WITH 
   // here we're changing the 'selection' from the where clause
   MEMBER [Measure].[NumberOfInvoices II] AS ([Accounting Date].[Date Hierarchy].defaultmember,[Measure].[NumberOfInvoices])
 SELECT
   .. axis here [Measure].[RevenueYTD] will be applying the filters defined in the where clause
 FROM MyCube
 WHERE {[Accounting Date].[Date Hierarchy].[Date].&[2011-01-31]:[Accounting Date].[Date Hierarchy].[Date].&[2011-10-30]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文