英文:
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
这个公式给我以下的图像:
然而,我希望添加一个额外的列,我称之为“复合收益率”。该收益率计算如下:
((1+ apple[result])* (1+ amazon[result])* (1+ meta[result]))-1
对于所有选定的时期,如下所示:
注意:我有三家股票分别在不同的表中,如下所示:
谢谢!
英文:
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
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
Note: I have the three stocks in separate tables as below
Thank you!
答案1
得分: 1
需要将所有的股票数据放在同一张表中。
-
首先,在PowerQuery中为每个单独的表添加一个Ticker列。
-
然后将您的单独数据源附加为新的合并数据源。
-
将数据加载到模型中。
-
创建一个包含您的股票代码列表的表格。
Tickers = VALUES( data[ticker] )
-
创建一个日历表。
-
创建您的度量值:
首先,我们为前一个收盘价声明一个变量(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.
-
First, add a Ticker column for each individual table in PowerQuery.
-
Then appending your individual sources as a new combined source.
-
Load the data to the model.
-
Create a table with a list of your tickers.
Tickers = VALUES( data[ticker] )
-
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
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论