我的表情看起来像这样:
Sum({$<[Calendar Date] = {
<=$(=Date(MonthEnd(Date(Today()))))
>=$(=Date(AddMonths(MonthStart(Date(Today())), - 12)))"}>} [Amount])
这就是数据的样子:
Companies MonthYear Amount
Company1 201701 10000
Company1 201612 20000
Company2 201701 -
Company2 201612 -
Company2 201611 35000
Company2 201610 17000
Company3 201701 -
Company3 201612 50000
Company3 201611 10000
Company3 201610 24000
Company3 201609 40000
Company3 201608 -
Company3 201607 -
Company3 201606 32000
Company3 201605 18000
这就是我想要实现的目标:
示例:
但是因为我使用Today()作为我的限制,所以我显示了以下内容:(这不是我想要实现的目标)
示例:
我如何更改表达式,以便我的信息中心将上一个可用月份的[金额]字段与每个公司的数据相加,直到该日期之前的12个月?
PS:今天()= 2016年1月23日
答案 0 :(得分:1)
这可能有用......
=Sum({<MonthYear={'$(=Max(MonthYear))'}>}Amount)
答案 1 :(得分:0)
我想出了我必须使用的表达方式:
我只需将今天()替换为可用金额的最大月份
Sum({$< [Calendar Date] = {"
<=$(=Date(MonthEnd(Date(Date(MonthEnd(Date(Max(Aggr(Max({$<[Calendar Date] =
{"<=$(=Date(MonthEnd(Date(Today()))))"}, [Amount] = {"<>0"}>}
[Calendar Date]), [Companies])))))))))
>=$(=Date(AddMonths(MonthStart(Date(Date(MonthEnd(Date(Max(
Aggr(Max({$<[Calendar Date] = {"<=$(=Date(MonthEnd(Date(Today()))))"}, [Amount] = {"<>0"}>}
[Calendar Date]), [Companies]))))))), -12)))"}>} [Amount])
答案 2 :(得分:0)
我看到你已回答了自己的问题,但想提出另一个建议。
如果您希望在一段时间内(即去年)汇总值,并且您只想查看去年,那么您可以通过放置一些来减少该集合分析的复杂性标记到您的数据模型中。我知道它没有设置分析,但如果它扩展了它会改善你UI的性能,因为你正在使用一些复杂的函数来实现我们在模型中可以做的事情。
这个加载脚本做了三件事:
然后,在您的集合分析中,您可以在图表中使用以下表达式(他们将&#34;公司&#34;字段作为维度)。
Sum({$<Flag_LatestYear={1}>}Amount)
这里比较了金额字段通常总和的数量,以及设定分析(删除了我添加的额外记录 - 故意超出范围)
Normal Sum vs Set Analysis for the latest 12 months available for each company
这是加载脚本。
// Load raw data - preceding load used to clean up date. I added a new row at the bottom that was deliberately out of the date range it should pick up
OriginalTable:
LOAD
Companies,
DATE(DATE#([Calendar Date],'YYYYMM')) AS [Calendar Date],
Amount;
LOAD * INLINE [
Companies, Calendar Date, Amount
Company1, 201701, 10000
Company1, 201612, 20000
Company2, 201701, -
Company2, 201612, -
Company2, 201611, 35000
Company2, 201610, 17000
Company3, 201701, -
Company3, 201612, 50000
Company3, 201611, 10000
Company3, 201610, 24000
Company3, 201609, 40000
Company3, 201608, -
Company3, 201607, -
Company3, 201606, 32000
Company3, 201605, 18000
Company3, 201405, 50000
];
// Grab the max dates from the loaded table
TEMP_MaxDate:
LOAD
Companies,
MAX([Calendar Date]) AS CalendarDateMax
RESIDENT OriginalTable
WHERE Amount > 0
GROUP BY Companies;
// This is a cheat, but I've put it in as your dataset is small. It'll iterate through and create joining records for every month in the last 12, per company
// I'll use this to join back to the OriginalTable for my flag. Note that it joins on two fields - a key would be better
LEFT JOIN (OriginalTable)
LOAD
Companies,
DATE(AddMonths(CalendarDateMax,-iterNo() + 1)) AS [Calendar Date],
1 AS Flag_LatestYear
RESIDENT TEMP_MaxDate
WHILE AddYears(CalendarDateMax,-1) <= AddMonths(CalendarDateMax,-iterNo() + 1);
// Clean up and remove the temp table
DROP TABLE TEMP_MaxDate;
一如既往,还有其他方法可以实现这一目标!如果这有用,请告诉我。