Forum Discussion
Calculate Week number difference in header column
- 5 years ago
Hello Anonymous
It's not 100% clear what you need. But if you have a table with all column names that can be transformed in dates and you would like to use the first column as as start and rename the other with the time distance in weeks then you can use this approach. But bear in mind that you will have problems whenever you have
- other column names that are not dates and should not be considered in the renaming
- if you have another date-format (like Feb 15) you need to find a way to transform your date into a week
- when you are crossing a year, my approach will not work anymore
Here the code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYmOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"15.02.21" = _t, #"22.02.21" = _t, #"02.03.21" = _t]), GetFirstColumnAsDate = Date.From(Table.ColumnNames(Source){0},"de-DE"), GetRenamedColumns = Table.TransformColumnNames ( Source, (columnName) => if Date.From(columnName, "de-DE") = GetFirstColumnAsDate then columnName else "W+" & Text.From(Date.WeekOfYear(Date.From(columnName))-Date.WeekOfYear(GetFirstColumnAsDate)) ) in GetRenamedColumnsbefore
after
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
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
Thank you,
It's working after some minor changes but it's kind of static.
First issue is there are other "non weeks" column before and the "weeks" column, that's why I wanted to dynamically rename the "weeks" columns like a "find & replace" of weeks column to a W+X format.
Again, post sample data with expected results. I know you know what you want, but I'm not going to guess again what you want and miss.
How to get good help fast. Help us help you.
How To Ask A Technical Question If you Really Want An Answer
How to Get Your Question Answered Quickly - Give us a good and concise explanation
How to provide sample data in the Power BI Forum - Provide data in a table format per the link, or share an Excel/CSV file via OneDrive, Dropbox, etc.. Provide expected output using a screenshot of Excel or other image. Do not provide a screenshot of the source data. I cannot paste an image into Power BI tables.