Get previous year's sales data in custom columns from another Power Query

devtr

New Member
Joined
Jan 23, 2021
Messages
5
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Two power queries have sales data: Current Year and Previous Years.
The current Year has 2021 data.
Previous Years has 2019 and 2020 data.
Columns: Sales Date, Sales, Units

Current Year:
Sales DateSalesUnits
08-11-2021500001500
08-12-2021450001200
08-13-2021400001100
08-14-2021420001150
08-15-2021500001700
08-16-2021520001750
08-17-202135000800
08-18-202132000700


Previous Years:
Sales DateSalesUnits
08-11-201930000700
08-12-201935000800
08-13-201925000750
08-14-201927000700
08-15-201926000650
08-16-201920000550
08-17-201918000500
08-18-201920000700
08-11-202022000750
08-12-202028000900
08-13-202023000800
08-14-202022000700
08-15-202018000600
08-16-202019000650
08-17-202020000800
08-18-202018000800


I want to compare 2021 sales to the previous year's sales.
How do I bring historical data from Previous Years PQ to Current Year PQ with added custom columns for each date?
For Current Year PQ, columns will be Sales Date, Sales, Units, 2019 Sales, 2019 Units, 2020 Sales, 2020 Units.

New PQ for Current Year: (shows 08-17-2021 for example)
Sales DateSalesUnits2019 Sales2019 Units2020 Sales2020 Units
08-17-2021350008001800050020000800


I have very limited knowledge of Power Bi and PQ. This kind of situation can be taken care of in Excel with VLOOKUP and Index Match.
So, for the custom column 2019 Sales in Current Year PQ, what can be done to bring only 2019 sales value for every single date in 2021?
For example, for Aug 17, 2021, I want to bring Aug 17 2019 sales from Previous Years PQ.
Thank you for your help.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

JGordon11

Active Member
Joined
Jan 18, 2021
Messages
285
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales Date", type date}, {"Sales", Currency.Type}, {"Units", Int64.Type}}),
    #"Inserted Month" = Table.AddColumn(#"Changed Type", "Month", each Date.Month([Sales Date]), Int64.Type),
    #"Inserted Day" = Table.AddColumn(#"Inserted Month", "Day", each Date.Day([Sales Date]), Int64.Type),
    tblCurrent = Table.CombineColumns(Table.TransformColumnTypes(#"Inserted Day", {{"Month", type text}, {"Day", type text}}, "en-US"),{"Month", "Day"},Combiner.CombineTextByDelimiter("/", QuoteStyle.None),"Merged"),
    tblPrevious = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type1" = Table.TransformColumnTypes(tblPrevious,{{"Sales Date", type date}, {"Sales", Currency.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Sales", "2019 Sales"}, {"Units", "2019 Units"}}),
    #"Inserted Month1" = Table.AddColumn(#"Renamed Columns", "Month", each Date.Month([Sales Date]), Int64.Type),
    #"Inserted Day1" = Table.AddColumn(#"Inserted Month1", "Day", each Date.Day([Sales Date]), Int64.Type),
    #"Merged Columns1" = Table.CombineColumns(Table.TransformColumnTypes(#"Inserted Day1", {{"Month", type text}, {"Day", type text}}, "en-US"),{"Month", "Day"},Combiner.CombineTextByDelimiter("/", QuoteStyle.None),"Merged"),
    #"Filtered Rows" = Table.SelectRows(#"Merged Columns1", each [Sales Date] >= #date(2019, 1, 1) and [Sales Date] <= #date(2019, 12, 31)),
    tbl2020 = Table.SelectRows(#"Merged Columns1", each [Sales Date] >= #date(2020, 1, 1) and [Sales Date] <= #date(2020, 12, 31)),
    #"Renamed Columns1" = Table.RenameColumns(tbl2020,{{"2019 Sales", "2020  Sales"}, {"2019 Units", "2020 Units"}, {"Sales Date", "2020 Sales Date"}}),
    tbl20192020 = Table.Join(#"Filtered Rows", "Merged", #"Renamed Columns1", "Merged"),
    #"Removed Columns" = Table.RemoveColumns(tbl20192020,{"Sales Date", "2020 Sales Date"}),
    tblAll = Table.Join(tblCurrent, "Merged", #"Removed Columns", "Merged"),
    #"Removed Columns1" = Table.RemoveColumns(tblAll,{"Merged"})
in
    #"Removed Columns1"

PQ Tables Merge.xlsm
ABCDEFGHIJKLMNOP
1Sales DateSalesUnitsSales DateSalesUnitsSales DateSalesUnits2019 Sales2019 Units2020 Sales2020 Units
28/11/20215000015008/11/2019300007008/11/20215000015003000070022000750
38/12/20214500012008/12/2019350008008/12/20214500012003500080028000900
48/13/20214000011008/13/2019250007508/13/20214000011002500075023000800
58/14/20214200011508/14/2019270007008/14/20214200011502700070022000700
68/15/20215000017008/15/2019260006508/15/20215000017002600065018000600
78/16/20215200017508/16/2019200005508/16/20215200017502000055019000650
88/17/2021350008008/17/2019180005008/17/2021350008001800050020000800
98/18/2021320007008/18/2019200007008/18/2021320007002000070018000800
108/11/202022000750
118/12/202028000900
128/13/202023000800
138/14/202022000700
148/15/202018000600
158/16/202019000650
168/17/202020000800
178/18/202018000800
18
Sheet2
 

Forum statistics

Threads
1,147,747
Messages
5,742,966
Members
423,769
Latest member
LongToast

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
Top