M - 将累计值(运行 总计)转换为实际值
M - Transforming cumulative value (running total) to actual value
用户收到月度客户报告,其中 运行 每月总销售额(几万行)。销售额显示为 运行 总计,我试图找到一个 powerquery 解决方案来获得实际销售额作为(*必需*)输出:
╔══════╦═══════╦═════════╦═══════╦════════════╗
║ Year ║ Month ║ StoreID ║ Sales ║ *Required* ║
╠══════╬═══════╬═════════╬═══════╬════════════╣
║ 2017 ║ 10 ║ 1 ║ 5 ║ 5 ║
║ 2017 ║ 11 ║ 1 ║ 11 ║ 6 ║
║ 2017 ║ 12 ║ 1 ║ 18 ║ 7 ║
║ 2017 ║ 11 ║ 2 ║ 10 ║ 10 ║
╚══════╩═══════╩═════════╩═══════╩════════════╝
在 tSQL 中我会做类似
的事情
Sales - LAG(Sales,1,0)OVER(Partiton by Year, StoreID Order by Month)
一个类似的问题(但相反)was answered,但建议的解决方案看起来相当于交叉连接。我是 powerquery 的新手(所以我可能会误解)但是随着数据集每个月都在增长,我看不到这是可行的。有没有更好的方法来解决这个问题,我在 "M" 中找不到示例?
编辑:List.Range 看起来很有希望
实际上您需要当前行上一行的值。
一种常见的方法是添加 2 个索引列,一个以 0 开头,另一个以 1 开头。接下来,使用索引列作为连接列将 table 与其自身合并。从嵌套的 table 中展开所需的列后,您可以添加具有所需计算的自定义列。
let
Source = Sales,
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
#"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1),
#"Merged Queries" = Table.NestedJoin(#"Added Index1",{"Index"},#"Added Index1",{"Index.1"},"Previous",JoinKind.LeftOuter),
#"Expanded Previous" = Table.ExpandTableColumn(#"Merged Queries", "Previous", {"StoreID", "Sales"}, {"Previous.StoreID", "Previous.Sales"}),
#"Sorted Rows" = Table.Sort(#"Expanded Previous",{{"Index", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Required", each [Sales] - (if [StoreID] = [Previous.StoreID] then [Previous.Sales] else 0), Int64.Type),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Index.1", "Previous.StoreID", "Previous.Sales"})
in
#"Removed Columns"
用户收到月度客户报告,其中 运行 每月总销售额(几万行)。销售额显示为 运行 总计,我试图找到一个 powerquery 解决方案来获得实际销售额作为(*必需*)输出:
╔══════╦═══════╦═════════╦═══════╦════════════╗
║ Year ║ Month ║ StoreID ║ Sales ║ *Required* ║
╠══════╬═══════╬═════════╬═══════╬════════════╣
║ 2017 ║ 10 ║ 1 ║ 5 ║ 5 ║
║ 2017 ║ 11 ║ 1 ║ 11 ║ 6 ║
║ 2017 ║ 12 ║ 1 ║ 18 ║ 7 ║
║ 2017 ║ 11 ║ 2 ║ 10 ║ 10 ║
╚══════╩═══════╩═════════╩═══════╩════════════╝
在 tSQL 中我会做类似
的事情Sales - LAG(Sales,1,0)OVER(Partiton by Year, StoreID Order by Month)
一个类似的问题(但相反)was answered,但建议的解决方案看起来相当于交叉连接。我是 powerquery 的新手(所以我可能会误解)但是随着数据集每个月都在增长,我看不到这是可行的。有没有更好的方法来解决这个问题,我在 "M" 中找不到示例?
编辑:List.Range 看起来很有希望
实际上您需要当前行上一行的值。 一种常见的方法是添加 2 个索引列,一个以 0 开头,另一个以 1 开头。接下来,使用索引列作为连接列将 table 与其自身合并。从嵌套的 table 中展开所需的列后,您可以添加具有所需计算的自定义列。
let
Source = Sales,
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
#"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1),
#"Merged Queries" = Table.NestedJoin(#"Added Index1",{"Index"},#"Added Index1",{"Index.1"},"Previous",JoinKind.LeftOuter),
#"Expanded Previous" = Table.ExpandTableColumn(#"Merged Queries", "Previous", {"StoreID", "Sales"}, {"Previous.StoreID", "Previous.Sales"}),
#"Sorted Rows" = Table.Sort(#"Expanded Previous",{{"Index", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Required", each [Sales] - (if [StoreID] = [Previous.StoreID] then [Previous.Sales] else 0), Int64.Type),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Index.1", "Previous.StoreID", "Previous.Sales"})
in
#"Removed Columns"