Unpivot Columns and preserving original data type.

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

I have to unpivot 10 columns and only of them the data type is percentage, the other are numbers. However after doing it, that the final column (Value) will have only one data type. Is there a way to preserve the original format (%) or should I not include the percentage column in the unpivot process?

1697689985870.png
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I am not sure if there is a magic or better way doing this but since I don't know if any, I would probably change the percentage column to text and add a percentage sign before the unpivot action. Something like the following. The FormatAsText is the step that makes it work.

Power Query:
let 
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY45DsMwDAS/Eqg2At5HnWcELnI36fx/IJSFGCpYkKvRzvXaLt/btp2wLQ3OFOpmwqG13mrGWYxSQxC8rcufoD1KEMxIyKz1XjPO9RaRnXwmeI9CIhhV0Wt9HISHOBFQ8ETI6LDwRArqHc/DCowFNKtEJ0RH5kwGVs695XW0lBKYEMSM2B6hWjCAeG951wxd6mJsqjkRPryKMDKm7vOpGboqmVo/QbR1/QE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Class = _t, Percentage = _t, Text = _t, Integer = _t, Decimal = _t]),
    ChangeTypes = Table.TransformColumnTypes(Source,{{"Percentage", Percentage.Type}, {"Integer", Int64.Type}, {"Decimal", type number}}),
    FormatAsText = Table.TransformColumns(ChangeTypes, 
        {
            "Percentage", each Text.From(Number.Round(_ * 100, 2) ) & "%"}
        ),
    UnpivotColumns = Table.Unpivot(FormatAsText, {"Percentage", "Text", "Integer", "Decimal"}, "Attribute", "Value")
in
    UnpivotColumns

1697693547930.png


When I load it to the worksheet, it shows correctly if I would use it as report and actually can even use the percentage value as numeric.
1697693696610.png
 
Upvote 1
Solution

Forum statistics

Threads
1,223,935
Messages
6,175,496
Members
452,649
Latest member
mr_bhavesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top