Forum Discussion

freelensia's avatar
freelensia
Icon for Advocate II rankAdvocate II
5 years ago
Solved

Format date of column headers after pivoting

I have a table with country, date, and a value column. I want to pivot the date column so that I can see the value progressing over each day (each row in the table is a Monday of the week).

 

let
    Source = AdvWeeklyStats,
    DelCol = Table.RemoveColumns(Source,{"New infections", "New deaths", "Total deaths", "Day of Week"}),
    SortByDate = Table.Sort(DelCol,{{"Date", Order.Ascending}, {"Country", Order.Ascending}}),
    PivotCol = Table.Pivot(Table.TransformColumnTypes(SortByDate, {{"Date", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(SortByDate, {{"Date", type text}}, "en-US")[Date]), "Date", "Total infections")
in
    PivotCol

 

 

However after pivoting my date column headers are displayed in m-d-yyyy format.

How can I display them in yyyy/m format? Keeping in mind I do not want a monthly aggregate ! Each record must still pivot to its own column, and there should be 4 columns for each month like this:

 

2020/3 2020/3 2020/3 2020/3 2020/4 2020/4 2020/4 2020/4 2020/5 ...

 

 

If I change en-US to ja-JP then I get the dates formatted as yyyy/mm/dd which is a good start, but not what I need. I looked at this thread but the inner function was too complicated for me to replicate in my case.

 

Can anybody help me?
Jimmy801 Greg_Deckler @amitchandak @parry2k @Mariusz @ImkeF 

  • Hello freelensia 

     

    column headers have to be unique. So why not using weeks instead of months? Here an example

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TczBDcAgDEPRXXxGyEkoPXeH3lD2XwNQq4Sb9WT9MfC8KKBWWlWuKfDyq/RQTVULtVRjaEvlKrRPr6NrW2VrP7qM7w33CQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Country = _t, Date = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}, {"Date", type date}, {"Value", Int64.Type}}, "de-DE"),
        #"Calculated Week of Year" = Table.TransformColumns(#"Changed Type",{{"Date", each Text.From(Date.WeekOfYear(_))&"/"&Text.From(Date.Year(_)), type text}}),
        #"Pivoted Column" = Table.Pivot(#"Calculated Week of Year", List.Distinct(#"Calculated Week of Year"[Date]), "Date", "Value", List.Sum)
    in
        #"Pivoted Column"

     

    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

1 Reply

  • Jimmy801's avatar
    Jimmy801
    Icon for Community Champion rankCommunity Champion

    Hello freelensia 

     

    column headers have to be unique. So why not using weeks instead of months? Here an example

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TczBDcAgDEPRXXxGyEkoPXeH3lD2XwNQq4Sb9WT9MfC8KKBWWlWuKfDyq/RQTVULtVRjaEvlKrRPr6NrW2VrP7qM7w33CQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Country = _t, Date = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}, {"Date", type date}, {"Value", Int64.Type}}, "de-DE"),
        #"Calculated Week of Year" = Table.TransformColumns(#"Changed Type",{{"Date", each Text.From(Date.WeekOfYear(_))&"/"&Text.From(Date.Year(_)), type text}}),
        #"Pivoted Column" = Table.Pivot(#"Calculated Week of Year", List.Distinct(#"Calculated Week of Year"[Date]), "Date", "Value", List.Sum)
    in
        #"Pivoted Column"

     

    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