Power Query中是否有一种方法可以根据基于文本值重置的两列来计算运行总计?

时间:2019-04-10 18:51:22

标签: powerbi powerquery

我试图在每个新的会计年度开始时重置的每个会计年度的每个月的Power Query中创建一个运行总计。

我可以为整个列创建一个运行总计,但是我无法弄清楚每次ClosedMonth等于4月/会计年度更改时如何重置总计。

我想在Power Query中执行此操作,因为我正在从数据库中加载数据,并且希望表在安排Power BI刷新后每天自动更新。

Current Table called Monthly Sums

我试图为运行总计列实现的结果是:

Desired column result

1 个答案:

答案 0 :(得分:0)

创建名为fnAddRunningSum的函数

(MyTable as table) as table =>
//see:  https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/td-p/290123
let Source = Table.Buffer(MyTable),
TableType = Value.Type(Table.AddColumn(Source, "Running Sum", each null, type number)),
Cumulative = List.Accumulate(Source[Count],{},(cumulative,amount) => cumulative & {List.Last(cumulative, 0) + amount}),
AddedRunningSum = Table.FromColumns(Table.ToColumns(Source)&{Cumulative},TableType)
in AddedRunningSum

然后创建查询

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped" = Table.Group(Source, {"ClosedFiscalYear"}, {{"AllData", fnAddRunningSum}}),
#"Expanded AllData" = Table.ExpandTableColumn(#"Grouped", "AllData", {"ClosedMonth", "Count", "Index", "Running Sum"}, {"ClosedMonth", "Count", "Index", "Running Sum"})
in #"Expanded AllData"