By far the simplest solution is to add a “calendar” or “numbers” table to your universe. This is simply a table that contains a list of dates, or a list of sequential numbers. You can then do an outer join against that table to generate the missing months. Do a Google search for “calendar table” and your particular database for specific advice, as well as some options that other people have used.
If you happen to have a table already in your universe that is guaranteed to have at least one record (with a date field) for each month, you can also create a derived table against that to simulate a calendar table. Given that you only need month values, and not each day, that is viable.
thanks Lugh, but with your solution I guess indeed all moths will appear in the report… but the Prices will remain empty for those months and will only be filled in for jan, jun, sep and oct.
create view vfact as
(
select 200901 as period, 7.3 as val union all
select 200906 as period, 7.5 as val union all
select 200909 as period, 8.1 as val union all
select 200910 as period, 8.2 as val
)
go
create view vcalendar as
(
select 200901 as period union all
select 200902 as period union all
select 200903 as period union all
select 200904 as period union all
select 200905 as period union all
select 200906 as period union all
select 200907 as period union all
select 200908 as period union all
select 200909 as period union all
select 200910 as period union all
select 200911 as period union all
select 200912 as period
)
go
select
vcalendar.period,
MainFact.period,
MainFact.val,
(
select
Fact2.val
from
vFact Fact2
where
Fact2.period =
(
select
max(Fact1.period)
from
vFact Fact1
where
vcalendar.period > = Fact1.period
)
) val_calc
from
vCalendar
left outer join vFact MainFact
on vcalendar.period = MainFact.period
drop view vfact
go
drop view vcalendar
go
200901 200901 7.3 7.3
200902 NULL NULL 7.3
200903 NULL NULL 7.3
200904 NULL NULL 7.3
200905 NULL NULL 7.3
200906 200906 7.5 7.5
200907 NULL NULL 7.5
200908 NULL NULL 7.5
200909 200909 8.1 8.1
200910 200910 8.2 8.2
200911 NULL NULL 8.2
200912 NULL NULL 8.2
Oh, sure. You’d need to add an extra bit of SQL to fill that in. Or use the Previous() function on the report.
The SQL might look something like:
SELECT cal.DateName, MyTable.Price
FROM MyTable
INNER JOIN
(SELECT c.DateName, MAX(m.MyDate) as LastDate
FROM MyCalendar c
INNER JOIN MyTable m
ON m.MyDate < c.DateName
) cal
ON cal.LastDate = MyTable.MyDate