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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Power BI : Unpivot Columns

Hi, I have my base table like below. 

Input.PNG

 

My Expected Output is like below  :

Expected_Output.PNG

 

What I have done so far is that in Direct Query, I have unpivoted the columns which come under 'Component' and unpivoted the columns which come under 'Reverse Component' separately. But naturally it gives me below output.

 

Actual_Output.PNG

 

Every component gets repeated for each reverse component. Can anyone please help me to get the expected output.

 

 

2 ACCEPTED SOLUTIONS
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

 

You can add a Custom Column like below ( added column step ), but I don't think it will work in Direct Query Mode.

What data source are you connecting to as most of the time Unpivot would not work in Direct Query Mode?

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1AciIwMjAyDH0MAASsFJUygJUqoUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Loan ID" = _t, #"Due Date" = _t, #"Principal Amt" = _t, #"NI Amount" = _t, #"AI Amount" = _t, #"Principal Reverse Amt" = _t, #"NI Reverse Amt" = _t, #"AI Reverse Amt" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Loan ID", Int64.Type}, {"Due Date", type date}, {"Principal Amt", Int64.Type}, {"NI Amount", Int64.Type}, {"AI Amount", Int64.Type}, {"Principal Reverse Amt", Int64.Type}, {"NI Reverse Amt", Int64.Type}, {"AI Reverse Amt", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 
        #table( 
            type table [#"Component"=text, #"Amount"=number, #"Reverse Component"=text, #"Reverse Amount"=number],
            {
                { "Principal Amt", [Principal Amt], "Principal Reverse Amt", [Principal Reverse Amt] },
                { "NI Amount", [NI Amount], "NI Reverse Amt", [NI Reverse Amt] },
                { "AI Amount", [AI Amount], "AI Reverse Amt", [AI Reverse Amt] }
            }
        ), type table
    )
in
    #"Added Custom"

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

View solution in original post

Hi @Anonymous ,

 

I provide a troublesome solution, hope that a better solution:

 

1.I create a copy of the original query . For  the original query, I tried to unpivot colums for 'Principal Amt','NI Amount' and 'Al Amount' columns and deleted the reverse columns:

Untitled picture.png

 

 For duplicated query, I unpivot the reverse columns and remove other columns:

Untitled picture1.png

 

  1. Add index column for both of them for merging query:

 

Untitled picture2.png

 

3.Merge query by index column:

Untitled picture3.pngUntitled picture4.png

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

 

Best Regards,

Dedmon Dai

View solution in original post

6 REPLIES 6
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

 

You can add a Custom Column like below ( added column step ), but I don't think it will work in Direct Query Mode.

What data source are you connecting to as most of the time Unpivot would not work in Direct Query Mode?

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1AciIwMjAyDH0MAASsFJUygJUqoUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Loan ID" = _t, #"Due Date" = _t, #"Principal Amt" = _t, #"NI Amount" = _t, #"AI Amount" = _t, #"Principal Reverse Amt" = _t, #"NI Reverse Amt" = _t, #"AI Reverse Amt" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Loan ID", Int64.Type}, {"Due Date", type date}, {"Principal Amt", Int64.Type}, {"NI Amount", Int64.Type}, {"AI Amount", Int64.Type}, {"Principal Reverse Amt", Int64.Type}, {"NI Reverse Amt", Int64.Type}, {"AI Reverse Amt", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 
        #table( 
            type table [#"Component"=text, #"Amount"=number, #"Reverse Component"=text, #"Reverse Amount"=number],
            {
                { "Principal Amt", [Principal Amt], "Principal Reverse Amt", [Principal Reverse Amt] },
                { "NI Amount", [NI Amount], "NI Reverse Amt", [NI Reverse Amt] },
                { "AI Amount", [AI Amount], "AI Reverse Amt", [AI Reverse Amt] }
            }
        ), type table
    )
in
    #"Added Custom"

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Anonymous
Not applicable

Hi @Mariusz I am connecting to postgresql. Not able to open your PBIX file. It is showing "Object reference not set to an instance of object". Is it because I haven't updated my desktop to the latest release of Power BI ? 

Hi @Anonymous ,

 

I provide a troublesome solution, hope that a better solution:

 

1.I create a copy of the original query . For  the original query, I tried to unpivot colums for 'Principal Amt','NI Amount' and 'Al Amount' columns and deleted the reverse columns:

Untitled picture.png

 

 For duplicated query, I unpivot the reverse columns and remove other columns:

Untitled picture1.png

 

  1. Add index column for both of them for merging query:

 

Untitled picture2.png

 

3.Merge query by index column:

Untitled picture3.pngUntitled picture4.png

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

 

Best Regards,

Dedmon Dai

Anonymous
Not applicable

Thank you @v-deddai1-msft ...but will your solution work if I don't have same number of component and reverse component? In my actual case, there are 10 types of components and 9 types of reverse components.

Hi @Anonymous 

 

Yes, the version is probably an issue, but you can just paste the code provided into Blank Query.

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn


 

Anonymous
Not applicable

@Greg_Deckler @amitchandak Please help

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.