简体   繁体   中英

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 )?

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.

在此处输入图像描述

在此处输入图像描述

I could produce monthly aggregates using Filter function and Sum, then pivot that data to produce above result.

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

Excel file in Zip

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

在此处输入图像描述

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

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

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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