生成Power BI中动态时期的累积收益。

huangapple go评论52阅读模式
英文:

Generate cumulative returns over a dynamic period in Power BI

问题

我有一个包含股票从2015年到2021年的开盘价、收盘价、最高价、最低价和调整后的收盘价的表格。我的目标是在任何日期层次成员(年/季度/月/周/日)上生成累积收益。

Excel 尝试
我添加了3个额外的列:

(1) DailyReturn(使用 F3/F2-1)
(2) Prevalue(使用 H2)
(3) Cumulative Return(使用 ((1+J2)*(1+H3))-1)

Power BI 尝试

  • 表格的名称是 'Amazon2'。
  • 'Adjusted close' 是包含数值的列。

我创建了三个额外的列 -

(1) Index 列 - 从1开始

(2) PreValue - 用于捕捉前一天的调整后收盘价数值

CALCULATE(
    MAX(Amazon2[Adj Close]),
    ALL(Amazon2),
    Amazon2[Index] = EARLIER(Amazon2[Index])-1
)

(3) Return - 这提供每日报告

IF(Amazon2[PreValue] = BLANK(),
    BLANK(),
    (Amazon2[Adj Close]/Amazon2[PreValue])-1
)

以下是我在Power BI中的选择快照:

生成Power BI中动态时期的累积收益。

以及我的Power BI输出:

生成Power BI中动态时期的累积收益。

如何在任何日期层次级别上生成累积收益?

英文:

I have a table that contains the open, close, high, low, and adjusted close prices for a stock from 2015 to 2021. My goal is to produce cumulative returns over any date hierarchy member (year/quarter/month/week/daily).
Below is a snapshot of what I want to achieve and and my current results.

生成Power BI中动态时期的累积收益。

Excel attempt
I added 3 additional columns

(1) DailyReturn (using     F3/F2-1    )
(2) Prevalue (using     H2    )
(3) Cumulative Return (using     ((1+J2)*(1+H3))-1    )

Power BI attempt

  • Name of table is 'Amazon2'.

  • 'Adjusted close' is the column with the values

I created three (3) additional columns -

(1) Index column - starting with 1

(2) PreValue - this is to capture the previous day adjusted close value

CALCULATE(
    MAX(Amazon2[Adj Close]),
    ALL(Amazon2),
    Amazon2[Index] = EARLIER(Amazon2[Index])-1
)

(3) Return - this gives daily report

IF(Amazon2[PreValue] = BLANK(),
    BLANK(),
    (Amazon2[Adj Close]/Amazon2[PreValue])-1
)    

Below is the snapshot of my selections in Power BI:

生成Power BI中动态时期的累积收益。

And my Power BI output:

生成Power BI中动态时期的累积收益。

How can I produce cumulative returns over any date hierarchy level?

Excel working file

答案1

得分: 2

因为您只需要累计收益,我们实际上可以简化迭代每个日期的收益并相乘的过程。我们只需将结束时的调整后收益除以所选上下文之前的最后调整后收益。

VAR prior =
CALCULATE (
        FIRSTNONBLANKVALUE ( Amazon2[Date], [Adj Close] ),
        OFFSET( -1, ALLSELECTED ( Amazon2[Date] ), ORDERBY( Amazon2[Date], ASC ) )
    )
RETURN
DIVIDE (
    LASTNONBLANKVALUE ( Amazon2[Date], [Adj Close] ) - prior,
    prior
)

此度量采用上下文中的最后调整收盘价除以当前视觉上下文之前的最后调整收盘价。如果在视觉范围内没有调整收盘价数据,将使用视觉中第一个调整收盘价作为所有累积收益的分母。

我强烈建议创建一个单独的日历/日期表来过滤股票收益表。

例如,

Calendar = CALENDARAUTO()

在'Calendar'[Date]和Amazon2[Date]之间建立关系。

在日历上为周、月、季度和年添加列。这里有一些示例,但您可以根据需要进行调整。

Week = 'Calendar'[Date] - Weekday('Calendar'[Date],3) //周的第一天(从星期一开始)
Month = FORMAT('Calendar'[Date],"mmm") //月份名称格式为三个字符
Quarter = "Qtr"&FORMAT(INT((MONTH('Calendar'[Date])-1)/3)+1,"@") //季度格式为Qtr#
Year = YEAR('Calendar'[Date])
英文:

Because you are only looking for cumulative return, we can actually short-circuit iterating over each date's return and multiplying. We can just divide the ending adjusted return by the last adjusted return prior to the selected context.

VAR prior =
CALCULATE (
        FIRSTNONBLANKVALUE ( Amazon2[Date], [Adj Close] ),
        OFFSET( -1, ALLSELECTED ( Amazon2[Date] ), ORDERBY( Amazon2[Date], ASC ) )
    )
RETURN
DIVIDE (
    LASTNONBLANKVALUE ( Amazon2[Date], [Adj Close] ) - prior,
    prior
)

This measure takes the last adjusted close in context divided by the last adjusted close prior to the current visual context. If there is no adjusted close data prior to the date range included in the visual, the first adjusted close in the visual will be used as the denominator for all cumulative returns.

I would highly recommend creating a separate calendar/date table to filter the stock returns table by.

E.g.,

Calendar = CALENDARAUTO()

Create a relationship between 'Calendar'[Date] and Amazon2[Date].

Add columns on Calendar for week, month, quarter, and year. Here are some examples, but you can definitely adjust these as needed.

Week = 'Calendar'[Date] - Weekday('Calendar'[Date],3) //First day of weeks (starting on Monday)
Month = FORMAT('Calendar'[Date],"mmm") //Month name formatted to three characters
Quarter = "Qtr"&FORMAT(INT((MONTH('Calendar'[Date])-1)/3)+1,"@") //Quarter formatted as Qtr#
Year = YEAR('Calendar'[Date])

huangapple
  • 本文由 发表于 2023年5月24日 22:23:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定