Forum Discussion
jcbutts
6 years agoHelper I
Replace Column Headers with current dates
Hi, I'm new to this: I'm trying to replace column headers with the current week in one column and iterate from there. For example, I have columns that say "week 1", "week 2", "week 3".....et...
- 6 years ago
Hello jcbutts
I recognized that I got you wrong. So week 1 is always the current week.
I fixed this now. Check out this one if it works
let Source = #table ( {"week 1","week 2","week 3","other data","abc","week 52"}, { {"","","","","",""} } ), ColumnNames = Table.ColumnNames ( Source ), TransformWeekInDate = List.Transform ( ColumnNames, (row)=> try if Text.Contains(row,"week") or Text.Contains(row,"Week") then Text.From(Date.AddDays(Date.AddDays( Date.From(DateTime.FixedLocalNow()), Date.DayOfWeek(DateTime.FixedLocalNow())*-1), (Number.From(Text.Replace(row, "week ", ""))-1)*7)) else row otherwise row ), RenameColumnHeader = Table.RenameColumns ( Source, List.Zip({ColumnNames,TransformWeekInDate}) ) in RenameColumnHeaderJimmy
Jimmy801
6 years agoCommunity Champion
Hello jcbutts
this solution should be really dynamic and working for 2020. The column names have to be "week " and the number.
let
Source = #table
(
{"week 1","week 2","week 3","other data","abc","week 52"},
{
{"","","","","",""}
}
),
ColumnNames = Table.ColumnNames
(
Source
),
TransformWeekInDate = List.Transform
(
ColumnNames,
(row)=> try
if Text.Contains(row,"week") or Text.Contains(row,"Week") then
Text.From(Date.AddDays(#date(2019,12,30), (Number.From(Text.Replace(row, "week ", ""))-1)*7))
else
row
otherwise
row
),
RenameColumnHeader = Table.RenameColumns
(
Source,
List.Zip({ColumnNames,TransformWeekInDate})
)
in
RenameColumnHeader
Copy paste this code to the advanced editor to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy