Forum Discussion
Replace Column Headers with current dates
- 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
Thanks. I already have some modifications prior to this step, so how do I change your code to accept the existing source? I get an error in the advanced editor saying the variable named 'source' is already defined.
you should replace source here
#"Renamed Columns" = Table.RenameColumns(Source,{"Week1", Text.From(Date.StartOfWeek(Date.From(DateTime.LocalNow())))}),to the name of your previous step,like you could see here (#"Renamed Columns" is a name of previous step)
#"Renamed Columns2" = Table.RenameColumns(#"Renamed Columns",{"Week2", Text.From(Date.StartOfWeek(Date.AddWeeks(Date.From(DateTime.LocalNow()),1)))})
do not hesitate to give a kudo to useful posts and mark solutions as solution
- jcbutts6 years agoHelper I
Hi Az38,
Sorry, that's not making sense to me. My code looks like this:
let
Source = Excel.Workbook(File.Contents("V:\Dpmts\Forecast.xlsx"), null, true),
#"US_Forecast and Inventory Plann_Sheet" = Source{[Item="US_Forecast and Inventory Plann",Kind="Sheet"]}[Data],
#"Removed Top Rows" = Table.Skip(#"US_Forecast and Inventory Plann_Sheet",1),
#"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ASIN", type text}})
in
#"Changed Type"I assume I need to keep the line calling out source= my excel workbook. Do I put in a new "let"-> "in" sequence or does your code get nested in to what I have?
- az386 years agoCommunity Champion
try
let Source = Excel.Workbook(File.Contents("V:\Dpmts\Forecast.xlsx"), null, true), #"US_Forecast and Inventory Plann_Sheet" = Source{[Item="US_Forecast and Inventory Plann",Kind="Sheet"]}[Data], #"Removed Top Rows" = Table.Skip(#"US_Forecast and Inventory Plann_Sheet",1), #"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]), #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ASIN", type text}}), #"Renamed Columns" = Table.RenameColumns(Source,{"Week1", Text.From(Date.StartOfWeek(Date.From(DateTime.LocalNow())))}), #"Renamed Columns2" = Table.RenameColumns(#"Renamed Columns",{"Week2", Text.From(Date.StartOfWeek(Date.AddWeeks(Date.From(DateTime.LocalNow()),1)))}), #"Renamed Columns3" = Table.RenameColumns(#"Renamed Columns2",{"Week3", Text.From(Date.StartOfWeek(Date.AddWeeks(Date.From(DateTime.LocalNow()),2)))}) in #"Renamed Columns3"do not hesitate to give a kudo to useful posts and mark solutions as solution
- Mariusz6 years agoCommunity Champion
Hi jcbutts
You can try this as an alternative.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i44FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Id = _t, #"week 1" = _t, #"week 2" = _t, #"week 3" = _t]), transformColumnNames = List.Transform( List.Select( Table.ColumnNames( Source ), ( i ) => Text.Contains( i, "week" ) ), (i) => let extractWeek = Number.From( Text.Replace( i, "week ", "" ) ), weekStart = Date.StartOfWeek( DateTime.LocalNow() ), newName = Text.From( Date.From( Date.AddWeeks( weekStart, extractWeek - 1 ) ) ) in { i, newName } ), #"Renamed Columns" = Table.RenameColumns(Source, transformColumnNames ) in #"Renamed Columns"Best Regards,
Mariusz
If this post helps, then please consider Accepting it as the solution.