Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

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

Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

问题

我想要能够在Power BI的当前视觉上下文中,针对我正在处理的所有股票在动态时期内生成累积收益率,基于每日价格数据。

我在Power BI中使用了下面的公式来生成从每日价格数据中计算动态时期内的累积收益率(对于三家公司)。

ReturnsApple = DIVIDE (
    LASTNONBLANKVALUE ( Apple[Date], max([Close])  ),
    CALCULATE (
        FIRSTNONBLANKVALUE ( Apple[Date], max([Close]) ),
        OFFSET( -1, ALLSELECTED ( Apple[Date] ), ORDERBY( Apple[Date], ASC ) )
    )
) - 1

这个公式给我以下的图像:

Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

然而,我希望添加一个额外的列,我称之为“复合收益率”。该收益率计算如下:

((1+ apple[result])* (1+ amazon[result])* (1+ meta[result]))-1

对于所有选定的时期,如下所示:

Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

注意:我有三家股票分别在不同的表中,如下所示:

Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

谢谢!

英文:

I want to be able to generate a cumulative returns for all the shares that I am working on over a dynamic period in the current visual context from a daily price feed in Power BI.

I used the below formula to generate the cumulative returns over a dynamic period from a daily price feed ( for three companies ) in Power BI.

   ReturnsApple = DIVIDE (
LASTNONBLANKVALUE ( Apple[Date], max([Close])  ),
CALCULATE (
    FIRSTNONBLANKVALUE ( Apple[Date], max([Close]) ),
    OFFSET( -1, ALLSELECTED ( Apple[Date] ), ORDERBY( Apple[Date], ASC ) )
)

) - 1

This gives me the image below

Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

However, I am keen to add an additional column - which I refer to as Composite returns. The returns calculates
((1+ apple[result])* (1+ amazon[result])* (1+ meta[result]))-1
for all the selected periods as below

Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

Note: I have the three stocks in separate tables as below

Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI
Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

Thank you!

答案1

得分: 1

需要将所有的股票数据放在同一张表中。

  1. 首先,在PowerQuery中为每个单独的表添加一个Ticker列。

  2. 然后将您的单独数据源附加为新的合并数据源。

  3. 将数据加载到模型中。

  4. 创建一个包含您的股票代码列表的表格。 Tickers = VALUES( data[ticker] )

  5. 创建一个日历表。

  6. 创建您的度量值:

首先,我们为前一个收盘价声明一个变量(prior)。该变量取得了在视觉所选周期之前的最后一个非空数据日期上的值(LASTNONBLANKVALUE),这对于所选周期之前的一天没有数据(即市场不开放的情况)非常重要。

然后,我们获取上下文中最后一天或之前的最后一个非空值(与之前一样,如果最后一天不是交易日,则很重要),然后减去刚刚计算的prior变量,然后将其除以prior值,以获取累积变化。

VAR prior =
    CALCULATE (
        LASTNONBLANKVALUE ( 'Calendar'[Date], SUM ( data[Adj Close] ) ),
        DATESBETWEEN (
            'Calendar'[Date],
            BLANK (),
            CALCULATE ( FIRSTDATE ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) ) - 1
        )
    )
RETURN
    DIVIDE (
        LASTNONBLANKVALUE (
            DATESBETWEEN ( 'Calendar'[Date], BLANK (), LASTDATE ( 'Calendar'[Date] ) ),
            SUM ( data[Adj Close] )
        ) - prior,
        prior
    )
英文:

You'll need all of your ticker data in the same table.

  1. First, add a Ticker column for each individual table in PowerQuery.
    Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

  2. Then appending your individual sources as a new combined source.
    Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

  3. Load the data to the model.

  4. Create a table with a list of your tickers. Tickers = VALUES( data[ticker] )

  5. Create a Calendar table.
    Generate a composite returns for all the shares that I am working on over a dynamic period from a daily price feed in Power BI

  6. Create your measure:

First we declare a variable for the prior close (prior). This takes the value on the last date with non-blank data (LASTNONBLANKVALUE) prior to the visual's selected period (DATESBETWEEN(...,BLANK(),CALCULATE(FIRSTDATE(...),ALLSELECTED(...)))). This is important if the day immediately prior to the selected period has no data (i.e., markets were not open).

Then we take the last non-blank value on or before the last day in context (like before, important if last day is not a market day) less the prior variable we just calculated (prior) and divide that by the prior value to get cumulative change.

VAR prior =
    CALCULATE (
        LASTNONBLANKVALUE ( 'Calendar'[Date], SUM ( data[Adj Close] ) ),
        DATESBETWEEN (
            'Calendar'[Date],
            BLANK (),
            CALCULATE ( FIRSTDATE ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) ) - 1
        )
    )
RETURN
    DIVIDE (
        LASTNONBLANKVALUE (
            DATESBETWEEN ( 'Calendar'[Date], BLANK (), LASTDATE ( 'Calendar'[Date] ) ),
            SUM ( data[Adj Close] )
        ) - prior,
        prior
    )

huangapple
  • 本文由 发表于 2023年5月29日 03:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353224.html
匿名

发表评论

匿名网友

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

确定