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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Abasalm
New Member

how to formatting a column in power bi have number and percentage

how to formatting a column in power bi have number and percentage in the same column

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @Abasalm 

 

and use this for a little more professional formatting of the text 🙂

For other type of formatting (I integrated only % and $) you have to expand the if statement

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUlEoSS0uUdJRMjQAA6VYnWglVZiggY6RqVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [KPI = _t, Q1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"KPI", type text}, {"Q1", Percentage.Type}}),
    FormatQ1 = Table.TransformRows
    (
        #"Changed Type",
        (rec)=>
        if Text.Start(rec[KPI],1)="$" then   Record.TransformFields(rec,{"Q1",each Number.ToText(_, "n") & " $" }) else if Text.Start(rec[KPI],1)="%" then   Record.TransformFields(rec,{"Q1", each Number.ToText(_, "P1")}) else rec
    ), 

    RecordsTobTable = Table.FromRecords(FormatQ1)
in
    RecordsTobTable

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

6 REPLIES 6
Jimmy801
Community Champion
Community Champion

Hello @Abasalm 

 

can you make an example? WIth percentage you mean the symbol and you want to delete it and use only the number part?

If this so you can use this example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlRVio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [NumberPercentage = _t]),
    ChangePercentage = Table.TransformColumnTypes(Source,{{"NumberPercentage", Percentage.Type}})
in
    ChangePercentage

 

if you have two values in one cell, then you have make an exact data example with the expected result

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

@Jimmy801 

 

thank you for your reply,

 

this an example for it

I have in one column different categories, how I do formating for it

 

 

Abasalm_1-1599561556876.png

 

Jimmy801
Community Champion
Community Champion

Hello @Abasalm 

 

just to be clear... you want to format the column Q1 according to the symbol that is stated as first character in your KPI column?

 

Jimmy

@Jimmy801 

 

Yes please 

Jimmy801
Community Champion
Community Champion

Hello @Abasalm 

 

and use this for a little more professional formatting of the text 🙂

For other type of formatting (I integrated only % and $) you have to expand the if statement

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUlEoSS0uUdJRMjQAA6VYnWglVZiggY6RqVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [KPI = _t, Q1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"KPI", type text}, {"Q1", Percentage.Type}}),
    FormatQ1 = Table.TransformRows
    (
        #"Changed Type",
        (rec)=>
        if Text.Start(rec[KPI],1)="$" then   Record.TransformFields(rec,{"Q1",each Number.ToText(_, "n") & " $" }) else if Text.Start(rec[KPI],1)="%" then   Record.TransformFields(rec,{"Q1", each Number.ToText(_, "P1")}) else rec
    ), 

    RecordsTobTable = Table.FromRecords(FormatQ1)
in
    RecordsTobTable

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Jimmy801
Community Champion
Community Champion

Hello @Abasalm 

 

try this.It uses Table.TransformRows to check the first character of your KPI column and then it adds it as text to the Q1 column.

You could work also with a Table.AddColumn to do the same. This is only for visual, as the cell type is text.

Here the complete code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUlEoSS0uUdJRMjQAA6VYnWglVZiggY6RqVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [KPI = _t, Q1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"KPI", type text}, {"Q1", Percentage.Type}}),
    FormatQ1 = Table.TransformRows
    (
        #"Changed Type",
        (rec)=>
        if Text.Start(rec[KPI],1)="$" then   Record.TransformFields(rec,{"Q1",each Text.From(_) & " $"}) else if Text.Start(rec[KPI],1)="%" then   Record.TransformFields(rec,{"Q1",each Text.From(Number.From(_)*100) & " %"}) else rec
    ), 

    RecordsTobTable = Table.FromRecords(FormatQ1)
in
    RecordsTobTable

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors
Top Kudoed Authors