我有以下MDX查询:
WITH
member [Measures].[USD Sales YTD] as (
SUM( {[Invoice Date].[Calendar].[Month].&[201501]:[Invoice Date].[Calendar].[Month].&[201506]},[Measures].[USD Sales] ))
member [Measures].[USD Sales LY YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201401]:[Invoice Date].[Calendar].[Month].&[201406]},[Measures].[USD Sales]))
member [Measures].[Unit Sales YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201501]:[Invoice Date].[Calendar].[Month].&[201506]},[Measures].[Unit Sales]))
member [Measures].[Unit Sales LY YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201401]:[Invoice Date].[Calendar].[Month].&[201406]},[Measures].[Unit Sales]))
SELECT
NON EMPTY { [Measures].[USD Sales YTD]
, [Measures].[USD Sales LY YTD]
, [Measures].[Unit Sales YTD]
, [Measures].[Unit Sales LY YTD]
} ON COLUMNS
, NON EMPTY {
(
[Invoice Date].[Calendar].[Month]
* [Customer].[Customer Name].[Customer Name]
* [Part].[Part #].[Part #]
* [Sales Rep].[Sales Reps].[Sales Rep]
) } DIMENSION PROPERTIES [Part].[Part #].[Part Desc], MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM ( SELECT ( [Sales Rep].[Sales Reps].[Sales Rep].&[12]) ON COLUMNS
FROM ( SELECT ( [Invoice Date].[Calendar].[Month].&[201506]) ON COLUMNS
FROM [AlphaBI])
)
WHERE ( {[Part].[Sales Unit].&[B], [Part].[Sales Unit].&[M]})
工作正常。它给了我月,客户,产品,推销员和销售年初至今,销售额年初至今,单位年初至今和单位LYDT。
最近,用户要求我更改WHERE条件,说明它的销售额是否超过使用销售单位 B & M (就像现在一样),但如果它是单位,那么只需 B 。你知道我怎么做吗?在SQL中它会很容易,但我的MDX技能有点生疏。任何帮助将不胜感激。
谢谢, 亚历
答案 0 :(得分:1)
只需在WHERE
子句中添加一个小条件,就像使用IIF
WHERE (
{
[Part].[Sales Unit].&[B],
IIF(@param = "sales", [Part].[Sales Unit].&[M], null)
}
)
如果所选参数为“sales”,则会选择成员[Part].[Sales Unit].&[B]
和[Part].[Sales Unit].&[M]
,否则仅选择[Part].[Sales Unit].&[B]
如果你想更进一步,再添加一个级别的检查,比如选择“无”,它应该添加销售单位“A”,然后使用嵌套的IIF
语句,如此 -
IIF(@param = "sales", [Part].[Sales Unit].&[M], IIF(@param = "units", null, [Part].[Sales Unit].&[A])
假设不需要额外的检查,代码应该看起来像 -
WITH
MEMBER [Measures].[USD Sales YTD] AS
SUM(
{
[Invoice Date].[Calendar].[Month].&[201501]
:
[Invoice Date].[Calendar].[Month].&[201506]
},
[Measures].[USD Sales]
)
MEMBER [Measures].[USD Sales LY YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201401]
:
[Invoice Date].[Calendar].[Month].&[201406]
},
[Measures].[USD Sales]
)
MEMBER [Measures].[Unit Sales YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201501]
:
[Invoice Date].[Calendar].[Month].&[201506]
},
[Measures].[Unit Sales]
)
MEMBER [Measures].[Unit Sales LY YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201401]
:
[Invoice Date].[Calendar].[Month].&[201406]
},
[Measures].[Unit Sales]
)
SELECT
NON EMPTY
{
[Measures].[USD Sales YTD],
[Measures].[USD Sales LY YTD],
[Measures].[Unit Sales YTD],
[Measures].[Unit Sales LY YTD]
} ON COLUMNS,
NON EMPTY
{
(
[Invoice Date].[Calendar].[Month] *
[Customer].[Customer Name].[Customer Name] *
[Part].[Part #].[Part #] *
[Sales Rep].[Sales Reps].[Sales Rep]
)
} DIMENSION PROPERTIES [Part].[Part #].[Part Desc], member_caption,
member_unique_name ON ROWS
FROM ( SELECT
(
[Sales Rep].[Sales Reps].[Sales Rep].&[12]
) ON COLUMNS
FROM ( SELECT
(
[Invoice Date].[Calendar].[Month].&[201506]
) ON COLUMNS
FROM [AlphaBI]) )
WHERE (
{
[Part].[Sales Unit].&[B],
IIF(@param = "sales", [Part].[Sales Unit].&[M], null)
}
)
编辑2
这有帮助吗?我已将Sales Unit
属性的关联移动到成员的定义而不是WHERE
子句。
WITH
MEMBER [Measures].[USD Sales YTD] AS
SUM(
{
[Invoice Date].[Calendar].[Month].&[201501]
:
[Invoice Date].[Calendar].[Month].&[201506]
}
*
{
[Part].[Sales Unit].&[B],
[Part].[Sales Unit].&[M]
}
,
[Measures].[USD Sales]
)
MEMBER [Measures].[USD Sales LY YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201401]
:
[Invoice Date].[Calendar].[Month].&[201406]
}
*
{
[Part].[Sales Unit].&[B],
[Part].[Sales Unit].&[M]
}
,
[Measures].[USD Sales]
)
MEMBER [Measures].[Unit Sales YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201501]
:
[Invoice Date].[Calendar].[Month].&[201506]
}
*
{
[Part].[Sales Unit].&[B]
}
,
[Measures].[Unit Sales]
)
MEMBER [Measures].[Unit Sales LY YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201401]
:
[Invoice Date].[Calendar].[Month].&[201406]
}
*
{
[Part].[Sales Unit].&[B]
}
,
[Measures].[Unit Sales]
)
SELECT
NON EMPTY
{
[Measures].[USD Sales YTD],
[Measures].[USD Sales LY YTD],
[Measures].[Unit Sales YTD],
[Measures].[Unit Sales LY YTD]
} ON COLUMNS,
NON EMPTY
{
(
[Invoice Date].[Calendar].[Month] *
[Customer].[Customer Name].[Customer Name] *
[Part].[Part #].[Part #] *
[Sales Rep].[Sales Reps].[Sales Rep]
)
} DIMENSION PROPERTIES [Part].[Part #].[Part Desc], member_caption,
member_unique_name ON ROWS
FROM ( select {[Part].[Sales Unit].&[B], [Part].[Sales Unit].&[M]} FROM (
SELECT
(
[Sales Rep].[Sales Reps].[Sales Rep].&[12]
) ON COLUMNS
FROM ( SELECT
(
[Invoice Date].[Calendar].[Month].&[201506]
) ON COLUMNS
FROM [AlphaBI]) ) )
答案 1 :(得分:0)
(I don't feed our mdx
into RS
so guessing a bit)
Can you add in a parameter into your report (I've called it @switch
) which dynamically creates a set at the top of your script- this is then referenced in the WHERE
clause:
WITH
set [whereSet] AS
strToSet(
iif(@switch = "Both",
"{[Part].[Sales Unit].&[B], [Part].[Sales Unit].&[M]}"
,"{[Part].[Sales Unit].&[B]}"
)
)
member [Measures].[USD Sales YTD] as (
SUM( {[Invoice Date].[Calendar].[Month].&[201501]:[Invoice Date].[Calendar].[Month].&[201506]},[Measures].[USD Sales] ))
member [Measures].[USD Sales LY YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201401]:[Invoice Date].[Calendar].[Month].&[201406]},[Measures].[USD Sales]))
member [Measures].[Unit Sales YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201501]:[Invoice Date].[Calendar].[Month].&[201506]},[Measures].[Unit Sales]))
member [Measures].[Unit Sales LY YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201401]:[Invoice Date].[Calendar].[Month].&[201406]},[Measures].[Unit Sales]))
SELECT
...
...
WHERE [whereSet]