Excel (or) Power BI, Rolling Sum - excel

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

Related

Excel Power Query - Filter Rows by Pivoted results

I'm working to analyze a Status Change over time. I have a large table in Excel as:
ID
Date
Status
01
Aug-01
Pending
01
Aug-02
Pending
01
Aug-03
Pending
02
Aug-01
Pending
02
Aug-02
Pending
02
Aug-03
Assigned
There are thousands of rows of source data... I am only looking at data from the past 7 days. Essentially I'm looking for change activity since the last status report.
I use Power query to read the table and then pivot the data so I get the following results:
ID
Aug-01
Aug-02
Aug-03
01
Pending
Pending
Pending
02
Pending
Pending
Assigned
I only expect a dozen or so rows (ID's) to be reported each week with an ever changing set of pivoted date columns as we progress through the year.
I want to get rid of the rows of data where each column is exactly the same... Each day has a full set of data including historically closed items. I only want to see data where there is a change in the series from the original table.
I'd be happy to do it prior to the Pivot, honestly that may be the better approach, but I don't understand how to remove rows where based upon the ID and the Date where the Status doesn't change.
I'm currently pushing the data from the Pivot
Any suggestions would be so appreciated!
Thanks
Rob
Try this code, on the first table in powerquery. Pulls last 7 days. Pivots. Checks if the contents of all the columns are identical, then filters on that
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Date", type date}, {"Status", type text}}),
//dates from last seven days, to examine
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [Date] >Date.AddDays(DateTime.Date(DateTime.LocalNow()),-7)),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Filtered Rows", {{"Date", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Filtered Rows", {{"Date", type text}}, "en-US")[Date]), "Date", "Status"),
coltocheck = List.RemoveFirstN(Table.ColumnNames(#"Pivoted Column"),1),
IDX = Table.AddIndexColumn(#"Pivoted Column","Index",0,1),
totals = Table.AddColumn(IDX, "Custom", each List.Count(List.Distinct(Record.ToList( Table.SelectColumns(IDX,coltocheck){[Index]}) ))),
#"Filtered Rows1" = Table.SelectRows(totals, each ([Custom] = 2)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows1",{"Index", "Custom"})
in #"Removed Columns"
Alternate version that uses grouping
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Date", type date}, {"Status", type text}}),
//dates from last seven days, to examine
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [Date] >Date.AddDays(DateTime.Date(DateTime.LocalNow()),-7)),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"ID"}, {{"Count", each _, type table }, {"Unique", each List.Count(List.Distinct(_[Status]))}}),
#"Filtered Rows1" = Table.SelectRows(#"Grouped Rows", each ([Unique] = 2)),
#"Expanded Count" = Table.ExpandTableColumn(#"Filtered Rows1", "Count", {"Date", "Status"}, {"Date", "Status"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Count",{"Unique"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"Date", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Date", type text}}, "en-US")[Date]), "Date", "Status")
in #"Pivoted Column"

Pivot columns with multiple instance (rows) of attribute

