简体   繁体   中英

Power BI: Split columns into rows

I have something similar to this:

在此处输入图像描述

what I want is to get a structure similar to this one:

Item   | Period | Qty
----------------------
Item A |   1    |  2
Item A |   2    |  2
Item B |   1    |  1
Item B |   2    |  1
Item C |   1    |  1
Item C |   2    |  1
Item D |   1    |  2
Item D |   2    |  2

So basically I want to split the values in columns into rows, but not merely transposing (at least in my understanding). How can I achieve that?

Using UNPIVOT :

在此处输入图像描述

在此处输入图像描述

And extract LastCharacter:

在此处输入图像描述

Reordering and renaming:

在此处输入图像描述

Full M query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixJzXVU0lEyAuNYHYiIE5BnCMaxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, ABT_Period1 = _t, ABT_Period2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"ABT_Period1", Int64.Type}, {"ABT_Period2", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name"}, "Attribute", "Value"),
    #"Inserted Last Characters" = Table.AddColumn(#"Unpivoted Columns", "Last Characters", each Text.End([Attribute], 1), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Last Characters",{"Attribute"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Name", "Last Characters", "Value"}),
    #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Last Characters", "Period"}, {"Value", "Qty"}})
in
    #"Renamed Columns"

EDIT:

As @Alexis Olson proposed in comment the easiest way to get Period number is usage of: Text.AfterDelimiter([Attribute],"Period")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM