简体   繁体   English

Power BI (DAX) 中的自定义日历 - 会计季度的计算列

[英]Custom Calendar in Power BI (DAX) - Calculated column for Fiscal Quarter

I am building a custom calendar in Power Bi.我正在 Power Bi 中构建自定义日历。

I work mostly with US Fiscal Year values, which means that a Fiscal Year 2017 started on October 1st (2016-10-01) and will end on September 30th (2017-09-30).So, current calendar year compared to fiscal year would look as follows:我主要使用美国财政年度值,这意​​味着 2017 财政年度从 10 月 1 日 (2016-10-01) 开始,并将在 9 月 30 日 (2017-09-30) 结束。因此,当前日历年与财政年度相比如下所示:

===============================================
Calendar YearMonth     ||   Fiscal YearMonth
===============================================
201601                 ||   201604
201602                 ||   201605
201603                 ||   201606
201604                 ||   201607
201605                 ||   201608
201606                 ||   201609
201607                 ||   201610
201608                 ||   201611
201609                 ||   201612
201610                 ||   201701
201611                 ||   201702
201612                 ||   201703

In order to calculate month-to-date, quarter-to-date and year-to-date values, I need a working calendar.为了计算月初至今、季度至今和年初至今的值,我需要一个工作日历。 The way I tried to approach the issue, is that I have created calculated columns.我试图解决这个问题的方式是我创建了计算列。

I have managed to get Fiscal Year and Fiscal Month No. formulas to work:我已经设法使会计年度和会计月份编号公式起作用:

Fiscal Year = IF(MONTH([DATE]) > 9, YEAR([DATE])+1, YEAR([DATE]))

Fiscal YearMonth No = 
SWITCH( 
    TRUE(),
    MONTH('Calendar'[Date]) = 10, 1,
    MONTH('Calendar'[Date]) = 11, 2,
    MONTH('Calendar'[Date]) = 12, 3,
    MONTH('Calendar'[Date]) = 1, 4,
    MONTH('Calendar'[Date]) = 2, 5,
    MONTH('Calendar'[Date]) = 3, 6,
    MONTH('Calendar'[Date]) = 4, 7,
    MONTH('Calendar'[Date]) = 5, 8,
    MONTH('Calendar'[Date]) = 6, 9,
    MONTH('Calendar'[Date]) = 7, 10,
    MONTH('Calendar'[Date]) = 8, 11,
    MONTH('Calendar'[Date]) = 9, 12
    )

But unfortunately I cannot seem to figure out a formula to get Fiscal Year Quarter No. My latest attempt was to use SWITCH function together with DATESBETWEEN as follows:但不幸的是,我似乎无法找出获得财政年度季度编号的公式。我最近的尝试是将 SWITCH 函数与 DATESBETWEEN 一起使用,如下所示:

Fiscal YearQuarter No = 
SWITCH( 
    TRUE(),
    (DATESBETWEEN('Calendar',MONTH('Calendar'[Date]) >= 9, MONTH('Calendar'[Date])) <= 12)), 1,
    (DATESBETWEEN ('Calendar'[Date],MONTH('Calendar'[Date]) >= 1, MONTH('Calendar'[Date]) <= 3)), 2,
    (DATESBETWEEN ('Calendar'[Date],MONTH('Calendar'[Date]) >= 4, MONTH('Calendar'[Date]) <= 6)), 3,
    (DATESBETWEEN ('Calendar'[Date],MONTH('Calendar'[Date]) >= 7, MONTH('Calendar'[Date]) <= 9)), 4
)

Unfortunately I am getting a syntax error.不幸的是,我遇到了语法错误。

Since the MONTH() function returns an integer value, I think that is why you are getting a syntax error by wrapping that with a DATESBETWEEN() function.由于 MONTH() 函数返回一个整数值,我认为这就是为什么通过使用 DATESBETWEEN() 函数包装它会出现语法错误的原因。

Instead, just use simple integer operators.相反,只需使用简单的整数运算符。 I tested your data with this calculation and it works:我用这个计算测试了你的数据并且它有效:

Fiscal YearQuarter No = 
SWITCH(
    TRUE(),
    MONTH('Calendar'[Date])>=10 && MONTH('Calendar'[Date]) <= 12,1,
    MONTH('Calendar'[Date])>=1 && MONTH('Calendar'[Date]) <= 3,2,
    MONTH('Calendar'[Date])>=4 && MONTH('Calendar'[Date]) <= 6,3,
    MONTH('Calendar'[Date])>=7 && MONTH('Calendar'[Date]) <= 9,4
)

Also, you probably do not need such a long expression for calculating your Fiscal month numbers, because your fiscal calendar is simply 3 months offset from the standard calendar.此外,您可能不需要这么长的表达式来计算您的会计月份数,因为您的会计日历只是从标准日历偏移 3 个月。 Try an arithmetic formula to replace it with.尝试一个算术公式来代替它。 This should work:这应该有效:

Fiscal YearMonth No= = 
=IF((MONTH('Calendar'[Date])+3)>12,ABS(12-(MONTH('Calendar'[Date])+3)),(MONTH('Calendar'[Date])+3))

You Can simply change Your Calendar Year to Fiscal Year like this您可以像这样简单地将日历年更改为财政年

Fiscal Month = Orders[Order Date].[Month]

Fiscal Year = CONCATENATE("FY ",IF(Orders[Order Date].[MonthNo]>=4 && Orders[Order Date].[MonthNo]<=12,Orders[Order Date].[Year],Orders[Order Date].

[Year]-1))

This Fiscal Year is according to the Indian Fiscal year.这个财政年度是根据印度财政年度。

This will create a new table in power BI这将在 Power BI 中创建一个新表

Date = Calendar(Date(2019,1,1), Date(2025,12,31))
Month = Format('Date'[Date], "MMM yyyy")
Quarter = Year('Date'[Date]) & "-Q" & Format('Date'[Date], "q")
Year = Format('Date'[Date], "yyyy")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM