Transform Column Header only MM/DD information for several years

PowerQueryUser

New Member
Joined
Aug 27, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I am facing a power query challenge.
I have a csv file with many line items.

The first columns are some material descriptions.
The following columns are weekly dates MM/DD, but without year information.
The csv file covers around 3 years.
Following an example of the structure. Please consider each MM/DD represents one column header.
Just for explanation I added the year and separated in different lines.

2021:
8/1 8/8 8/15 8/22 8/29 9/1 9/5 9/12 9/19 9/26 10/1 10/3 10/10 10/17 10/24 10/31 11/1 11/7 11/14 11/21 11/28 12/1 12/5 12/12 12/19 12/26

2022:
1/1 1/2 1/9 1/16 1/23 1/30 2/1 2/6 2/13 2/20 2/27 3/1 3/6 3/13 3/20 3/27 4/1 4/3 4/10 4/17 4/24 5/1 5/8 5/15 5/22 5/29 6/1 6/5 6/12 6/19 6/26 7/1 7/3 7/10 7/17 7/24 7/31 8/1 8/7 8/14 8/21 8/28 9/1 9/4 9/11 9/18 9/25 10/1 10/2 10/9 10/16 10/23 10/30 11/1 11/6 11/13 11/20 11/27 12/1 12/4 12/11 12/18 12/25

2023:
1/1 1/8 1/15 1/22 1/29 2/1 2/5 2/12 2/19 2/26 3/1 3/5 3/12 3/19 3/26 4/1 4/2 4/9 4/16 4/23 4/30 5/1 5/7 5/14 5/21 5/28 6/1 6/4 6/11 6/18 6/25 7/1 7/2 7/9 7/16 7/23 7/30 8/1 8/6 8/13 8/20 8/27


In the consecutive power query I unpivot the dates.
Then I format each date as data type date.
The issue is, that all dates are formatted with the current year, e.g. 1st August 2021.
I could solve the issue for two years with a conditional column and comparing if the line item date is in the past. Then I knew, that the line time must be in 2022.
But now facing also 2023, this approach does not work anymore.

Do you have any idea, about a logic to format the columns with correct years?

You might recognize, that the dates are basically a weekly pattern. But each month starts also with an additional column. e.g. 29th Aug, 1st Sept, 5th Sept.

I really appreciate a hint, what kind of logic I could apply to solve this challenge.
Thank you very much.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
By any chance do you have the year in the file names?
It must be somewhere in the data or else the logic may become dodgy.
Owe, wait, only 1 file...

Again not sustainable in the longer run, but you could try this:
- make a calendar table for these 3 years
- keep only Mondays
- add the year column
- add a column to match mm/dd
- merge both table
- extract the year

Would need to find a logic where the in the week you have a new year. Maybe your column with new month might help.
 
Last edited:
Upvote 0
If your out years will always include a 1/1 column and the total years is always 3 this may work.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Conditional Column" = Table.AddColumn(#"Added Index", "Custom", each if Date.Month([Date]) = 1 and Date.Day([Date])=1 then [Index] else -1),
    lstJanOneRows = List.MaxN( #"Added Conditional Column"[Custom],2),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each 
            if [Index] <lstJanOneRows{1} then [Date]
            else if [Index] <lstJanOneRows{0} then Date.AddYears([Date], 1) else Date.AddYears([Date], 2)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date", "Index"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Date"}})
in
    #"Renamed Columns"

pq DateConversion.xlsm
ABCDE
4DescDateDescDate
5Desc18/1/2021Desc18/1/2021
6Desc28/8/2021Desc28/8/2021
7Desc38/15/2021Desc38/15/2021
8Desc48/22/2021Desc48/22/2021
9Desc58/29/2021Desc58/29/2021
10Desc69/1/2021Desc69/1/2021
11Desc79/5/2021Desc79/5/2021
12Desc89/12/2021Desc89/12/2021
13Desc99/19/2021Desc99/19/2021
14Desc109/26/2021Desc109/26/2021
15Desc1110/1/2021Desc1110/1/2021
16Desc1210/3/2021Desc1210/3/2021
17Desc1310/10/2021Desc1310/10/2021
18Desc1410/17/2021Desc1410/17/2021
19Desc1510/24/2021Desc1510/24/2021
20Desc1610/31/2021Desc1610/31/2021
21Desc1711/1/2021Desc1711/1/2021
22Desc1811/7/2021Desc1811/7/2021
23Desc1911/14/2021Desc1911/14/2021
24Desc2011/21/2021Desc2011/21/2021
25Desc2111/28/2021Desc2111/28/2021
26Desc2212/1/2021Desc2212/1/2021
27Desc2312/5/2021Desc2312/5/2021
28Desc2412/12/2021Desc2412/12/2021
29Desc2512/19/2021Desc2512/19/2021
30Desc2612/26/2021Desc2612/26/2021
31Desc271/1/2021Desc271/1/2022
32Desc281/2/2021Desc281/2/2022
33Desc291/9/2021Desc291/9/2022
34Desc301/16/2021Desc301/16/2022
35Desc311/23/2021Desc311/23/2022
36Desc321/30/2021Desc321/30/2022
37Desc332/1/2021Desc332/1/2022
38Desc342/6/2021Desc342/6/2022
39Desc352/13/2021Desc352/13/2022
40Desc362/20/2021Desc362/20/2022
41Desc372/27/2021Desc372/27/2022
42Desc383/1/2021Desc383/1/2022
43Desc393/6/2021Desc393/6/2022
44Desc403/13/2021Desc403/13/2022
45Desc413/20/2021Desc413/20/2022
46Desc423/27/2021Desc423/27/2022
47Desc434/1/2021Desc434/1/2022
48Desc444/3/2021Desc444/3/2022
49Desc454/10/2021Desc454/10/2022
50Desc464/17/2021Desc464/17/2022
51Desc474/24/2021Desc474/24/2022
52Desc485/1/2021Desc485/1/2022
53Desc495/8/2021Desc495/8/2022
54Desc505/15/2021Desc505/15/2022
55Desc515/22/2021Desc515/22/2022
56Desc525/29/2021Desc525/29/2022
57Desc536/1/2021Desc536/1/2022
58Desc546/5/2021Desc546/5/2022
59Desc556/12/2021Desc556/12/2022
60Desc566/19/2021Desc566/19/2022
61Desc576/26/2021Desc576/26/2022
62Desc587/1/2021Desc587/1/2022
63Desc597/3/2021Desc597/3/2022
64Desc607/10/2021Desc607/10/2022
65Desc617/17/2021Desc617/17/2022
66Desc627/24/2021Desc627/24/2022
67Desc637/31/2021Desc637/31/2022
68Desc648/1/2021Desc648/1/2022
69Desc658/7/2021Desc658/7/2022
70Desc668/14/2021Desc668/14/2022
71Desc678/21/2021Desc678/21/2022
72Desc688/28/2021Desc688/28/2022
73Desc699/1/2021Desc699/1/2022
74Desc709/4/2021Desc709/4/2022
75Desc719/11/2021Desc719/11/2022
76Desc729/18/2021Desc729/18/2022
77Desc739/25/2021Desc739/25/2022
78Desc7410/1/2021Desc7410/1/2022
79Desc7510/2/2021Desc7510/2/2022
80Desc7610/9/2021Desc7610/9/2022
81Desc7710/16/2021Desc7710/16/2022
82Desc7810/23/2021Desc7810/23/2022
83Desc7910/30/2021Desc7910/30/2022
84Desc8011/1/2021Desc8011/1/2022
85Desc8111/6/2021Desc8111/6/2022
86Desc8211/13/2021Desc8211/13/2022
87Desc8311/20/2021Desc8311/20/2022
88Desc8411/27/2021Desc8411/27/2022
89Desc8512/1/2021Desc8512/1/2022
90Desc8612/4/2021Desc8612/4/2022
91Desc8712/11/2021Desc8712/11/2022
92Desc8812/18/2021Desc8812/18/2022
93Desc8912/25/2021Desc8912/25/2022
94Desc901/1/2021Desc901/1/2023
95Desc911/8/2021Desc911/8/2023
96Desc921/15/2021Desc921/15/2023
97Desc931/22/2021Desc931/22/2023
98Desc941/29/2021Desc941/29/2023
99Desc952/1/2021Desc952/1/2023
100Desc962/5/2021Desc962/5/2023
101Desc972/12/2021Desc972/12/2023
102Desc982/19/2021Desc982/19/2023
103Desc992/26/2021Desc992/26/2023
104Desc1003/1/2021Desc1003/1/2023
105Desc1013/5/2021Desc1013/5/2023
106Desc1023/12/2021Desc1023/12/2023
107Desc1033/19/2021Desc1033/19/2023
108Desc1043/26/2021Desc1043/26/2023
109Desc1054/1/2021Desc1054/1/2023
110Desc1064/2/2021Desc1064/2/2023
111Desc1074/9/2021Desc1074/9/2023
112Desc1084/16/2021Desc1084/16/2023
113Desc1094/23/2021Desc1094/23/2023
114Desc1104/30/2021Desc1104/30/2023
115Desc1115/1/2021Desc1115/1/2023
116Desc1125/7/2021Desc1125/7/2023
117Desc1135/14/2021Desc1135/14/2023
118Desc1145/21/2021Desc1145/21/2023
119Desc1155/28/2021Desc1155/28/2023
120Desc1166/1/2021Desc1166/1/2023
121Desc1176/4/2021Desc1176/4/2023
122Desc1186/11/2021Desc1186/11/2023
123Desc1196/18/2021Desc1196/18/2023
124Desc1206/25/2021Desc1206/25/2023
125Desc1217/1/2021Desc1217/1/2023
126Desc1227/2/2021Desc1227/2/2023
127Desc1237/9/2021Desc1237/9/2023
128Desc1247/16/2021Desc1247/16/2023
129Desc1257/23/2021Desc1257/23/2023
130Desc1267/30/2021Desc1267/30/2023
131Desc1278/1/2021Desc1278/1/2023
132Desc1288/6/2021Desc1288/6/2023
133Desc1298/13/2021Desc1298/13/2023
134Desc1308/20/2021Desc1308/20/2023
135Desc1318/27/2021Desc1318/27/2023
Sheet2
 
Upvote 0
@GraH, @JGordon11 thank you so much for your quick response and great help.
@JGordon11, I could rebuild your code in a simplified testing file. It is working great. Next week I will try to implement it into my data file. I will come back with some feedback.

Really great help. Especially as I am not yet experienced with power query.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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