创建集合以获取每年的最后一个月
Create Set to get Last month of Each Year
我想创建一个 return 编辑每年最后一个日期的集合。例如,所有往年都是 return 十二月,但今年只会 return 当前月份。
WITH
SET testset AS
NonEmpty
(
Generate
(
{
OpeningPeriod([Date].[Calendar].[Month])
:
ClosingPeriod([Date].[Calendar].Month)
}
,{[Date].[calendar].CurrentMember}
)
,[Measures].[Sale Amount]
)
SELECT
NON EMPTY
(
[Measures].[Sale Amount]
,[Date].[Year].[Year]
) ON 0
,NON EMPTY
[testset] ON 1
FROM [Cube]
这是一个脚本示例,其中 return 设置了每个月的值。我试过使用 tail 和 lastchild,但只有 return 是最新的。我希望每年 return。
仅就上个月而言 - 忽略是否有以下数据:
WITH
SET [AllYears] AS
[Date].[Calendar].[Calendar Year].MEMBERS
SET [LastMths] AS
Generate
(
[AllYears] AS S
,Tail
(
Descendants
(
S.CurrentMember
,[Date].[Calendar].[Month]
)
,1
)
)
SELECT
{} ON 0
,[LastMths] ON 1
FROM [Adventure Works];
Returns这个:
如果我想调整以上内容,使其成为每年最后一个月具有特定度量的数据,则将 NonEmpty
环绕在由 Descendants
创建的集合周围:
WITH
SET [AllYears] AS
[Date].[Calendar].[Calendar Year].MEMBERS
SET [LastMths] AS
Generate
(
[AllYears] AS S
,Tail
(
NonEmpty //<<new
(
Descendants
(
S.CurrentMember
,[Date].[Calendar].[Month]
)
,[Measures].[Internet Sales Amount] //<<new
)
,1
)
)
SELECT
{} ON 0
,[LastMths] ON 1
FROM [Adventure Works];
它现在给我们这个:
然后我们可以添加你在行上的元组(这次我使用属性层次结构已经很多年了,因为 [Date].[Calendar]
已经在使用)
WITH
SET [AllYears] AS
[Date].[Calendar].[Calendar Year].MEMBERS
SET [LastMths] AS
Generate
(
[AllYears] AS S
,Tail
(
NonEmpty
(
Descendants
(
S.CurrentMember
,[Date].[Calendar].[Month]
)
,[Measures].[Internet Sales Amount]
)
,1
)
)
SELECT
NON EMPTY
(
[Measures].[Internet Sales Amount]
,[Date].[Calendar Year].[Calendar Year]
) ON 0
,[LastMths] ON 1
FROM [Adventure Works];
现在我们得到这个:
@Whytheq 已经给出了很好的解决方案。将此视为替代方案。这可能 tad
更快,因为它不使用 GENERATE
函数(虽然不确定!)。
有一个 calculated/cube 度量,基本上可以判断一个月是否是一年中的最后一个月。然后 select 月份集合中的那些月份。
with member measures.islastchild as
iif
(
[Date].[Calendar].currentmember is
[Date].[Calendar].currentmember.parent.parent.parent.lastchild.lastchild.lastchild,
1,
null
)
成员 measures.islastchild
returns 1 如果月份是一年中的最后一个月。否则会 return null
.
set lastmonths as
filter(
[Date].[Calendar].[Month].members,
measures.islastchild = 1
)
那一套lastmonths
就是你需要的一套。
编辑
为了进一步提高性能,您可以 NonEmpty
函数代替迭代 FILTER
函数。
set lastmonths as
NonEmpty(
[Date].[Calendar].[Month].members,
measures.islastchild
)
select lastmonths on 1,
{} on 0
from [Adventure Works]
编辑 2:获取最后一个非销售额的月份
with member measures.islastnonemptychild as
iif
(
[Date].[Calendar].currentmember is
TAIL(NonEmpty([Date].[Calendar].currentmember.parent.parent.parent.lastchild.lastchild.children, [Measures].[Sale Amount])).ITEM(0),
1,
null
)
set nonemptylastmonths as
NonEmpty(
[Date].[Calendar].[Month].members,
measures.islastnonemptychild
)
我想创建一个 return 编辑每年最后一个日期的集合。例如,所有往年都是 return 十二月,但今年只会 return 当前月份。
WITH
SET testset AS
NonEmpty
(
Generate
(
{
OpeningPeriod([Date].[Calendar].[Month])
:
ClosingPeriod([Date].[Calendar].Month)
}
,{[Date].[calendar].CurrentMember}
)
,[Measures].[Sale Amount]
)
SELECT
NON EMPTY
(
[Measures].[Sale Amount]
,[Date].[Year].[Year]
) ON 0
,NON EMPTY
[testset] ON 1
FROM [Cube]
这是一个脚本示例,其中 return 设置了每个月的值。我试过使用 tail 和 lastchild,但只有 return 是最新的。我希望每年 return。
仅就上个月而言 - 忽略是否有以下数据:
WITH
SET [AllYears] AS
[Date].[Calendar].[Calendar Year].MEMBERS
SET [LastMths] AS
Generate
(
[AllYears] AS S
,Tail
(
Descendants
(
S.CurrentMember
,[Date].[Calendar].[Month]
)
,1
)
)
SELECT
{} ON 0
,[LastMths] ON 1
FROM [Adventure Works];
Returns这个:
如果我想调整以上内容,使其成为每年最后一个月具有特定度量的数据,则将 NonEmpty
环绕在由 Descendants
创建的集合周围:
WITH
SET [AllYears] AS
[Date].[Calendar].[Calendar Year].MEMBERS
SET [LastMths] AS
Generate
(
[AllYears] AS S
,Tail
(
NonEmpty //<<new
(
Descendants
(
S.CurrentMember
,[Date].[Calendar].[Month]
)
,[Measures].[Internet Sales Amount] //<<new
)
,1
)
)
SELECT
{} ON 0
,[LastMths] ON 1
FROM [Adventure Works];
它现在给我们这个:
然后我们可以添加你在行上的元组(这次我使用属性层次结构已经很多年了,因为 [Date].[Calendar]
已经在使用)
WITH
SET [AllYears] AS
[Date].[Calendar].[Calendar Year].MEMBERS
SET [LastMths] AS
Generate
(
[AllYears] AS S
,Tail
(
NonEmpty
(
Descendants
(
S.CurrentMember
,[Date].[Calendar].[Month]
)
,[Measures].[Internet Sales Amount]
)
,1
)
)
SELECT
NON EMPTY
(
[Measures].[Internet Sales Amount]
,[Date].[Calendar Year].[Calendar Year]
) ON 0
,[LastMths] ON 1
FROM [Adventure Works];
现在我们得到这个:
@Whytheq 已经给出了很好的解决方案。将此视为替代方案。这可能 tad
更快,因为它不使用 GENERATE
函数(虽然不确定!)。
有一个 calculated/cube 度量,基本上可以判断一个月是否是一年中的最后一个月。然后 select 月份集合中的那些月份。
with member measures.islastchild as
iif
(
[Date].[Calendar].currentmember is
[Date].[Calendar].currentmember.parent.parent.parent.lastchild.lastchild.lastchild,
1,
null
)
成员 measures.islastchild
returns 1 如果月份是一年中的最后一个月。否则会 return null
.
set lastmonths as
filter(
[Date].[Calendar].[Month].members,
measures.islastchild = 1
)
那一套lastmonths
就是你需要的一套。
编辑
为了进一步提高性能,您可以 NonEmpty
函数代替迭代 FILTER
函数。
set lastmonths as
NonEmpty(
[Date].[Calendar].[Month].members,
measures.islastchild
)
select lastmonths on 1,
{} on 0
from [Adventure Works]
编辑 2:获取最后一个非销售额的月份
with member measures.islastnonemptychild as
iif
(
[Date].[Calendar].currentmember is
TAIL(NonEmpty([Date].[Calendar].currentmember.parent.parent.parent.lastchild.lastchild.children, [Measures].[Sale Amount])).ITEM(0),
1,
null
)
set nonemptylastmonths as
NonEmpty(
[Date].[Calendar].[Month].members,
measures.islastnonemptychild
)