简体   繁体   中英

Power BI Table Transformation

I am trying to find a way in Power BI to transform a table into a specific format for a custom visualization. The transformation that I'd like to do is the following image: 在此处输入图像描述

Is this possible to do? I've thought of unpivoting or transposing but haven't seemed to figure out how to get past this point.

I do not really understand the use case of this transformation, but you can achieve what you want by copying the source table, remove all columns except 2, and then append all copies.

Let's say we have one table, named Table , like this:

在此处输入图像描述

Make 3 copies of the table, by clicking New Source -> Blank Query :

在此处输入图像描述

and enter =Table as query text, where Table is the name of our source table. If our queries are named Query1 , Query2 and Query3 , then click on each of them and remove the extra column and keep A and B in Query1 , keep B and C in Query2 , and C and D in Query3 . Rename the remaining columns to be named the same way in all 3 queries and click Append Queries or Append Queries as New , then select the 3 copes:

在此处输入图像描述

This will give you the result you ask for:

在此处输入图像描述

Or you could do it in one go, without creating three different queries. Makes your Queries pane a bit nitter

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXICYmcgdlGKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [c1 = _t, c2 = _t, c3 = _t, c4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"c1", type text}, {"c2", type text}, {"c3", type text}, {"c4", type text}}),

    _t1 = Table.SelectColumns(#"Changed Type", {"c1", "c2"}),
    _t1_r = Table.RenameColumns(_t1, {{"c1", "ca"}, {"c2", "cb"}}),

    _t2 = Table.SelectColumns(#"Changed Type", {"c2", "c3"}),
    _t2_r = Table.RenameColumns(_t2, {{"c2", "ca"}, {"c3", "cb"}}),

    _t3 = Table.SelectColumns(#"Changed Type", {"c3", "c4"}),
    _t3_r = Table.RenameColumns(_t3, {{"c3", "ca"}, {"c4", "cb"}}),

    _res = Table.Combine({_t1_r, _t2_r, _t3_r})
in
    _res

One use case I can think of this transformation will be useful is that, when we have a time series data like this, and we want to compare the values of current and last periods.

数字

So here is a version for the case when the number of columns is variable.

let
    Source = ...,
    SingleRow = Table.SingleRow(Source),
    Values = Record.FieldValues(SingleRow),
    Pairs = List.Zip({List.RemoveLastN(Values, 1), List.RemoveFirstN(Values, 1)}),
    Result = Table.FromRows(Pairs, {"Last", "Current"})
in
    Result

I am implying the "rules" for this transformation are then you want a table where the first column and second column form each "pair" of items in the original set. Given the data loaded into Power Query like this:

在此处输入图像描述

I would transpose the data:

在此处输入图像描述

Then make two references to the query. In the first one remove the last row, in the second one remove the first row. Then add an index column to both.

(result of the first reference looks like this)

在此处输入图像描述

Then just do a merge between the two references and you get the two column outputs show in the example.

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