Forum Discussion
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 month | Company Name | Score | Category |
| March | A | 5 | x |
| March | B | 4 | x |
| April | A | 3 | y |
| March | B | 5 | y |
| March | C | 2 | x |
| March | C | 2 | y |
| April | A | 1 | x |
| March | A | 3 | z |
And I need a table like this:
| Company Name | March | April | Category |
| A | 5 | x | |
| B | 4 | x | |
| A | 3 | y | |
| B | 5 | y | |
| C | 2 | x | |
| C | 2 | y | |
| A | 1 | x | |
| A | 3 | z |
An alternative could also be:
| Company Name | March average score | April average score |
| A | 4 | 2 |
| B | 4,5 | |
| C | 2 |
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 regardsDenisBlog: WhatTheFact.biFollow me: twitter.com/DenSelimovic
1 Reply
- selimovd
Most 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 regardsDenisBlog: WhatTheFact.biFollow me: twitter.com/DenSelimovic