I've searched far and wide and haven't found an answer to this specific case, and wasn't able to adapt some of these solutions.
First of all, my data is a long list of attributes and their values for every product, structured like this:
Structured Initial Data
Note that some products have a single value per attributes, but (and here's my problem) some products have different values for the same attribute.
When I pivot the table in PowerQuery, i get errors where the products have multiple instances of the same attributes.
The resulting table that i'm looking for would be structured like this:
Structured Final Data
Thank you for your help!
See if this works for you
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Sorted Rows" = Table.Sort(Source,{{"Products", Order.Ascending}, {"Attributes", Order.Ascending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"Products"}, {{"data", each _, type table}}),
#"Added Index1" = Table.AddIndexColumn(#"Grouped Rows", "Index", 0, 1),
#"Expanded data" = Table.ExpandTableColumn(#"Added Index1", "data", {"Attributes", "Values"}, {"Attributes", "Values"}),
mGroup = Table.Group(#"Expanded data" , {"Attributes","Products"}, {{"GRP", each Table.AddIndexColumn(_, "Index2", 1, 1), type table}}),
#"Expanded GRP" = Table.ExpandTableColumn(mGroup, "GRP", {"Values", "Index", "Index2"}, {"Values", "Index", "Index2"}),
#"Added Custom" = Table.AddColumn(#"Expanded GRP", "Row#", each [Index]+[Index2]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Index2"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attributes]), "Attributes", "Values"),
#"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Row#"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"Products", "Each", "Pack"})
in #"Reordered Columns"
It groups on product and adds an index. Then it groups on product and Attribute and adds another index. The sum of those two are a unique row number you can use for pivoting

Power Query - Weighted average

I have the following table on Power Query that contains a quantity of a product bought and his price:
I want to create another third column with the current average purchase price, however, I need to consider the history and the average should be weighted:
Can anyone help me?
To calculate a running weighted average, you merely have to generate a running total cost column (Price x Quantity) and a running total quanity column, then divide one by the other:
There are various ways to do this, and you don't need to do this by adding columns to the table, but I did it so you can examine the steps involved.
M-Code
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type text}, {"Quantity", Int64.Type}, {"Price", type number}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "Cost", each [Quantity]*[Price]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "runTotCost", each List.Sum(List.Range(#"Added Custom"[Cost],0,[Index]))),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "runTotQuant", each List.Sum(List.Range(#"Added Custom"[Quantity],0,[Index]))),
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "Weighted Average", each [runTotCost] / [runTotQuant]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Index", "Cost", "runTotCost", "runTotQuant"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Weighted Average", type number}})
in
#"Changed Type1"
For better understanding, I suggest you do an internet search for methods of generating running totals. There are other methods that are more complicated, but more efficient.

Create a different pivot view in Power Query

I have the data structured in excel in the following format
What I want to do with that is to transform it into this. In simple words for each ID I want to record the difference in value from previous day, and if there is no value in previous day we just keep the current value.
As an intermediate step I am trying to transform the raw data into something like this but I am not sure how to go about it in simple Excel pivot tables, or Power query transformations.
There is something wrong with your sample because [v1-v2] is not the same method as [v5-v4, v3-v2, v8-v7] but I assume the latter ones were right
See if this works for you
Assumes data in 3 columns in a range named Table1 with column headers Dates, ID, Value
You can paste into PowerQuery using ... Advanced Editor ...
Creates a column with the value of yesterday for that ID and returns a null if nothing is found. Then does the subtraction, and pivots
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Dates", type date}, {"ID", type text}, {"Value", Int64.Type}}),
Yesterday = Table.AddColumn(#"Changed Type" , "Yesterday", (i) => List.Sum(Table.SelectRows( #"Changed Type", each ([ID] = i[ID] and Date.AddDays([Dates],1) = i[Dates]))[Value]), type number ),
#"Replaced Value" = Table.ReplaceValue(Yesterday,null,0,Replacer.ReplaceValue,{"Yesterday"}),
#"Added Custom" = Table.AddColumn(#"Replaced Value", "Custom", each [Value]-[Yesterday]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value", "Yesterday"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"Dates", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Dates", type text}}, "en-US")[Dates]), "Dates", "Custom", List.Sum)
in #"Pivoted Column"

Power Query transpose and pivot list

I have the following list in Excel Powerquery:
I would like to transform this list within the Power Query editor to make the following list:
Assuming a source table called Table1, you could use this (in the advanced editor):
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Buffered = Table.Buffer(Source),
#"Grouped Rows" = Table.Group(Buffered, {"value"}, {{"AllRows", each Table.AddIndexColumn(_, "Record", 1, 1), type table}}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"value2", "Record"}, {"value2", "Record"}),
#"Pivoted Column" = Table.Pivot(#"Expanded AllRows", List.Distinct(#"Expanded AllRows"[value]), "value", "value2"),
#"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Record"})
in
#"Removed Columns"

Resources