简体   繁体   English

Excel(或)Power BI,滚动总和

[英]Excel (or) Power BI, Rolling Sum

Is there any way in Excel Pivot or Power BI to do the rolling sum of the given data (let say monthly )?在 Excel Pivot 或 Power BI 中有什么方法可以对给定数据进行滚动求和(比如每月)?

Let say I have a list of cases, each row represent case count and amount.假设我有一个案例列表,每一行代表案例数量和数量。 The project start date and end date varied as follows.项目开始日期和结束日期变化如下。

在此处输入图像描述

For, simplicity, if I demonstrate the data graphically, would be as follows.为了简单起见,如果我以图形方式演示数据,将如下所示。

在此处输入图像描述

What I'm try to do is to aggregate how much case counts and amounts in total for each chunk of month.我想做的是汇总每个月份的案例数量和总金额。

My goal is to produce below list using Pivot (if Pivot is not possible, then by Power Query) directly.我的目标是直接使用 Pivot(如果 Pivot 不可能,则通过 Power Query)生成以下列表。

在此处输入图像描述

在此处输入图像描述

I could produce monthly aggregates using Filter function and Sum, then pivot that data to produce above result.我可以使用过滤器 function 和 Sum 生成月度聚合,然后 pivot 该数据生成上述结果。

If there is a direct way of producing that aggregates in one step, that would be better.如果有一种直接的方法可以在一个步骤中生产这种聚集体,那就更好了。 Please suggest it for me.请为我推荐它。

Please see sample data in below link请查看以下链接中的示例数据

https://docs.google.com/spreadsheets/d/1vAKElb2-V_If-MMlPwHk_VGhYr8pkOg_gQfRYRrkbtc/edit?usp=share_link https://docs.google.com/spreadsheets/d/1vAKElb2-V_If-MMlPwHk_VGhYr8pkOg_gQfRYRrkbtc/edit?usp=share_link

Excel file in Zip Excel 文件在 Zip

https://drive.google.com/file/d/1QqgNUrJlBuvin7iecsxsvexrGZXFIt-g/view?usp=share_link https://drive.google.com/file/d/1QqgNUrJlBuvin7iecsxsvexrGZXFIt-g/view?usp=share_link

Thank you in advance先感谢您

LuZ陆志

You can load the data into powerquery and transform from left to data.table on right您可以将数据加载到 powerquery 并从左侧转换为右侧的 data.table

在此处输入图像描述

code for that is代码是

let  Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom1" = Table.AddColumn(Source, "Date", each List.Generate(()=>[x=[Start Date],i=0], each [i]<12, each [i=[i]+1,x=Date.AddMonths([x],1)], each [x])),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom1", "Date"),
#"Added Custom" = Table.AddColumn(#"Expanded Custom", "Year", each Date.Year([Date])),
#"Added Custom2" = Table.AddColumn(#"Added Custom", "Month", each Date.Month([Date])),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Start Date", "End Date", "Date"})
in  #"Removed Columns"

Afterwards, load the powerquery back into excel as pivot report and generate your table之后,将 powerquery 加载回 excel 作为 pivot 报告并生成您的表

在此处输入图像描述 在此处输入图像描述

Alternatively, just use use或者,只需使用

let  Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom1" = Table.AddColumn(Source, "Date", each List.Generate(()=>[x=[Start Date],i=0], each [i]<12, each [i=[i]+1,x=Date.AddMonths([x],1)], each [x])),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom1", "Date"),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Start Date", "End Date"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Date"}, {{"Amount", each List.Sum([Amount]), type number}, {"Case Count", each List.Sum([Case Count]), type number}}),
#"Changed Type" = Table.TransformColumnTypes(#"Grouped Rows",{{"Date", type date}, {"Amount", type number}, {"Case Count", type number}})
in   #"Changed Type"

to generate this table, then graph it生成这个表,然后绘制它

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM