DateKey格式为YYYYMMDD

时间:2017-12-13 12:10:26

标签: powerquery m

我有以下日历表脚本:

let
    StartDate = #date(2016, 1, 1),
    EndDate = #date(2018, 12, 31),
    CurrentDate = DateTime.Date(DateTime.FixedLocalNow()),
    FiscalYearEndMonth = 6,
    ListDates = List.Dates(StartDate, Number.From(EndDate - StartDate)+1, #duration(1,0,0,0)),
    #"Converted to Table" = Table.FromList(ListDates, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Renamed Columns as Date" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
    #"Changed Type to Date" = Table.TransformColumnTypes(#"Renamed Columns as Date",{{"Date", type date}}),
    #"Added Calendar MonthNum" = Table.AddColumn(#"Changed Type to Date", "MonthNum", each Date.Month([Date]), Int64.Type),
    #"Added Calendar Year" = Table.AddColumn(#"Added Calendar MonthNum", "Year", each Date.Year([Date]), Int64.Type),
    #"Added MonthYearNum" = Table.AddColumn(#"Added Calendar Year", "MonthYearNum", each [Year]*100 + [MonthNum] /*e.g. Sep-2016 would become 201609*/, Int64.Type),
    #"Added Day Num" = Table.AddColumn(#"Added MonthYearNum", "DayNum", each Date.Day([Date])),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Datekey",{{"DayNum", Int64.Type}}),    
    #"Added Datekey" = Table.AddColumn(#"Added Day Num", "DateKey", each [Year]*100 + [MonthNum] + [DayNum]),
    #"Changed Type" = Table.TransformColumnTypes(#"Changed Type1",{{"DayNum", Int64.Type}, {"DateKey", Int64.Type}})
in
    #"Changed Type"

结果如下:

enter image description here

所以最后一栏让我头疼 - 我认为该专栏的代码是:

each [Year]*100 + [MonthNum] + [DayNum]

似乎这个表达式的前两个元素按预期连接在一起,但是然后添加了DayNum。对于1月1日,我们将2016连接为1以提供201601,但随后将数字添加到DayNum中,以提供201602,但我希望它提供20160101(格式为YYYYMMDD)< / p>

为什么会发生这种情况以及避免此行为的正确M/PowerQuery是什么?

1 个答案:

答案 0 :(得分:1)

我相信你所做的事情是正确的。怎么样呢?

each [MonthYearNum]*100 + [DayNum]

或者你可以做到

make