У меня есть аналогичное требование номера финансовой недели, Периода и dataid.
Я создал таблицу в базе данных, и она была импортирована в отчет poweri bi.
моя финансовая неделя начинается со второго марта воскресенья и номер финансовой недели в течение 10 лет с этого года.
скрипт ниже может помочь вам:
-- Create DI_Date diamension table:
CREATE TABLE APM_Reporting.di_date
(
"datekey" INTEGER NOT NULL
,"dateid" DATE NOT NULL
,"day" INTEGER NOT NULL
,"dayname" VARCHAR(10) NOT NULL
,"daynameshort" VARCHAR(3) NOT NULL
,"weekid" INTEGER NOT NULL
,"financialweekid" INTEGER NOT NULL
,"monthid" INTEGER NOT NULL
,"monthname" VARCHAR(25) NOT NULL
,"periodid" INTEGER NOT NULL
,"periodname" VARCHAR(25) NOT NULL
,"quarterid" INTEGER NOT NULL
,"quartername" VARCHAR(25) NOT NULL
,"year" INTEGER NOT NULL
,"finyear" VARCHAR(25) NOT NULL
,"created_timestamp" DATETIME DEFAULT CURRENT_TIMESTAMP
,PRIMARY KEY (datekey)
);
-- Check if temp table exists and drop it if true
IF OBJECT_ID('tempdb..##Dates') IS NOT NULL
DROP TABLE ##Dates
-- Create temporary table for pre-load
CREATE TABLE ##Dates(
DateValue Date
)
;
-- Declare Start Date
DECLARE @start DATE = GETDATE() - 396
DECLARE @end DATE = DATEADD(year, 10,@start)
WHILE @start < @end
BEGIN
INSERT INTO ##Dates(DateValue)
VALUES(@start)
SET @start = DATEADD(dd,1,@start)
END
;
-- Insert generated data to di_date
INSERT INTO APM_Reporting.di_date
(
"datekey",
"dateid",
"day",
"dayname",
"daynameshort",
"weekid",
"financialweekid",
"monthid",
"monthname",
"periodid",
"periodname",
"quarterid",
"quartername",
"year",
"finyear"
)
SELECT
YEAR(DateValue)*10000+MONTH(DateValue)*100+DAY(DateValue) AS "datekey"
,DateValue "dateid"
,DAY(DateValue) "day"
,DATENAME(dw, DateValue) "dayname"
,LEFT(DATENAME(dw,DateValue),3) "daynameshort"
,DATEPART(WK,DateValue) "weekid"
,CASE
WHEN DATEPART(WK,DateValue) < 11 THEN DATEPART(WK,DateValue)+42
ELSE DATEPART(WK,DateValue) - 10
END AS "financialweekid"
,DATEPART(MM, DateValue) "monthid"
,DATENAME(MM,DateValue) "monthname"
,CAST(
CONCAT(
(CASE
WHEN DATEPART(WK,DateValue) BETWEEN 1 and 10 THEN DATEPART(YYYY,DateValue)-1
ELSE DATEPART(YYYY,DateValue)
END)
,
CASE
WHEN DATEPART(WK,DateValue) BETWEEN '11' AND '14' THEN '01'
WHEN DATEPART(WK,DateValue) BETWEEN '15' AND '18' THEN '02'
WHEN DATEPART(WK,DateValue) BETWEEN '19' AND '22' THEN '03'
WHEN DATEPART(WK,DateValue) BETWEEN '23' AND '26' THEN '04'
WHEN DATEPART(WK,DateValue) BETWEEN '27' AND '30' THEN '05'
WHEN DATEPART(WK,DateValue) BETWEEN '31' AND '34' THEN '06'
WHEN DATEPART(WK,DateValue) BETWEEN '35' AND '38' THEN '07'
WHEN DATEPART(WK,DateValue) BETWEEN '39' AND '42' THEN '08'
WHEN DATEPART(WK,DateValue) BETWEEN '43' AND '46' THEN '09'
WHEN DATEPART(WK,DateValue) BETWEEN '47' AND '50' THEN '10'
WHEN DATEPART(WK,DateValue) BETWEEN '51' AND '53' THEN '11'
WHEN DATEPART(WK,DateValue) BETWEEN '01' AND '02' THEN '11'
WHEN DATEPART(WK,DateValue) BETWEEN '03' AND '06' THEN '12'
WHEN DATEPART(WK,DateValue) BETWEEN '06' AND '53' THEN '13'
ELSE '00'
END) AS INTEGER) AS "periodid"
,CONCAT('P',
CASE
WHEN DATEPART(WK,DateValue) BETWEEN '11' AND '14' THEN '01'
WHEN DATEPART(WK,DateValue) BETWEEN '15' AND '18' THEN '02'
WHEN DATEPART(WK,DateValue) BETWEEN '19' AND '22' THEN '03'
WHEN DATEPART(WK,DateValue) BETWEEN '23' AND '26' THEN '04'
WHEN DATEPART(WK,DateValue) BETWEEN '27' AND '30' THEN '05'
WHEN DATEPART(WK,DateValue) BETWEEN '31' AND '34' THEN '06'
WHEN DATEPART(WK,DateValue) BETWEEN '35' AND '38' THEN '07'
WHEN DATEPART(WK,DateValue) BETWEEN '39' AND '42' THEN '08'
WHEN DATEPART(WK,DateValue) BETWEEN '43' AND '46' THEN '09'
WHEN DATEPART(WK,DateValue) BETWEEN '47' AND '50' THEN '10'
WHEN DATEPART(WK,DateValue) BETWEEN '51' AND '53' THEN '11'
WHEN DATEPART(WK,DateValue) BETWEEN '01' AND '02' THEN '11'
WHEN DATEPART(WK,DateValue) BETWEEN '03' AND '06' THEN '12'
WHEN DATEPART(WK,DateValue) BETWEEN '06' AND '53' THEN '13'
ELSE '00'
END) "periodname"
,DATEPART(Q,DateValue) "quarterid"
,CONCAT('Q',DATEPART(Q,DateValue)) "quartername"
,DATENAME(YYYY,DateValue) "year"
,CASE
WHEN DATEPART(WK,DateValue) BETWEEN '11' AND '53' THEN CONCAT(DATENAME(YYYY,DateValue),'-',DATENAME(YYYY,DateValue)+1)
ELSE CONCAT(DATENAME(YYYY,DateValue)-1,'-',DATENAME(YYYY,DateValue))
END AS "finyear"
FROM ##Dates;