PowerBI - 计算每日百分比回报和累积回报

发布于 2025-01-12 10:46:02 字数 472 浏览 0 评论 0原文

我试图找出一种在PowerBI中计算/可视化ETF价格(收盘价)累积百分比变化的方法,这样我就可以创建折线图并过滤日期范围(即使用时间线切片器选择不同的时期,以便我可以显示 1 个月/一年至今的累积变化等)。

这是我的数据集:

实际上,我正在尝试计算“收盘价”列中的累积百分比变化,但是我有多个 ETF(因此“交易品种”列有超过 1 个 ETF),这意味着每只 ETF 都需要有不同的“最低收盘价”。我希望能够每天这样做,这样我就可以生成折线图来可视化数据。

我已经尝试过几种方法来做到这一点,如果有帮助的话,我很乐意提供详细信息,但我发布的这种信息告诉您我是如何(不)成功的...对 PowerBI/DAX 相当陌生,所以非常感谢您的帮助。

I am trying to figure out a way of calculating/visualizing the cumulative percentage change in price (close price) of an ETF in PowerBI, such that I can create a line chart and filter the date range (i.e. select different periods with timeline slicer so I can show cumulative change over 1 month/year-to-date etc).

This is my dataset:

Effectively, I am trying to calculate the cumulative percent change in column 'close', however I have multiple ETFs (so column 'Symbol' has more than 1 ETF), and this means that there will need to be a different 'minimum close price' per ETF. I would like to be able to do this daily so I can produce a line chart to visualize the data.

I have tried doing this a few ways, am happy to provide details if helpful but me posting this sort of tells you how (un)successful I have been... Quite new to PowerBI/DAX so would really appreciate the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2025-01-19 10:46:02

以下是添加按符号分组的列的一种方法,然后添加具有百分比变化的列(基于 (currentClose-previousClose)/previousClose

  • List.Generate生成百分比变化值数组
  • 列表作为一列添加到表中
  • 展开分组表
let

//Get table data from someplace
//edit next lines to reflect you actual source
    Source = Excel.Workbook(File.Contents("C:\Users\ron\OneDrive\Documents\Book1.xlsx"), null, true),
    Table21_Table = Source{[Item="Table21",Kind="Table"]}[Data],

//set the data types
    #"Changed Type" = Table.TransformColumnTypes(Table21_Table,{
        {"datetime", type date}, {"open", type number}, {"high", type any}, 
        {"low", type any}, {"close", type number}, {"volume", type any}, {"Symbol", type text}}),

//Group rows by Symbol, then add a column with the percent change from close to close
   #"Grouped Rows" = Table.Group(#"Changed Type", {"Symbol"}, {
        {"Percent Change", (t) => Table.FromColumns(
            Table.ToColumns(t) & 
            {List.Generate(
                    ()=>[p=null, idx=0],
                    each [idx]< Table.RowCount(t),
                    each [p= (t[close]{[idx]+1}-t[close]{[idx]})/t[close]{[idx]}, idx=[idx]+1],
                    each [p])},type table [datetime=nullable date, open=nullable number, high=any, low=any, close=nullable number, 
                            volume=any, Symbol=nullable text, percentChange=nullable number])
            }
        
        }),

//expand the created table and set the datatypes
    #"Expanded Percent Change" = Table.ExpandTableColumn(#"Grouped Rows", "Percent Change", {"datetime", "open", "high", "low", "close", "volume", "percentChange"}, {"datetime", "open", "high", "low", "close", "volume", "percentChange"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Percent Change",{{"datetime", type date}, {"open", type number}, {"high", type any}, {"low", type any}, {"close", type number}, {"volume", type any}, {"percentChange", Percentage.Type}})
in
    #"Changed Type1"

在此处输入图像描述

Here's one way of adding a column which groups by Symbol, and then adds a column which has the percent change (based on (currentClose-previousClose)/previousClose.

  • List.Generate generates the array of percentage change values
  • Add that List as a column to the table
  • Expand the grouped table
let

//Get table data from someplace
//edit next lines to reflect you actual source
    Source = Excel.Workbook(File.Contents("C:\Users\ron\OneDrive\Documents\Book1.xlsx"), null, true),
    Table21_Table = Source{[Item="Table21",Kind="Table"]}[Data],

//set the data types
    #"Changed Type" = Table.TransformColumnTypes(Table21_Table,{
        {"datetime", type date}, {"open", type number}, {"high", type any}, 
        {"low", type any}, {"close", type number}, {"volume", type any}, {"Symbol", type text}}),

//Group rows by Symbol, then add a column with the percent change from close to close
   #"Grouped Rows" = Table.Group(#"Changed Type", {"Symbol"}, {
        {"Percent Change", (t) => Table.FromColumns(
            Table.ToColumns(t) & 
            {List.Generate(
                    ()=>[p=null, idx=0],
                    each [idx]< Table.RowCount(t),
                    each [p= (t[close]{[idx]+1}-t[close]{[idx]})/t[close]{[idx]}, idx=[idx]+1],
                    each [p])},type table [datetime=nullable date, open=nullable number, high=any, low=any, close=nullable number, 
                            volume=any, Symbol=nullable text, percentChange=nullable number])
            }
        
        }),

//expand the created table and set the datatypes
    #"Expanded Percent Change" = Table.ExpandTableColumn(#"Grouped Rows", "Percent Change", {"datetime", "open", "high", "low", "close", "volume", "percentChange"}, {"datetime", "open", "high", "low", "close", "volume", "percentChange"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Percent Change",{{"datetime", type date}, {"open", type number}, {"high", type any}, {"low", type any}, {"close", type number}, {"volume", type any}, {"percentChange", Percentage.Type}})
in
    #"Changed Type1"

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文