Forum Discussion

Kaitra's avatar
Kaitra
Frequent Visitor
5 years ago
Solved

Table transformation based on dynamic entries

Hello everybody,

 

i am having a problem which I cannot solve by myself.

 

I have a table that looks like this:

Date as monthCompany NameScoreCategory
MarchA5x
MarchB4x
AprilA3y
MarchB5y
MarchC2x
MarchC2y
AprilA1x
MarchA3z

 

And I need a table like this:

 

Company NameMarchAprilCategory
A5 x
B4 x
A 3y
B5 y
C2 x
C2 y
A 1x
A3 z

 

An alternative could also be:

Company NameMarch average scoreApril average score
A42
B4,5 
C2 

 

If a new entry with a new month is included, a new column needs to be created with the name of the month

Is there a possibility to create these tables ?

 

Thank you for supporting me here.

 

Cheers,

Kai

  • Hey Kaitra ,

     

    that should be possible.

    Mark the column [Date as month] and select pivot amd chose the score column for the values:

     

    Afterwards you should have the desired result:

     

     

    Here the full M-Code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8k0sSs5Q0lFyBGJTIK5QitVBiDoBsQlc1LGgKDMHqtYYiCsx1JpiiDoDsRGGuTDRSgxzDTHUwmyrUoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Date as month" = _t, #"Company Name" = _t, Score = _t, Category = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date as month", type text}, {"Company Name", type text}, {"Score", Int64.Type}, {"Category", type text}}),
        #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[#"Date as month"]), "Date as month", "Score", List.Sum)
    in
        #"Pivoted Column"

     

    If you need any help please let me know.
    If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
     
    Best regards
    Denis
     

1 Reply

  • selimovd's avatar
    selimovd
    Icon for Most Valuable Professional rankMost Valuable Professional

    Hey Kaitra ,

     

    that should be possible.

    Mark the column [Date as month] and select pivot amd chose the score column for the values:

     

    Afterwards you should have the desired result:

     

     

    Here the full M-Code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8k0sSs5Q0lFyBGJTIK5QitVBiDoBsQlc1LGgKDMHqtYYiCsx1JpiiDoDsRGGuTDRSgxzDTHUwmyrUoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Date as month" = _t, #"Company Name" = _t, Score = _t, Category = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date as month", type text}, {"Company Name", type text}, {"Score", Int64.Type}, {"Category", type text}}),
        #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[#"Date as month"]), "Date as month", "Score", List.Sum)
    in
        #"Pivoted Column"

     

    If you need any help please let me know.
    If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
     
    Best regards
    Denis