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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
tonyclifton
Helper III
Helper III

Table transformation - Separate columns

Hello community,

 

I'm failing in trying to turn this table into below desired output:

a.PNG

 

Input (please copy, in case you want to test yourself):

ColumnNames12
2017nullnull
xyz1112
DEABnullnull
abc1314
2017nullnull
xyz2223
ESYZnullnull
abc3334
2018nullnull
xyz4445
DEABnullnull
abc5556
2018nullnull
xyz6667
ESYZnullnull
abc7778

 

Output:

 

b.PNG


Explanation: 

  • Column "1" and "2" of Input represent the month in the output table (for simplicity I didn't enter all 12 month columns).
  • I should then be able to use fill function on those 12 newly created rows 
  • I need to find a way to move the product and year values to separate columns
    • it would be sufficent to look for text.length = 4
    • for example I tried with: try Number.FromText([ColumnNames]) otherwise null
      • this returns only year and product values but I couldn't find a way to get both into an own column

 

I hope you can help me.

If something is unclear just let me know.

 

Thank you.

1 ACCEPTED SOLUTION

I managed to get the source data transformed via SQL.

View solution in original post

3 REPLIES 3
v-danhe-msft
Employee
Employee

Hi @tonyclifton,

Based on my test, you could refer to below steps:
First copy your row table for two times:

3.PNG

Filter the column equals to 'abc' and 'xyz' then unpivot [1] and [2], sort it:

1.PNG

2.PNG

Copy the filtered table again and filter 'abc', filter the row table 'xyz', merge them as new and add an index column:

4.PNG

In table 2, you could refere below code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7PKc3N80vMTS1W0lEyBGIjpVidaCUjA0NzICevNCcHRoGEKyqrQMpA6gwhCl1cHZ2wKExMSgapMQYRJoRNNDICEcZgEdfgyCicJhqDTDSGm2iB00QTExBhStiNpqYgwoywiWZmIMKcsBvNQf40t1CKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", Int64.Type}, {"(blank).2", Int64.Type}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Filtered Rows" = Table.SelectRows(#"Promoted Headers", each ([ColumnNames] = "2017" or [ColumnNames] = "2018")),
    #"Replaced Value" = Table.ReplaceValue(#"Filtered Rows",null,1000,Replacer.ReplaceValue,{"1"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",null,2000,Replacer.ReplaceValue,{"2"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Replaced Value1", {"ColumnNames"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Value"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1)
in
    #"Added Index"

A.PNG

In table 3, you could refer to below code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7PKc3N80vMTS1W0lEyBGIjpVidaCUjA0NzICevNCcHRoGEKyqrQMpA6gwhCl1cHZ2wKExMSgapMQYRJoRNNDICEcZgEdfgyCicJhqDTDSGm2iB00QTExBhStiNpqYgwoywiWZmIMKcsBvNQf40t1CKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", Int64.Type}, {"(blank).2", Int64.Type}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Filtered Rows" = Table.SelectRows(#"Promoted Headers", each ([ColumnNames] = "DEAB" or [ColumnNames] = "ESYZ")),
    #"Replaced Value" = Table.ReplaceValue(#"Filtered Rows",null,1000,Replacer.ReplaceValue,{"1"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",null,2000,Replacer.ReplaceValue,{"2"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Replaced Value1", {"ColumnNames"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Value"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1)
in
    #"Added Index"

B.PNG

Merge them all and delete extra columns,you could see the result:

1.PNG

You could download the pbix file to have a view

 

Regards,

Daniel He

 

Community Support Team _ Daniel He
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-danhe-msftThanks for the detailed explanation. But it looks like it's to static for my source data with many ColumnNames.

 

I was able to bring in the source data in the following format:

 

I1.PNG

 

ProductYearColumnNames12
DEAB2017xyz1112
DEAB2017abc1314
ESYZ2017xyz2223
ESYZ2017abc3334
DEAB2018xyz4445
DEAB2018abc5556
ESYZ2018xyz6667
ESYZ2018abc7778

 

How can I now get the distinct values in "ColumnNames" transposed?

I managed to get the source data transformed via SQL.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.