Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Unpivot table with different fields

Hello everybody,

 

I have searched and read some posts about pivot and unpivot tables; however, my data is a bit differnet and very ugly, I need some help on the this:

 

Table I got:

IDMonthYearTeam/Score/MinutesValue 1 Value 2Value 3
a202002TeamTeam XTeam Y 
a202002Score55.7 
a202002Minutes6065 
b202001TeamTeam Z1Team Z2Team Z3
b202001Score866.2
b202001Minutes605560
b202001TeamTeam Z4Team Z5Team Z6
b202001Score8.14.87
b202001Minutes557080

 

I would like to transfrom into this:

IDMonthYearTeamScoreMinutes
a202002Team X560
a202002Team Y5.765
b202001Team Z1860
b202001Team Z2655
b202001Team Z36.260
b202001Team Z48.155
b202001Team Z54.870
b202001Team Z6780

 

I tried to create a new table in DAX with union but didn't succeed (https://community.powerbi.com/t5/Desktop/unpivot-table-with-milestones/m-p/951385#M455873). It seems my case is more dynamic (cuz Team/Score/Minutes) and don't know how to solve it.

 

Any advice would be appreciated!

 

5 REPLIES 5
Greg_Deckler
Super User
Super User

This is a job for @ImkeF or @edhans or someone of similar skill. I think it will involve an index column and a custom column that uses like MOD 3 or something and then you group on it. Not sure but those 2 will be able to help.

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi @trevorhh9 ,

please paste this code into the advaned editor of a new query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTIyMDIwMAIyQlITc6GUQgSMEQlkKMXqoCkNTs4vSgXSpiCsZ45VjW9mXmlJajGQZWYAIkxhqpJgqgzRLI0yhLOM4CxjTD0w2y1AxoKwnhGmIjTrTU0hLPwOMIGzTOEsMzwO0APxTPRADjHH5wSw7eYgd1gAnRALAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, MonthYear = _t, #"Team/Score/Minutes" = _t, #"Value 1 " = _t, #"Value 2" = _t, #"Value 3" = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
    #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 3), Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Integer-Divided Column", {"ID", "MonthYear", "Index"}, {{"Partition", each Table.PromoteHeaders(Table.Transpose(Table.RemoveColumns(_, {"ID", "MonthYear", "Index"})))}}),
    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Index"}),
    #"Expanded Partition" = Table.ExpandTableColumn(#"Removed Columns", "Partition", {"Team", "Score", "Minutes"}, {"Team", "Score", "Minutes"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Partition", each ([Team] <> ""))
in
    #"Filtered Rows"

It automatically wraps to more value columns.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Hello @ImkeF ,

 

Thanks for the reply.

I tried to use the query you provided in power bi with the real data. When I did the last step to "#Expanded Partition", it gave the null value for all rows.  It seems like I didn't use the column [Team/Scores/Munites] and maybe PBI didn't know what value to put back.

 

Or am I doing something wrong?

 

 

Hi @Anonymous 

Yes, you might have to replace the column names in the code.

You can also delete the last steps and do the expansion manually so you can see what's actually there.

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Hi @ImkeF ,

 

I found the issue that the column "Team/Scores/Munites" in my read data is not in the third column, so when I expand the table, it showed null as the order is wrong.

 

Thank you for the brilliant answer!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.