Convert rows to columns

Fredrik Söderholm Pettersson 81 Reputation points
2021-01-11T19:30:20.763+00:00

I have an item table (question) and a sales price table (question) in Power query

In the item table all items (rows are unique)
In the price table each item code can have up to 4 different sales prices (buy more save more)

So the price table looks like this

Item code #units Price
1234 0 40
1234 5 35
1234 10 30

  • Not all items have 4 prices, som items have 1 price and some have 3 or maximum 4.
  • The number of units can differ for each price. So price2 can have different #units.

I want to import these sales prices to the item table and I want price1 to be in one column and the #untis for Price1 in the next column, then Price2 and the #units for price 2 so it looks like this.

Item code Price1 units1 Price2 units2 Price3 units3
1234 40 0 35 5 30 10

How do I do this? Anyone who knows?

Best regards Fredrik

Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. Lz._ 9,016 Reputation points
    2021-01-12T07:18:44.057+00:00

    Hi @Fredrik Söderholm Pettersson

    (When you have a minute could you provide feedback/mark as answer your previous request please - Thanks)

    Assuming TableItem and TablePrice loaded to PQ:

    55682-demo.png

    The following code for TableItem should do what you want

    let  
        Source = Excel.CurrentWorkbook(){[Name="TableItem"]}[Content],  
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", Int64.Type}}),  
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Item"}, TablePrice, {"Item"}, "TablePrice", JoinKind.LeftOuter),  
        #"Removed Columns" = Table.RemoveColumns(#"Merged Queries",{"Item"}),  
        #"Tranformed TablePrice" = Table.TransformColumns(#"Removed Columns",  
            {"TablePrice", each  
                let  
                    Idx = Table.AddIndexColumn(_, "Index", 1, 1),  
                    Unpivot = Table.UnpivotOtherColumns(Idx, {"Index", "Item"}, "Attribute", "Value"),  
                    MergeColumns = Table.CombineColumns(  
                        Table.TransformColumnTypes(Unpivot, {{"Index", type text}}),  
                        {"Attribute", "Index"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"ColName"  
                    )  
                in  
                    Table.Pivot(MergeColumns, List.Distinct(MergeColumns[ColName]), "ColName", "Value"),  
                type table  
            }  
        ),  
        CombinedNestedTables = Table.Combine(#"Tranformed TablePrice"[TablePrice])  
    in  
        CombinedNestedTables  
    

    Corresponding sample available here. Any question let me know

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Fredrik Söderholm Pettersson 81 Reputation points
    2021-01-11T19:32:33.467+00:00

    And now I see that the formatting didn't show so it can be a bit tricky to see what I mean. Hopefully someone will understand anyway.
    /Fredrik

    0 comments No comments

  2. Fredrik Söderholm Pettersson 81 Reputation points
    2021-02-18T07:36:37.033+00:00

    This was the solution I needed. Thank you!!!

    Sorry for my late reply.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.