cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
mdemos02 Frequent Visitor
Frequent Visitor

Rows to Columns

Hello guys,

 

How can I convert the rows of a particular transaction to specific columns in Power Query?

 

See below:

 

My data

 

Transaction DateTransaction detailsCreditDebit
02/10/20191157983.79 
 2  
 3  
08/10/20194226529.07 
 5  
 6  
23/10/2019162306805.07 
 17  
 18  
24/10/201919 2000000
 20  
 21  
24/10/201922 580
 23  
29/10/201925155361.21 
 26  
 27  
31/10/201928 35
 29  
01/11/2019302317497.1 
 31  
 32  

 

The expected outcome:

Transaction DateTransaction details 1Transaction details 2Transaction details 3TypeAmount
02/10/2019123Credit157983.8
08/10/2019456Credit226529.1
23/10/2019161718Credit2306805
24/10/2019192021Debit2000000
24/10/20192223 Debit580
29/10/2019252627Credit155361.2
31/10/20192829 Debit35
01/11/2019303132Credit2317497

 

Is it possible?

 

Thank you

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mcybulski Member
Member

Re: Rows to Columns

The query below should work for you.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dVFbDsUQFNyL70bPw8FZi3T/22hpq+MmVyJMzMPQWpCdaRdiD1vgPq141Vg6DsfWwrVK339QAVaQp06VbOKRCvBtlWeAohjfT0QpV7LVgctqwRU9Enr4fSI0xmxAq4HwPwN5ylr9xFhYHMk2Xsw0c7wtX0n+ycMCymjxVFGbXAfuReWXqjQuwyV5iZimvKbp/LDjBA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Transaction Date" = _t, #"Transaction details" = _t, Credit = _t, Debit = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Transaction Date", "Transaction details", "Credit", "Debit"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Credit", type number}, {"Debit", type number}, {"Transaction details", Int64.Type}}),
    AddSum = Table.AddColumn(#"Changed Type", "Sum", each List.Sum({[Debit],-[Credit]})),
    AddTranStart = Table.AddColumn(AddSum, "TransStart", each if [Transaction Date] <> null then [Transaction details] else null),
    #"Filled Down" = Table.FillDown(AddTranStart,{"Transaction Date", "TransStart"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Transaction Date", "TransStart"}, {{"Sum", each List.Sum([Sum]), type number}, 
{"TranDetails", each [Transaction details], type list}, {"Count", Table.RowCount, type number}}),
    BreakList = List.Accumulate(List.Buffer({1..3}) ,#"Grouped Rows",  (s,c) => Table.AddColumn(s, "Transaction details " & Text.From(c),each try [TranDetails]{c-1} otherwise null)),
    AddType = Table.AddColumn(BreakList, "Type", each if [Sum] < 0 then "Credit" else "Debit"),
    AddAmount = Table.AddColumn(AddType, "Amount", each Number.Abs([Sum])),
    #"Removed Columns" = Table.RemoveColumns(AddAmount,{"TransStart", "Sum", "TranDetails", "Count"})
in
    #"Removed Columns"

View solution in original post

1 REPLY 1
mcybulski Member
Member

Re: Rows to Columns

The query below should work for you.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dVFbDsUQFNyL70bPw8FZi3T/22hpq+MmVyJMzMPQWpCdaRdiD1vgPq141Vg6DsfWwrVK339QAVaQp06VbOKRCvBtlWeAohjfT0QpV7LVgctqwRU9Enr4fSI0xmxAq4HwPwN5ylr9xFhYHMk2Xsw0c7wtX0n+ycMCymjxVFGbXAfuReWXqjQuwyV5iZimvKbp/LDjBA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Transaction Date" = _t, #"Transaction details" = _t, Credit = _t, Debit = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Transaction Date", "Transaction details", "Credit", "Debit"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Credit", type number}, {"Debit", type number}, {"Transaction details", Int64.Type}}),
    AddSum = Table.AddColumn(#"Changed Type", "Sum", each List.Sum({[Debit],-[Credit]})),
    AddTranStart = Table.AddColumn(AddSum, "TransStart", each if [Transaction Date] <> null then [Transaction details] else null),
    #"Filled Down" = Table.FillDown(AddTranStart,{"Transaction Date", "TransStart"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Transaction Date", "TransStart"}, {{"Sum", each List.Sum([Sum]), type number}, 
{"TranDetails", each [Transaction details], type list}, {"Count", Table.RowCount, type number}}),
    BreakList = List.Accumulate(List.Buffer({1..3}) ,#"Grouped Rows",  (s,c) => Table.AddColumn(s, "Transaction details " & Text.From(c),each try [TranDetails]{c-1} otherwise null)),
    AddType = Table.AddColumn(BreakList, "Type", each if [Sum] < 0 then "Credit" else "Debit"),
    AddAmount = Table.AddColumn(AddType, "Amount", each Number.Abs([Sum])),
    #"Removed Columns" = Table.RemoveColumns(AddAmount,{"TransStart", "Sum", "TranDetails", "Count"})
in
    #"Removed Columns"

View solution in original post

Helpful resources

Announcements
New Kudos Received Badges Coming

New Kudos Received Badges Coming

Kudos to you if you earned one of these! Check your inbox for a notification.

Microsoft Implementation for Communities Wins Award

Microsoft Implementation for Communities Wins Award

Learn about the award-winning innovation that was implemented across Microsoft’s Business Applications Communities.

Power Platform World Tour

Power Platform World Tour

Find out where you can attend!

Top Kudoed Authors (Last 30 Days)
Users online (1,282)