SSAS - Facts that happened over a time range

送分小仙女□ 提交于 2019-12-08 08:14:29

问题


I don't really know how to handle facts that happened over a period of time. I sually deal with facts that happened on a specific date.

In clear, my facts have a start_date and an end_date. So, let's say my start_date is 01/01/2008 and my end_date is 01/01/2011. I need to get the number of those facts that happened in 2009 and those that happened this year. The same fact can have happened on both years. The way to determine a fact is part of 2009 is to check for 12/31/2009.

I was thinking about a StartDate and EndDate dimensions, using date ranges (so from the first date of my StartDate dimension to 12/31/2009 and from 12/31/2009 to the last date in my EndDate dimension). I would cross join those.

I tried it, it works, but it's REALLY slow.

Any thoughts?


回答1:


I found the solution to what I wanted. David and Chris for the anwsers tho! Here's what I wanted to achieve, but I was lacking MDX syntax :

SELECT  [Measures].[NumberX] ON COLUMNS
FROM    [MyCube]
WHERE   ([START DATE].[REFDATE].FirstMember:[START DATE].[REFDATE].&[2009-12-31T00:00:00],
        [END DATE].[REFDATE].&[2010-01-01T00:00:00]:[END DATE].[REFDATE].LastMember)

Pretty simple. I think my question was not clear, that's why I got different answers ;-)




回答2:


You can always use a date range with the two time dimensions like:

Select [start date].[year].[2009]:[end date].[year].[2010] on 0 from cube

If I'm understanding the question correctly. Two time dimensions should work together fine. I have two in a project I'm doing at work and they work rather fast together. Make sure that you set them up in dimension usage section of the cube so you can differentiate the two dates.




回答3:


You just need one Date dimension DimDate. Your fact table can have 2 foreign keys to the DimDate, one for startdate and one for enddate.

FactTable
{
    FactID int,
    StartDate int,
    EndDate int
    -- Other fields
}

DimDate
{
    DimDateID int,
    Year int,
    Month int,
    Day int,
    -- Other fields if needed
}

To get all facts that fall on the year 2009, use

SELECT f.FactID FROM FactTable f
INNER JOIN DimDate dStart ON dStart.DimDateID = f.StartDate
INNER JOIN DimDate dEnd ON dEnd.DimDateID = f.EndDate
WHERE dStart.Year <= 2009
  AND dEnd.Year >= 2009


来源:https://stackoverflow.com/questions/3752313/ssas-facts-that-happened-over-a-time-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!