Calculate YTD values with Power Query

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I have a data table containing rows of monthly data, similar to below,

CustomerPeriodVolumeRevenueMargin
A8/1/2016302010
A9/1/2016302010
B7/1/20161052
B9/1/2016842

<tbody>
</tbody>

I would like to use Power Query to create a table that outputs the following:

CustomerMTD - VolumeMTD - RevenueMTD - MarginYTD - VolumeYTD - RevenueYTD - Margin
A302010604020
B8421894

<tbody>
</tbody>

This assumes that the "current" month is September 2016 (not necessarily always the latest month of the data, so the month designated as current needs to be a parameter type selection).

At first I thought this would be fairly simple but when I actually started working on it, I am drawing a blank.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Date 9/30/2016 in TableCurrent with column label Current.
Data in Table1.

Code:
let
    Current = Date.From(Excel.CurrentWorkbook(){[Name="TableCurrent"]}[Content][Current]{0}),
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Period", type date}, {"Volume", Int64.Type}, {"Revenue", Int64.Type}, {"Margin", Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each [Period] <= Current and [Period] >= #date(Date.Year(Current), 1, 1)),
    #"Added MTD Volume" = Table.AddColumn(#"Filtered Rows", "MTD Volume", each if Date.Month([Period]) = Date.Month(Current) then [Volume] else 0),
    #"Added MTD Revenue" = Table.AddColumn(#"Added MTD Volume", "MTD Revenue", each if Date.Month([Period]) = Date.Month(Current) then [Revenue] else 0),
    #"Added MTD Margin" = Table.AddColumn(#"Added MTD Revenue", "MTD Margin", each if Date.Month([Period]) = Date.Month(Current) then [Margin] else 0),
    #"Grouped Rows" = Table.Group(#"Added MTD Margin", {"Customer"}, 
                        {{"YTD Volume", each List.Sum([Volume]), type number}, 
                         {"YTD Revenue", each List.Sum([Revenue]), type number}, 
                         {"YTD Margin", each List.Sum([Margin]), type number}, 
                         {"MTD Volume", each List.Sum([MTD Volume]), type number}, 
                         {"MTD Revenue", each List.Sum([MTD Revenue]), type number}, 
                         {"MTD Margin", each List.Sum([MTD Margin]), type number}}),
    #"Reorder Columns" = Table.ReorderColumns(#"Grouped Rows",{"Customer","MTD Volume","MTD Revenue", "MTD Margin","YTD Volume","YTD Revenue", "YTD Margin"})
in
    #"Reorder Columns"
 
Upvote 0
Marcel - trying to replicate the code posted here - Calculate YTD values with Power Query & pasted below. reachable @ drew@bambic.com


let
Current = Date.From(Excel.CurrentWorkbook(){[Name="TableCurrent"]}[Content][Current]{0}),
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Period", type date}, {"Volume", Int64.Type}, {"Revenue", Int64.Type}, {"Margin", Int64.Type}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [Period] <= Current and [Period] >= #date(Date.Year(Current), 1, 1)),
#"Added MTD Volume" = Table.AddColumn(#"Filtered Rows", "MTD Volume", each if Date.Month([Period]) = Date.Month(Current) then [Volume] else 0),
#"Added MTD Revenue" = Table.AddColumn(#"Added MTD Volume", "MTD Revenue", each if Date.Month([Period]) = Date.Month(Current) then [Revenue] else 0),
#"Added MTD Margin" = Table.AddColumn(#"Added MTD Revenue", "MTD Margin", each if Date.Month([Period]) = Date.Month(Current) then [Margin] else 0),
#"Grouped Rows" = Table.Group(#"Added MTD Margin", {"Customer"},
{{"YTD Volume", each List.Sum([Volume]), type number},
{"YTD Revenue", each List.Sum([Revenue]), type number},
{"YTD Margin", each List.Sum([Margin]), type number},
{"MTD Volume", each List.Sum([MTD Volume]), type number},
{"MTD Revenue", each List.Sum([MTD Revenue]), type number},
{"MTD Margin", each List.Sum([MTD Margin]), type number}}),
#"Reorder Columns" = Table.ReorderColumns(#"Grouped Rows",{"Customer","MTD Volume","MTD Revenue", "MTD Margin","YTD Volume","YTD Revenue", "YTD Margin"})
in
#"Reorder Columns"
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top