Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

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

  • I created a calculated table for you like this:

    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! 🙂

9 Replies

  • v-yuta-msft's avatar
    v-yuta-msft
    Community Support

    Anonymous ,

     

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

     

     Then add two conditional columns as below:

     

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

     

     

    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's avatar
      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's avatar
    JarroVGIT
    Resident Rockstar

    Hi Anonymous 

     

    I've recreated your table like this:

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

    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

     

    • Anonymous's avatar
      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

  • v-yuta-msft's avatar
    v-yuta-msft
    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:

     

     

    Finally, you will achieve the table.

     

     

    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's avatar
      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

      • JarroVGIT's avatar
        JarroVGIT
        Resident Rockstar

        I created a calculated table for you like this:

        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! 🙂

  • 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.