我有一个包含 7 列的表格:日期、时间、开盘价、最高价、最低价、收盘价、TickVol。 在 Excel 中,要计算体积变化百分比,请在单元格 H3=if iserror((G3/G2-1)*100,"",(G3/G2-1)*100) 上。我只需从 H4 开始复制并粘贴该公式即可,它可以在 Excel 中使用。 但是当我尝试在 Power Query 中执行此操作时,它仍然有效,但存在一个恼人的问题,即结果从第一行开始,而它应该从第二行开始。基本上,它不太协调。 我试图通过尝试在第一行插入空值来解决这个问题,但它不起作用。尝试了几种方法但没有一个有效。这是我最终得到的最后一个代码,但它不起作用。 我知道一旦数据加载到 Excel 中,我就可以轻松地通过简单的复制和粘贴来移动数据,但我只是觉得很烦人,因为我不知道如何使用代码来解决这个简单的问题。
这有效:
let
Source = Excel.CurrentWorkbook(){[Name="tbl_BTCUSD_M30"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
#"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 0, 1, Int64.Type),
#"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Index"}, #"Added Index1", {"Index.1"}, "Added Index1", JoinKind.LeftOuter),
#"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"<TICKVOL>"}, {"Added Index1.<TICKVOL>"}),
#"Added Custom" = Table.AddColumn(#"Expanded Added Index1", "%VOLUME CHANGE", each [#"Added Index1.<TICKVOL>"]/[#"<TICKVOL>"]-1),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Index.1", "Added Index1.<TICKVOL>"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"%VOLUME CHANGE", Percentage.Type}})
in
#"Changed Type"
这不是:
let
Source = Excel.CurrentWorkbook(){[Name="tbl_BTCUSD_M30"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
#"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 0, 1, Int64.Type),
#"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Index"}, #"Added Index1", {"Index.1"}, "Added Index1", JoinKind.LeftOuter),
#"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"<TICKVOL>"}, {"Added Index1.<TICKVOL>"}),
#"Added Custom" = Table.AddColumn(#"Expanded Added Index1", "%VOLUME CHANGE", each [#"Added Index1.<TICKVOL>"]/[#"<TICKVOL>"]-1),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Index.1", "Added Index1.<TICKVOL>"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"%VOLUME CHANGE", Percentage.Type}}),
InsertNull = Table.InsertRows(Source, 0, {["%VOLUME CHANGE" = null]})
in
InsertNull
您似乎将一个相当简单的问题过于复杂化了。 您需要的是一种方法来获取 TicVol except 的先前值(第一个值应为空)。 您可以使用索引列来完成此操作:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type1" = Table.TransformColumnTypes(Source,{{"Open", type number}, {"High", type number}, {"Low", type number}, {"Close", type number}, {"TickVol", type number}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type1", "Index", 0, 1, Int64.Type),
// Add previous value of TickVol except for the first one where null
#"Shifted TickVol" = Table.AddColumn(#"Added Index", "Previous TickVol", each try Source[TickVol]{[Index] - 1} otherwise null),
// Calculate the %VOLUME CHANGE
#"Added Volume Change" = Table.AddColumn(#"Shifted TickVol", "%VOLUME CHANGE", each if [Previous TickVol] = null then null else ([TickVol] / [Previous TickVol] - 1)),
// Remove unnecessary columns
#"Removed Columns" = Table.RemoveColumns(#"Added Volume Change",{"Previous TickVol", "Index"}),
// Change type to Percentage
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns", {{"%VOLUME CHANGE", Percentage.Type}})
in
#"Changed Type"