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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

power query transpose

Hi guys

I'm trying to transpose this table but i can't do that. Maybe i'm not looking well.

Can you guys give me help?

The idea is to have this result...

Name           Age      Code1  Code2

Pedro15ASJA
Luis17AS 
João16ASJA
Carlos18AS 
Manuel15ASJA

 

from this table...

 

Name          Age      Code

Pedro15AS
Pedro15JA
Luis17AS
João16AS
João16JA
Carlos18AS
Manuel15AS
Manuel15JA

 

Anyone?

Thanks in advance

Best regards

Pedro

1 ACCEPTED SOLUTION

I created a calculated table for you like this:

image.png

NewTable = 
VAR names = DISTINCT('Table'[Name])
RETURN
ADDCOLUMNS(
    SELECTCOLUMNS(names, "Names", [Name]), 
    "Age", CALCULATE(AVERAGE('Table'[Age]), FILTER('Table', 'Table'[Name] = [Names])),
    "Code1", IF(CONTAINS('Table', 'Table'[Name], [Names], 'Table'[Code], "AS"), "AS", BLANK()),
    "Code2", IF(CONTAINS('Table', 'Table'[Name], [Names], 'Table'[Code], "JA"), "JA", BLANK())
)

If this doesn't work in your case, please explain in detail how PowerBI should know what value should be in Code1 and Code2 value. 

 

Kind regards

Djerro123

-------------------------------

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Keep those thumbs up coming! 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

9 REPLIES 9
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkhNKcpX0lEyNAUSjsFKsTpoYl6OYDGf0sxikJA5QplX/uHFYGVmOMSgWp0Ti3LywZotEAp9E/NKU3PQLEYVBGmPBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Name = _t, Age = _t, Code = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Age", Int64.Type}, {"Code", type text}}),
    Partition = Table.Group(#"Changed Type", {"Name"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Age", "Code", "Index"}, {"Age", "Code", "Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Partition", "Custom", each "Code "&Number.ToText([Index])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Code")
in
    #"Pivoted Column"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-yuta-msft
Community Support
Community Support

@Anonymous ,

 

That's much more simple. Click on column Code1 and Code2, then click Transform-> Unpivot Columns. Remove and Attribute column and rename Value column with "Code". Then remove the blank value in the Code column using filter as below:

Capture.PNG 

 

Finally, you will achieve the table.

2.PNG 

 

Community Support Team _ Jimmy Tao

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hey @v-yuta-msft 

I'm very sorry to say but you got it all wrong. The final solution that you've presented is what i have now, and what i want to transform. Could you please read my original/first message. You'll understand what i need...

Thank you so much for your effort

@Anonymous ,

 

Hi, please see the previous solution I have post.

 

Regards,

Jimmy Tao

I created a calculated table for you like this:

image.png

NewTable = 
VAR names = DISTINCT('Table'[Name])
RETURN
ADDCOLUMNS(
    SELECTCOLUMNS(names, "Names", [Name]), 
    "Age", CALCULATE(AVERAGE('Table'[Age]), FILTER('Table', 'Table'[Name] = [Names])),
    "Code1", IF(CONTAINS('Table', 'Table'[Name], [Names], 'Table'[Code], "AS"), "AS", BLANK()),
    "Code2", IF(CONTAINS('Table', 'Table'[Name], [Names], 'Table'[Code], "JA"), "JA", BLANK())
)

If this doesn't work in your case, please explain in detail how PowerBI should know what value should be in Code1 and Code2 value. 

 

Kind regards

Djerro123

-------------------------------

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Keep those thumbs up coming! 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




v-yuta-msft
Community Support
Community Support

@Anonymous ,

 

In query editor, click on column [Code] then click Pivot using Values Column [Age] as below:

1.PNG 

 Then add two conditional columns as below:

2.PNG3.PNG

 

Finally, remove column [JA] and rename column [AS] with [Age]:

4.PNG 

 

Community Support Team _ Jimmy Tao

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @v-yuta-msft 

Thank you very much for your reply but your solution starts with my desired solution....
Could you please think of a way of doing it, from my original table, from the 2nd table?

Thank you so much

Pedro

JarroVGIT
Resident Rockstar
Resident Rockstar

Hi @Anonymous 

 

I've recreated your table like this:

image.png

In Query Editor, select column Code1 and Code 2 and hit Unpivot Columns in the Transform ribbon. This results in this:

image.png

Remove column Attribute and filterout the blanks in column Value and you have your desired result.

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Keep those thumbs up coming! 🙂

Kind regards

Djerro123

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Hi @JarroVGIT 

Thanks for trying to help me but....when you say "i've recreated your table..." this is how i'd like the result to be, you know? Maybe i wasn't explicit enough. So sorry about it.
I can't have any duplicates on names and i have to see for each name if there's an AS or a JA or both...like the table you've recreated.

Can you help anyhow? Thank you so much.
Pedro

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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