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
tarocha
Helper II
Helper II

Help with advanced Power Query

Hello

I'm new to Power BI and have some issues with Power Query.

 

I have the following scenario I have a table with 3 columns, cod, cr and percentage.

In the percentage column the sum of the cod always has to give 100%.

 

Example, cod 1.
We have 60% and the other is null, so what I would bring would be 40%, because 100% - 60% - 40%.

Example, cod 3.
We have 60% on the first line, 30% on the second line and on the third line I would like to bring the remaining 10%.

 

I need this to work on Power Query, I tried some solutions, but I couldn't.

Can you help me?

The result I hope is column D of Excel printing.

 

414140iAC2DCC0F13C2FD09.png

 

I share the pbix I'm using in the link below.

a.PNG


Link downalod pbix

 


Thank you

2 ACCEPTED SOLUTIONS
HotChilli
Super User
Super User

It would probably be easier to change the datatype to decimal rather than dealing with percentage type but anyway here's what I did.

Duplicate the table.

'Group By' COD and sum the Percent column. Call the new column Add

You'll get this

Screenshot 2020-12-10 203456.png

You then add a custom column to subtract from 100%.

Use this code, in the formula :

Percentage.From("100%") -[Add]

 Change the type to Percent.

 

You can then Merge this table with the original table (join on COD).

Expand the Table column to return only the Custom column from the 2nd table.

It'll look like this:

Screenshot 2020-12-10 204056.png

 

and from there you can add a column with logic ''If Percent column is null use Table 2 custom else use Percent column"

 

Good luck.

View solution in original post

mahoneypat
Employee
Employee

Here is another way to do it.  It uses a modified Replace Values step to do it in one step. To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

 

let
    Fonte = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYjMDVaVYHQjfCYjzSnNywAJGUAUWUAVG6AqM0UwwhiowRuI7wzXEAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cod = _t, CR = _t, Percent = _t]),
    #"Tipo Alterado" = Table.TransformColumnTypes(Fonte,{{"Cod", Int64.Type}, {"CR", type text}, {"Percent", Percentage.Type}}),
    CustomReplace = Table.ReplaceValue(#"Tipo Alterado",null,each let thiscod = [Cod] in 1 - List.Sum(Table.SelectRows(#"Tipo Alterado", each [Cod] = thiscod)[Percent]),Replacer.ReplaceValue,{"Percent"}),
    #"Changed Type" = Table.TransformColumnTypes(CustomReplace,{{"Percent", Percentage.Type}})
in
    #"Changed Type"

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

4 REPLIES 4
mahoneypat
Employee
Employee

Here is another way to do it.  It uses a modified Replace Values step to do it in one step. To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

 

let
    Fonte = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYjMDVaVYHQjfCYjzSnNywAJGUAUWUAVG6AqM0UwwhiowRuI7wzXEAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cod = _t, CR = _t, Percent = _t]),
    #"Tipo Alterado" = Table.TransformColumnTypes(Fonte,{{"Cod", Int64.Type}, {"CR", type text}, {"Percent", Percentage.Type}}),
    CustomReplace = Table.ReplaceValue(#"Tipo Alterado",null,each let thiscod = [Cod] in 1 - List.Sum(Table.SelectRows(#"Tipo Alterado", each [Cod] = thiscod)[Percent]),Replacer.ReplaceValue,{"Percent"}),
    #"Changed Type" = Table.TransformColumnTypes(CustomReplace,{{"Percent", Percentage.Type}})
in
    #"Changed Type"

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat 
Thank you very much, your solution is sensational.
It solved my problem.

Thank you

HotChilli
Super User
Super User

It would probably be easier to change the datatype to decimal rather than dealing with percentage type but anyway here's what I did.

Duplicate the table.

'Group By' COD and sum the Percent column. Call the new column Add

You'll get this

Screenshot 2020-12-10 203456.png

You then add a custom column to subtract from 100%.

Use this code, in the formula :

Percentage.From("100%") -[Add]

 Change the type to Percent.

 

You can then Merge this table with the original table (join on COD).

Expand the Table column to return only the Custom column from the 2nd table.

It'll look like this:

Screenshot 2020-12-10 204056.png

 

and from there you can add a column with logic ''If Percent column is null use Table 2 custom else use Percent column"

 

Good luck.

@HotChilli  Thank you very much, your solution is sensational.
It solved my problem.

Thank you

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.