使用方法:在PQ中新建一个【空查询】,然后在【视图】打开【高级编辑器】,清除原来的内容,输入以下内容,设置你的起始日期和结束日期,点完成就ok了,优雅方便高效~希望对大家有帮助!
大家也可以在此基础上调整代码,以便获得自己想要的分析维度。
let
// 设置起始日期、结束日期
自定义日期区间 = {Number.From(#date(2018,1,1)) .. Number.From(#date(2018,12,31))},
转换为表= Table.FromList(自定义日期区间, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
生成日期列= Table.RenameColumns(转换为表,{{"Column1", "日期"}}),
日期类型 = Table.TransformColumnTypes(生成日期列,{{"日期", type date}}),
整数日期 = Table.AddColumn(日期类型, "IntDate", each Date.ToText([日期],"yyyyMMdd")),
整数类型1 = Table.TransformColumnTypes(整数日期,{{"IntDate", Int64.Type}}),
年份数 = Table.AddColumn(整数类型1, "YearID", each Date.Year([日期]), type number),
季度数 = Table.AddColumn(年份数, "QuarterID", each Date.QuarterOfYear([日期]), type number),
月份数 = Table.AddColumn(季度数, "MonthID", each Date.Month([日期]), type number),
年月数 = Table.AddColumn(月份数, "yyMMID", each Text.From([YearID]) & Text.PadStart(Text.From([MonthID]),2,"0")),
整数类型2 = Table.TransformColumnTypes(年月数,{{"yyMMID", Int64.Type}}),
年度周数 = Table.AddColumn(整数类型2, "WeekOfYearID", each Date.WeekOfYear([日期]), type number),
月度日数 = Table.AddColumn(年度周数, "DayID", each Date.Day([日期]), type number),
周星期数 = Table.AddColumn(月度日数, "DayOfWeekID", each Date.DayOfWeek([日期],1),type number),
年份 = Table.AddColumn(周星期数, "年份", each "Y" & Text.From([YearID]),type text ),
季度 = Table.AddColumn(年份, "季度", each "Q"&Text.From([QuarterID]),type text),
月份= Table.AddColumn(季度, "月份", each Text.From([MonthID]) & "月",type text),
年月S = Table.AddColumn(月份, "年月S", each Date.ToText([日期],"yy-MM"),type text),
年月S格式= Table.ReplaceValue(年月S,"-","'",Replacer.ReplaceText,{"年月S"}),
年月L = Table.AddColumn(年月S格式, "年月L", each Date.ToText([日期],"yyyy年M月"), type text),
年周= Table.AddColumn(年月L, "周", each "W" & Text.From( [WeekOfYearID] ),type text),
月日 = Table.AddColumn(年周, "日", each "D" & Text.From( [DayID] ),type text),
星期 = Table.AddColumn(月日, "星期", each Date.ToText([日期],"ddd"),type text),
调整列顺序= Table.ReorderColumns(星期,{"日期", "年份", "季度", "月份", "日", "星期", "周", "年月L", "年月S", "IntDate", "YearID", "QuarterID", "MonthID", "yyMMID", "WeekOfYearID", "DayID", "DayOfWeekID"})
in
调整列顺序