按自然顺序对层次结构成员排序

Order hierarchy members by natural order

我有一个具有以下尺寸的 SSAS 多维数据集:

Time
    DayName <- Hierarchy

Item
    Description <- Hierarchy

Measures 维度中,我有一个 LostAmount 的度量。

我想查询立方体,这样我就可以得到每周损失金额的总和。以下查询有效:

select
    except(Time.DayName.members, {Time.DayName.[All]}) on 0,
    except(Item.Description.members, {Item.Description.All}) on 1
from Cube

但是当返回结果时,列的顺序不是工作日的自然顺序(即列的顺序是 Friday, Monday, Sunday etc)。

如何让结果列按工作日的自然顺序排列,即 Monday, Tuesday etc.

如果您查看屏幕截图,Time.DayName 级别 中的日期 是按自然顺序排序的:这是成员姓名的字母顺序。

SSAS 无法 "know" 您想要的顺序。我建议向该维度添加一个 Key 列,按您想要的顺序对天数进行编号,并将维度属性的 OrderBy 属性 更改为 Key 而不是 Name。 (也许您已经有了一个键:但是 OrderBy=Name 自然会按成员的名字对成员进行排序)。

补充:SSAS中有一个维度属性Type属性,可以设置为"Day of Week"——不过我没用过,并且不知道它是否会正确检测到您的成员名称是工作日的名称。或者它是否会从您想要开始一周的 "right" 天开始安排一周。您可以尝试设置它,看看会发生什么。

虽然改变立方体结构肯定更好,但有时这不是一个可行的选择。在这种情况下,它必须在 MDX 代码中处理(速度较慢)。下面是一种方法。

WITH 
  MEMBER measures.weekdaynumber AS  
    CASE Time.DayName.CURRENTMEMBER 
        WHEN "Monday" THEN 1
        WHEN "Tuesday" THEN 2
        WHEN "Wednesday" THEN 3
        WHEN "Thursday" THEN 4
        WHEN "Friday" THEN 5
        WHEN "Saturday" THEN 6
        WHEN "Sunday" THEN 7
    END

SELECT 
  Order
  (
    Except
    (
      Time.DayName.MEMBERS
     ,{Time.DayName.[All]}
    )
   ,Measures.weekdaynumber
  ) ON 0
 ,Except
  (
    [Item].[Description].MEMBERS
   ,{[Item].[Description].[All]}
  ) ON 1
FROM [Cube];

我找到了解决排序问题的方法,方法是在 Measures 维度上定义其他成员并按所需顺序输出它们:

with
member Measures.Monday as 
    sum(Time.DayName.&[Monday], Measures.LostValue)
member Measures.Tuesday as
    sum(Time.DayName.&[Tuesday], Measures.LostValue)
member Measures.Wednesday as
    sum(Time.DayName.&[Wednesday], Measures.LostValue)
member Measures.Thursday as 
    sum(Time.DayName.&[Thursday], Measures.LostValue)
member Measures.Friday as
    sum(Time.DayName.&[Friday], Measures.LostValue)
member Measures.Saturday as
    sum(Time.DayName.&[Saturday], Measures.LostValue)
member Measures.Sunday as
    sum(Time.DayName.&[Sunday], Measures.LostValue)
select
{
    Measures.Monday,
    Measures.Tuesday,
    Measures.Wednesday,
    Measures.Thursday,
    Measures.Friday,
    Measures.Saturday,
    Measures.Sunday
} on 0,
except(Item.Description.members, {Item.Description.[All]}) on 1
from [Cube]