powerBI应用技巧

power BI应用技巧

持续跟新

1.函数日期表

使用高级编辑器—M函数(把下面代码直接复制即可)

版本1:前单位写的M函数,日期是2021年开始的

let

date=(optional 请输入开始年份 as number,optional 请输入结束年份 as number)=>

let
    x = 请输入开始年份,
    y = if 请输入结束年份 = null then 请输入开始年份 else 请输入结束年份,
    begin_date = if x = null then #date(Date.Year(DateTime.LocalNow()),1,1) else #date(x,1,1),
    end_date = if y = null then #date(Date.Year(DateTime.LocalNow()),12,31) else #date(y,12,31),
    list = {1..Number.From(end_date)-Number.From(begin_date)+1},
    dates = List.Transform( list , (item)=> Date.AddDays(begin_date,item-1) ),
    table = Table.TransformColumnTypes(Table.RenameColumns(Table.FromList(dates, Splitter.SplitByNothing(), null, null, ExtraValues.Error),{{"Column1", "日期"}}),{{"日期", type date}}),
    date_id = Table.TransformColumnTypes(Table.AddColumn(table, "日期序号", each Date.Year([日期])*10000+Date.Month([日期])*100+Date.Day([日期])),{{"日期序号", type number}}),
    year_id = Table.AddColumn(date_id, "年序号", each Date.Year([日期]), type number),
    year_name = Table.AddColumn(year_id, "年份名称", each Text.From([年序号]) & "年"),
    quarter_id = Table.AddColumn(year_name, "季度序号", each Date.QuarterOfYear([日期]), type number),
    quarter_name = Table.AddColumn(quarter_id, "季度名称", each Text.From([季度序号])& "季"),
    month_id = Table.AddColumn(quarter_name, "月份序号", each Date.Month([日期]), type number),
    month_name = Table.AddColumn(month_id, "月份名称", each Text.From([月份序号])& "月"),
    week_id = Table.AddColumn(month_name, "周序号", each Date.WeekOfYear([日期]), type number),
    week_name = Table.AddColumn(week_id, "周名称", each "W"&Text.From([周序号])),
    year_quarter_id = Table.AddColumn(week_name, "年季序号", each Date.Year([日期])*10+Date.QuarterOfYear([日期]), type number),
    year_quarter_name = Table.AddColumn(year_quarter_id, "年季名称", each Text.From([年序号])&"年"&Text.From([季度序号])&"季"),
    year_month_id = Table.AddColumn(year_quarter_name, "年月序号", each Date.Year([日期])*100+ Date.Month([日期]), type number),
    year_month_name = Table.AddColumn(year_month_id, "年月名称", each Text.From([年序号])& "年" &Text.From([月份序号])& "月"),
    year_week_id = Table.AddColumn(year_month_name, "年周序号", each Date.Year([日期])*100+ Date.WeekOfYear([日期]), type number),
    #"year_week-name" = Table.AddColumn(year_week_id, "年周名称", each Text.From([年序号]) & "年"&Text.From([周序号])&"周"),
    day_in_week_id = Table.AddColumn(#"year_week-name", "日序号", each Date.DayOfWeek([日期],0), type number),
    day_in_week_name = Table.AddColumn(day_in_week_id, "周天名称", each if [日序号] = 1 then "WD1" else
if [日序号] = 2 then "WD2" else
if [日序号] = 3 then "WD3" else
if [日序号] = 4 then "WD4" else
if [日序号] = 5 then "WD5" else
if [日序号] = 6 then "WD6" else
"WD7"),
    work_day = Table.AddColumn(day_in_week_name , "工作日", each if [日序号] = 6 or [日序号] = 0 then "休息日" else "工作日" )
in
    work_day

in 
    date( 2021 , Date.Year( DateTime.LocalNow() ))

版本2:参考@Fabric白茶文档 ,这里就不复制了,需要的可以参考链接(https://www.yuque.com/powerplat/ai1yri/yvk3gc)。

版本3:使用表函数

1.在建模窗口下,选择新建表格

2.修改表名之后,复制代码

日期表 =
GENERATE (
    CALENDAR ( MIN ( '调用的函数'[日期] ), MAX ( '调用的函数'[日期] ) ),
    VAR DA = [Date]
    VAR YEAR =YEAR ( DA )
    VAR QUARTER = "季度" & FORMAT ( DA, "Q" )
    VAR MONTE =FORMAT ( DA, "MM" ) & "月"
    VAR DAY =DAY ( DA )
    VAR WEEKID =WEEKDAY ( DA, 2 )
    RETURN
        ROW (
            "年度", YEAR,
            "季度", QUARTER,
            "月份", MONTE,
            "日", DAY,
            "年度季度", YEAR & QUARTER,
            "年度月份", YEAR & MONTE,
            "星期", WEEKID)
)

2.同比环比

同比:2022年1月与2023年1月比较;

环比:2022年2月与2022年1月比较:

直接上案例:计算库存金额的同比?(环比同理,YEAR改成MONTH)

1.库存金额 = 
SWITCH(
    SELECTEDVALUE('PAT还原口径'[ID],1) --默认还原后
    , 1 , [库存金额.还原后]
    , 2 , [库存金额.还原前]
)
2.库存金额.上年 = CALCULATE([库存金额],DATEADD('DIM日期'[日期],-1,YEAR))
3.库存金额.同比 = [库存金额]-[库存金额.上年]

你可能感兴趣的:(powerBi,powerbi,java,服务器,DAX,bi,finebi)