PowerBI中構(gòu)建日期表的終極方法

可以用簡(jiǎn)單的方式構(gòu)建日期表于颖,例如:Power Query / M 日期表。這種方式對(duì)于一般場(chǎng)景是足夠的酿箭,但并非極致,這里記錄構(gòu)建日期表的終極方案趾娃,目標(biāo)是最大限度的使用M或DAX優(yōu)雅地構(gòu)建一致可維護(hù)的日期表缭嫡。

PowerBI中日期表的構(gòu)建位置可以有兩個(gè)機(jī)會(huì):

  1. 在數(shù)據(jù)模型中用DAX構(gòu)建
  2. 在查詢(xún)編輯中用M構(gòu)建
    下面給出構(gòu)建日期表的終極方案。
    在日期表的構(gòu)建通常會(huì)考慮一些額外問(wèn)題抬闷,我們會(huì)看到不同方案的對(duì)比妇蛀。

用M構(gòu)建日期表

將以下內(nèi)容復(fù)制到PowerBI查詢(xún)編輯耕突,創(chuàng)建空查詢(xún)并保存為CreateCalendar即可。

更新日志:
2018-04-23 加入中文顯示


let

    CalendarType =  type function (
    
        optional CalendarYearStart as (type number meta [
            Documentation.FieldCaption = "開(kāi)始年份评架,日期表從開(kāi)始年份1月1日起眷茁。",
            Documentation.FieldDescription = "日期表從開(kāi)始年份1月1日起",
            Documentation.SampleValues = { Date.Year( DateTime.LocalNow( ) ) - 1 } // Previous Year
        ]),
        
        optional CalendarYearEnd as (type number meta [
            Documentation.FieldCaption = "結(jié)束年份,日期表至結(jié)束年份12月31日止纵诞。",
            Documentation.FieldDescription = "日期表至結(jié)束年份12月31日止",
            Documentation.SampleValues = { Date.Year( DateTime.LocalNow( ) ) } // Current Year
        ]),

        optional CalendarFirstDayOfWeek as (type text meta [
            Documentation.FieldCaption = "定義一周開(kāi)始日上祈,從 Monday,Tuesday浙芙,Wednesday登刺,Thursday,F(xiàn)riday嗡呼,Saturday纸俭,Sunday中選擇一個(gè),缺省默認(rèn)為Monday南窗。",
            Documentation.FieldDescription = "從 Monday揍很,Tuesday,Wednesday矾瘾,Thursday女轿,F(xiàn)riday箭启,Saturday壕翩,Sunday中選擇一個(gè),缺省默認(rèn)為Monday傅寡。",
            Documentation.SampleValues = { "Monday" }
        ]),

        optional CalendarCulture as (type text meta [
            Documentation.FieldCaption = "指定日期表顯示月以及星期幾的名稱(chēng)是中文或英文放妈,en 表示英文,zh 表示中文荐操,缺省默認(rèn)與系統(tǒng)一致芜抒。",
            Documentation.FieldDescription = " en 表示英文,zh 表示中文托启,缺省默認(rèn)與系統(tǒng)一致宅倒。",
            Documentation.SampleValues = { "zh" }
        ])

    ) 
    as table meta [
        Documentation.Name = "構(gòu)建日期表",
        Documentation.LongDescription = "創(chuàng)建指定年份之間的日期表。并可進(jìn)行各種設(shè)置屯耸。",
        Documentation.Examples = {
        [
            Description = "返回當(dāng)前年份日期表",
            Code = "CreateCalendar()",
            Result = "當(dāng)前年份日期表拐迁。"
        ],
        [
            Description = "返回指定年份的日期表",
            Code = "CreateCalendar( 2017 )",
            Result = "返回2017/01/01至2017/12/31之間的日期表。"
        ],
        [
            Description = "返回起止年份之間的日期表",
            Code = "CreateCalendar( 2015 , 2017 )",
            Result = "返回2015/01/01至2017/12/31之間的日期表疗绣。"
        ],
        [
            Description = "返回起止年份之間的日期表线召,并指定周二為每周的第一天",
            Code = "CreateCalendar( 2015 , 2017 , ""Tuesday"" )",
            Result = "2015/01/01至2017/12/31之間的日期表,且周二是每周的第一天多矮。"
        ],
        [
            Description = "返回起止年份之間的日期表缓淹,并指定周二為每周的第一天,并使用英文顯示名稱(chēng)。",
            Code = "CreateCalendar( 2015 , 2017 , ""Tuesday"", ""en"" )",
            Result = "2015/01/01至2017/12/31之間的日期表讯壶,且周二是每周的第一天料仗,并使用英文顯示月名稱(chēng)及星期幾的名稱(chēng)。"
        ]
        }
    ],
    
    
    CreateCalendar = ( optional CalendarYearStart as number, optional CalendarYearEnd as number, optional CalendarFirstDayOfWeek as text, optional  CalendarCulture as text) => let
        begin_year = CalendarYearStart ,
        end_year = CalendarYearEnd ,
        first_day_of_week = if Text.Lower( CalendarFirstDayOfWeek ) = "monday" then Day.Monday
                            else if Text.Lower( CalendarFirstDayOfWeek ) = "tuesday" then Day.Tuesday
                            else if Text.Lower( CalendarFirstDayOfWeek ) = "wednesday" then Day.Wednesday
                            else if Text.Lower( CalendarFirstDayOfWeek ) = "thursday" then Day.Thursday
                            else if Text.Lower( CalendarFirstDayOfWeek ) = "friday" then Day.Friday
                            else if Text.Lower( CalendarFirstDayOfWeek ) = "saturday" then Day.Saturday
                            else if Text.Lower( CalendarFirstDayOfWeek ) = "sunday" then Day.Sunday
                            else if CalendarFirstDayOfWeek <> null then error "參數(shù)錯(cuò)誤:參數(shù)CalendarFirstDayOfWeek必須是Monday伏蚊,Tuesday罢维,Wednesday,Thursday丙挽,F(xiàn)riday肺孵,Saturday,Sunday中的一個(gè)颜阐。"
                            else Day.Monday ,
        culture = if CalendarCulture <> null then CalendarCulture else "zh" , // "en" , "zh"
        y1 = if begin_year <> null then begin_year else if end_year <> null then end_year else Date.Year( DateTime.LocalNow() ) ,
        y2 = if end_year <> null then end_year else if begin_year <> null then begin_year else Date.Year( DateTime.LocalNow() ) ,
        calendar_list = { Number.From ( #date( Number.From( y1 ) , 1 , 1 ) ) .. Number.From( #date( Number.From( y2 ) , 12, 31 ) ) },
        calendar_list_table = Table.FromList(calendar_list, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Changed Type" = Table.TransformColumnTypes(calendar_list_table,{{"Column1", type date}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Column1", "Date"}}),
        #"Inserted Year" = Table.AddColumn(#"Renamed Columns", "Year", each Date.Year([Date]), Int64.Type),
        #"Inserted Quarter" = Table.AddColumn(#"Inserted Year", "Quarter", each Date.QuarterOfYear([Date]), Int64.Type),
        #"Inserted Month" = Table.AddColumn(#"Inserted Quarter", "Month", each Date.Month([Date]), Int64.Type),
        #"Inserted Week of Year" = Table.AddColumn(#"Inserted Month", "WeekOfYear", each Date.WeekOfYear( [Date] , first_day_of_week ), Int64.Type),
        #"Inserted Week of Month" = Table.AddColumn(#"Inserted Week of Year", "WeekOfMonth", each Date.WeekOfMonth( [Date] ), Int64.Type),
        #"Inserted Start of Week" = Table.AddColumn(#"Inserted Week of Month", "DateOfWeekStart", each Date.StartOfWeek( [Date] ), type date),
        #"Inserted End of Week" = Table.AddColumn(#"Inserted Start of Week", "DateOfWeekEnd", each Date.EndOfWeek([Date]), type date),
        #"Inserted Day" = Table.AddColumn(#"Inserted End of Week", "DayOfMonth", each Date.Day([Date]), Int64.Type),
        #"Inserted Day of Week" = Table.AddColumn(#"Inserted Day", "DayOfWeek", each Date.DayOfWeek( [Date] , first_day_of_week ), Int64.Type),
        #"Inserted Day of Year" = Table.AddColumn(#"Inserted Day of Week", "DayOfYear", each Date.DayOfYear([Date]), Int64.Type),
        #"Inserted Day Name" = Table.AddColumn(#"Inserted Day of Year", "DayOfWeekName", each Date.DayOfWeekName( [Date] , culture ), type text),
        #"Inserted Year Name" = Table.AddColumn(#"Inserted Day Name", "YearName", each "Y" & Text.From( [Year] )  , type text  ),
        #"Inserted Quarter Name" = Table.AddColumn(#"Inserted Year Name", "QuarterName", each "Q" & Text.From( [Quarter] ) , type text ),
        #"Inserted Month Name" = Table.AddColumn(#"Inserted Quarter Name", "MonthName", each Date.MonthName( [Date] , culture ), type text),
        #"Inserted Week Name" = Table.AddColumn(#"Inserted Month Name", "WeekName", each "W" & Text.From( [WeekOfYear] ) , type text ),
        #"Inserted Year Quarter" = Table.AddColumn(#"Inserted Week Name", "YearQuarter", each [Year] * 100 + [Quarter] , Int64.Type ),
        #"Inserted Year Month" = Table.AddColumn(#"Inserted Year Quarter", "YearMonth", each [Year] * 100 + [Month] , Int64.Type ),
        #"Inserted Year Week" = Table.AddColumn(#"Inserted Year Month", "YearWeek", each [Year] * 100 + [WeekOfYear] , Int64.Type ),
        #"Inserted Date Code" = Table.AddColumn(#"Inserted Year Week", "DateCode", each [Year] * 10000 + [Month] * 100 + [DayOfMonth] , Int64.Type )
    in
        if culture = "zh"
        then Table.RenameColumns( #"Inserted Date Code" ,{{"Date", "日期"}, {"Year", "年"}, {"Quarter", "季"}, {"Month", "月"}, {"WeekOfYear", "周"}, {"WeekOfMonth", "月周"}, {"DayOfMonth", "月日"}, {"DateOfWeekStart", "周開(kāi)始日期"}, {"DateOfWeekEnd", "周結(jié)束日期"}, {"DayOfWeek", "周天"}, {"DayOfYear", "年日"}, {"DayOfWeekName", "星期幾名稱(chēng)"}, {"YearName", "年份名稱(chēng)"}, {"QuarterName", "季度名稱(chēng)"}, {"MonthName", "月份名稱(chēng)"}, {"WeekName", "周名稱(chēng)"}, {"YearQuarter", "年季"}, {"YearMonth", "年月"}, {"YearWeek", "年周"}, {"DateCode", "日期碼"}})
        else #"Inserted Date Code"

in
    Value.ReplaceType( CreateCalendar , CalendarType )

這里看上去比普通的構(gòu)建日期表的方式復(fù)雜了一些平窘,但它盡量考慮了幾乎能考慮到的一切。我們看看效果:

可以看出這種方法給出了四個(gè)參數(shù)來(lái)構(gòu)建靈活的日期表:

  • 默認(rèn)構(gòu)建本年度的
  • 可以構(gòu)建指定年份之間的
  • 可以指定每周開(kāi)始的日期
  • 可以指定按中文或英文顯示名稱(chēng)

另外凳怨,這里充分地使用了M的元數(shù)據(jù)功能瑰艘,可以用來(lái)做函數(shù)的提示并帶有完整的示例,代碼中還做了有效性校驗(yàn)防止用戶(hù)輸入不合理的數(shù)據(jù)肤舞,例如:

M代碼格式做了嚴(yán)格的調(diào)校紫新,不失為理解M的一個(gè)案例。

用DAX構(gòu)建日期表

當(dāng)然還可以在數(shù)據(jù)建模的時(shí)候使用DAX來(lái)構(gòu)建日期表李剖,一種方便的方法是創(chuàng)建計(jì)算表并使用如下DAX表達(dá)式:


Calendar = 
------------------------------------------------------------------------
VAR WeekNumberFlag = 2 // 1 - sunday , 2 - monday
// 1 - Sunday (1) and ends on Saturday (7). numbered 1 through 7. 
// 2 - Monday (1) and ends on Sunday (7). 
// 3 - Monday (0) and ends on Sunday (6).numbered 1 through 7
VAR WeekDayFlag = 2
VAR CalendarYearStart = 2016
VAR CalendarYearEnd = 2017
-------------------------------------------------------------------------
VAR CalendarBase = CALENDAR( DATE( CalendarYearStart , 1 , 1 ) , DATE( CalendarYearEnd , 12 , 31 ) )
RETURN
    GENERATE (
        CalendarBase,
        VAR CalendarCurrentDate = [Date]
        VAR CalendarYear = YEAR ( CalendarCurrentDate )
        VAR CalendarMonth = MONTH ( CalendarCurrentDate )
        VAR CalendarQuarter = SWITCH( TRUE() , 
            CalendarMonth <= 3 , 1 , 
            CalendarMonth <= 6 , 2 ,
            CalendarMonth <= 9 , 3 ,
            4 
        )
        VAR CalendarYearMonth = CalendarYear * 100 + CalendarMonth
        VAR CalendarYearWeek = CalendarYear * 100 + WEEKNUM( CalendarCurrentDate )
        VAR CalendarDayOfWeek = WEEKDAY( CalendarCurrentDate , WeekDayFlag ) 
        VAR CalendarWeekOfYear = WEEKNUM( CalendarCurrentDate , WeekNumberFlag )
        VAR CalendarDayOfMonth = DAY( CalendarCurrentDate )
        RETURN ROW (
            "Year" , CalendarYear ,
            "Quarter" , CalendarQuarter ,
            "Month" , CalendarMonth ,
            "WeekOfYear" , CalendarWeekOfYear ,
            "DayOfMonth" , CalendarDayOfMonth ,
            "DayOfWeek" , CalendarDayOfWeek ,
            "DayOfWeekName" , FORMAT( CalendarCurrentDate , "aaaa" ) ,
            "YearName" , "Y" & CalendarYear ,
            "QuarterName" , "Q" & CalendarQuarter ,
            "MonthName", FORMAT ( CalendarCurrentDate, "mmm" ) ,
            "WeekName", "W" & CalendarWeekOfYear ,
            "YearQuarter", CalendarYear * 100 + CalendarQuarter ,
            "YearMonth" , CalendarYearMonth ,
            "YearWeek" , CalendarYear * 100 + CalendarWeekOfYear ,
            "DateCode" , CalendarYear * 10000 + CalendarMonth * 100 + CalendarDayOfMonth
        )
    )

這里使用了與上述M構(gòu)建日期表一樣的命名方式芒率,以兼容兩個(gè)日期表的字段名稱(chēng)。

總結(jié)

通過(guò)對(duì)比上述兩種構(gòu)建日期表的方式篙顺,考慮優(yōu)先使用M的構(gòu)建方式偶芍。
如果您有更好的方法,歡迎提供交流德玫。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末匪蟀,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子宰僧,更是在濱河造成了極大的恐慌材彪,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,311評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件琴儿,死亡現(xiàn)場(chǎng)離奇詭異段化,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)凤类,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)穗泵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人谜疤,你說(shuō)我怎么就攤上這事佃延∠志鳎” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,671評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵履肃,是天一觀的道長(zhǎng)仔沿。 經(jīng)常有香客問(wèn)我,道長(zhǎng)尺棋,這世上最難降的妖魔是什么封锉? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,252評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮膘螟,結(jié)果婚禮上成福,老公的妹妹穿的比我還像新娘。我一直安慰自己荆残,他們只是感情好奴艾,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,253評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著内斯,像睡著了一般蕴潦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上俘闯,一...
    開(kāi)封第一講書(shū)人閱讀 49,031評(píng)論 1 285
  • 那天潭苞,我揣著相機(jī)與錄音,去河邊找鬼真朗。 笑死此疹,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蜜猾。 我是一名探鬼主播秀菱,決...
    沈念sama閱讀 38,340評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼振诬,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼蹭睡!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起赶么,我...
    開(kāi)封第一講書(shū)人閱讀 36,973評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤肩豁,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后辫呻,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體清钥,經(jīng)...
    沈念sama閱讀 43,466評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,937評(píng)論 2 323
  • 正文 我和宋清朗相戀三年放闺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了祟昭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,039評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡怖侦,死狀恐怖篡悟,靈堂內(nèi)的尸體忽然破棺而出谜叹,到底是詐尸還是另有隱情,我是刑警寧澤搬葬,帶...
    沈念sama閱讀 33,701評(píng)論 4 323
  • 正文 年R本政府宣布荷腊,位于F島的核電站,受9級(jí)特大地震影響急凰,放射性物質(zhì)發(fā)生泄漏女仰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,254評(píng)論 3 307
  • 文/蒙蒙 一抡锈、第九天 我趴在偏房一處隱蔽的房頂上張望疾忍。 院中可真熱鬧,春花似錦床三、人聲如沸锭碳。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,259評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)擒抛。三九已至,卻和暖如春补疑,著一層夾襖步出監(jiān)牢的瞬間歧沪,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工莲组, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留诊胞,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,497評(píng)論 2 354
  • 正文 我出身青樓锹杈,卻偏偏與公主長(zhǎng)得像撵孤,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子竭望,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,786評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容