![](/img/trans.png)
[英]How to calculate difference in months in JSP between date column and current date
[英]MDX : how to calculate number of months between a date and beginning of the year?
我想使用iccube計算日期與年初之間的月份數。
我的日期維度中有2個層次結構: [Date de sortie].[Année, semestre, trimestre, mois, jour]
:包含年,半年,季度,月和日[Date de sortie].[Année et Mois]
包含年份和月份
執行以下mdx會給我帶來良好的效果(5個月)
with
set myData as {[Date de sortie].[Année et Mois].[Mois].&[2017-01-01]:[Date de sortie].[Année et Mois].[Mois].&[2017-05-01]}
member nbMonths as myData.count
select
{
nbMonths
} on 0,
myData on 1
from [Cube]
但是,當試圖對此進行參數化時,它將使用以數據的第一個日期開頭的時間段,而不是以計算的日期(一年中的第一天)開頭的時間段。
with
calculated member beginOfYear as dtWithDayOfYear(lookupByKey([Date de sortie].[Année, semestre, trimestre, mois, jour].[Jour], [Date de sortie].[Année et Mois].[Mois].&[2017-05-01].key),1).key
set myData as {lookupbykey([Date de sortie].[Année et Mois].[Mois], beginOfYear):[Date de sortie].[Année et Mois].[Mois].&[2017-05-01]}
calculated member nbMonths as myData.count
select
{
nbMonths, beginOfYear
} on 0,
myData on 1
from [Cube]
...
結果如上所示,beginOfYear似乎已正確計算,但未在設置myData中使用。 因此,它返回113而不是5。
僅使用MDX可能會很復雜,一種選擇是在icCube中使用Java接口
通過聲明一個需要兩個日期並使用Joda庫的函數,可以實現上述目標:
Function NumberOfMonth( Value date1 ,Value date2 ) as
abs(J!org.joda.time.Months.monthsBetween(date2, date1)->getMonths() )
代碼如下:
WITH
FUNCTION NumberOfMonth( Value date1 ,Value date2 ) as abs( J!org.joda.time.Months.monthsBetween(date2, date1)->getMonths() )
MEMBER Diff as NumberOfMonth( [D. Time].[H. Time].[Day].&[2015-03-02].key, [D. Time].[H. Time].[Day].&[2015-10-02].key )
SELECT
[Diff] ON 0
FROM [Sales]
------- 2018年3月8日-------
不要忘記打開Java模塊並將classRepositoryActive設置為true(如果該錯誤將在下一版本中修復,則為最新)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.