MDX - 将行转置为列

MDX - transpose rows to columns

我正在尝试使用 MDX 将两行的详细信息显示为一行。如果我执行下面的 MDX,它 return 有 2 行,一行包含 998 键,另一行包含 999 键

SELECT NON EMPTY { 
  [Measures].[FactTableCount] } ON COLUMNS, 
NON EMPTY { ([DimXXXX].[XXXXKey].[XXXXKey].ALLMEMBERS 
  * ([DimAAAA].[AAAAKey].[AAAAKey],{[DimBBBB].[Key].&[998],[DimBBBB].[Key].&[999]},[DimCCCC].[CCCCKey].[CCCCKey])
  ) } ON ROWS 
FROM ( SELECT ( { [DimXXXX].[XXXXKey].&[MyValue] } ) ON COLUMNS 
FROM [FactTable])

它return是这样的

    (columns [DimXXXX].[XXXXKey], [DimAAAA].[AAAAKey], [DimBBBB].[Key], [DimCCCC].[CCCCKey], [Measures].[FactTableCount])

MyValue, MyAAAAKey1, 998, MyCCCCKey1, 1

MyValue, MyAAAAKey2, 999, MyCCCCKey2, 1

但是我想 return 这样一行

`(columns [DimXXXX].[Key], [DimAAAA].[AAAAKey], [DimAAAA].[AAAAKey], [DimBBBB].[Key], [DimBBBB].[Key], [DimCCCC].[CCCCKey], [DimCCCC].[CCCCKey], [Measures].[FactTableCount])

MyValue, MyAAAAKey1, MyAAAAKey2, 998, 999, MyCCCCKey1, MyCCCCKey2, 1

除其他外(例如使用 SET,将 998/999 逻辑放在 ROWS/COLUMNS 之后,等等)我已经尝试过

SELECT NON EMPTY { 
  [Measures].[FactTableCount] } ON COLUMNS, 
NON EMPTY { ([DimXXXX].[XXXXKey].[XXXXKey].ALLMEMBERS 
  * ([DimAAAA].[AAAAKey].[Key],[DimBBBB].[Key].&[998],[DimCCCC].[CCCCKey].[CCCCKey])
  * ([DimAAAA].[AAAAKey].[Key],[DimBBBB].[Key].&[999],[DimCCCC].[CCCCKey].[CCCCKey])
  ) } ON ROWS 
FROM ( SELECT ( { [DimXXXX].[XXXXKey].&[MyValue] } ) ON COLUMNS 
FROM [FactTable])

...但是因为重复了 AAAAKey 层次结构,所以我收到错误消息 "The AAAAKey hierarchy is used more than once in the Crossjoin function"

有没有办法做到这一点?

根据您在下面的评论,我有一个 sample.Let 我知道它是否有效。

I think I can see what you're saying however the measures are one thing, but the dimension values are another - say Record1:MyValue, MyAAAAKey1, 998, MyCCCCKey1, 2 and Record2: MyValue, MyAAAAKey2, 999, MyCCCCKey2, 5 - I would like to output MyValue, MyAAAAKey1, MyAAAAKey2, 998, 999, MyCCCCKey1, MyCCCCKey2, 2, 5

所以在下面的查询中我试图模拟你的问题。

select
{[Measures].[Internet Sales Amount]}
on columns,
non empty
([Customer].[City].[City],{[Product].[Category].&[1],[Product].[Category].&[3]},[Product].[Subcategory].[Subcategory])
on rows 
from [Adventure Works]

结果

现在唯一的方法是将变化的值带到列中,在我的例子中是“{[Product].[Category].&1,[Product].[Category].&2}”和“{[DimBBBB].[Key].& [998],[DimBBBB].[Key].&[999]}" 在你的例子中

select
{
({[Product].[Category].&[1],[Product].[Category].&[3]},[Measures].[Internet Sales Amount]),
([Product].[Category].defaultmember,[Measures].[Internet Order Quantity])
}
on columns,
non empty
([Customer].[City].[City],[Product].[Subcategory].[Subcategory])
on rows 
from [Adventure Works]

结果:

请注意如何仅针对相关列重复这些值。这确实添加了一个额外的列,但您现在的行数是原始数的一半。

编辑:根据评论处理需求

1st row of the grid would be Ballard, Bikes, Mountain Bikes, Road Bikes. The 2nd: Ballard, Clothing, Caps, Gloves. The 3rd: Barstow, Bikes, Road Bikes, null. I want to merge/list the actual dimension values

因此,要实现上述目标,我们有两种选择。但无论哪种情况,都需要对 UI 进行一些操作。 1)第一个选项

with member 
measures.t 
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(0).item(2).name

member measures.t1
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(1).item(2).name
select
{measures.t,measures.t1}
on columns,
nonempty(([Customer].[City].[City],{[Product].[Category].&[1],[Product].[Category].&[3]}),[Measures].[Internet Sales Amount])
on rows 
from [Adventure Works]

2)第二个选项,

with member 
measures.t1
as 
[Customer].[City].currentmember.name

member measures.t2
as 
[Product].[Category].currentmember.name

member measures.t3 
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(0).item(2).name

member measures.t4
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(1).item(2).name

select
{measures.t1,measures.t2,measures.t3,measures.t4}
on columns,
nonempty(([Customer].[City].[City],{[Product].[Category].&[1],[Product].[Category].&[3]}),[Measures].[Internet Sales Amount])
on rows 
from [Adventure Works]