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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Sriku
Helper IV
Helper IV

Calculate nested if in power query

Hi,

 

I need to calculate Percentage in power query. If Country = India then (Available - (DowntimeHrs/7.5)- Required)/Required ) and for other country (Available - (DowntimeHrs/6.5)- Required)/Required )

 

IFERROR(IF($A2="India",($B2-($C2/7.5)-$D2)/$D2,($B2-($C2/6.5)-$D2)/$D2),"")

 

CountryAvailableDowntimeHrsRequiredPercentage%
Philippines15.000.0018.00-16.7%
India14.002.2514.00-2.1%
Japan42.000.0044.00-4.5%
India27.000.0031.00-12.9%
India5.000.005.000.0%
India20.000.0020.000.0%
India1.000.001.000.0%
India3.500.005.00-30.0%
India0.500.001.00-50.0%
India1.000.001.000.0%
India0.500.001.00-50.0%
India1.000.001.000.0%
India2.000.002.000.0%
India4.000.004.000.0%
India6.000.006.000.0%
India12.000.0012.000.0%
India14.000.0016.00-12.5%
India5.000.005.000.0%
India5.004.004.401.5%
India3.000.001.50100.0%
India12.0038.576.0014.3%
India9.007.001.00706.7%
Japan7.0012.142.26127.1%
Japan11.000.001.17840.2%
Japan2.000.260.00 
Japan4.000.260.81388.9%
Japan2.001.053.97-53.7%
Japan4.000.000.53654.7%
Japan6.0038.571.52-95.6%
Japan3.000.004.33-30.7%
Japan28.500.000.00 
Japan14.000.001.161106.9%
Japan11.500.003.00283.3%
Japan15.50 3.00416.7%
Japan4.0010.000.00 
Japan148.000.003.004833.3%
Japan2.000.0029.00-93.1%
Japan8.000.002.00300.0%
Japan4.600.002.00130.0%
Japan0.400.0026.00-98.5%
Philippines72.000.0069.004.3%
Philippines107.000.005.901713.6%
Philippines10.000.482.00396.3%
Philippines2.000.00  
Philippines1.000.00  
8 REPLIES 8
AllisonKennedy
Super User
Super User

You can edit the formula bar for the built in conditional column. First add a conditional column like this: 

AllisonKennedy_0-1599995137212.png

 

Then edit the formula bar so it looks like this: 

AllisonKennedy_1-1599995444768.png

 

Otherwise, click the button to create a new CUSTOM COLUMN in the Add Column tab, and paste this formula into the box: 

if [Required]= 0 then null else if [Country] = "India" then Value.Divide([Available] - [DowntimeHrs]/7.5 - [Required],[Required]) else Value.Divide([Available] - [DowntimeHrs]/6.5 - [Required],[Required])

 

@Sriku  UPDATE: Don't forget to set data type to number when you're done too.


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Hi @AllisonKennedy 

thx for pointing to the exception, your formula is 👍.

 

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

FrankAT
Community Champion
Community Champion

Hi @Sriku 

take a look at the following solution:

 

13-09-_2020_13-09-21.png

 

// Table
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZXLasMwEEV/JRi6G4NGb31Cu+reZBFooYYSAv1/qCxZtu/Y7iJ0ZRSdzOPeGXsYuvev8Xt8PMb7509HHTtSKj9VfXCsz549hZfuSkP3ev8Yb9OVrVeatNsce01cwbfb43bPv1gNIW0DLTmMqAOAhltuTQlJLHI9iXgKsM1RdIItn1CG3D5nb3acAq714J7M+r/R0Al9Qln064TyQPkzbTEln+VkTMp+9d49472D4i3ZKouTnqJqRWtWp12YSC6s3eaiDYKpXgSwIahledpOhEUOtnWHfDkGuTwsjeUpf7SKNIKLsCXSjF9wEQUSubQU23aJUDmzKxqlUGbOyCbQsjypZpLGWcl5FC/LrKeAyZFHEO3I4pp5xURAHWErDpuVA0VcFObsRdpJvI02F6GjaeYuoKvgZaUs76yd8/LfxUV81c3RotklFSs7D1ifjJwUjNgGdp3ltT6/59jsOFV3pnFtIVNsS4QfjoCF+rSYeESzwne9o1TKCGzaUMg/NN7GTXvJH8fXQvxmgAh6RF1/AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Country = _t, Available = _t, DowntimeHrs = _t, Required = _t, #"Percentage%" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}, {"Available", type number}, {"DowntimeHrs", type number}, {"Required", type number}, {"Percentage%", Percentage.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Percentage%"}),
    #"Added Custom" = Table.AddColumn(#"Removed Columns", "Percentage", 
each if [Country] = "India" 
then ([Available] - ([DowntimeHrs]/7.5) - [Required])/[Required]  
else ([Available] - ([DowntimeHrs]/6.5) - [Required])/[Required]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Percentage", Percentage.Type}})
in
    #"Changed Type1"

 

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

@FrankAT you just need to account for the divide errors and infinity values still I think?

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Thanks for the solution. Now I am getting error due to infinity, How is overcome this issue.  Please suggest 

 

@Sriku Which solution are you referring to?

This formula checks if the [Required] column is 0 first, and if it is then puts null as value, which should remove the infinity values. If it's not working, please share a screenshot of what you see and which rows have infinity and what formula you're using.

if [Required]= 0 then null else if [Country] = "India" then Value.Divide([Available] - [DowntimeHrs]/7.5 - [Required],[Required]) else Value.Divide([Available] - [DowntimeHrs]/6.5 - [Required],[Required])

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

AllisonKennedy
Super User
Super User

You should be able to do this in Power Query using the built in functionality.

Click the Add Column tab, then choose 'conditional column'.

You can add a row to add more conditions, but your formula only needs one condition - change the type from value to column and choose the [Country] column. Then type India in the box.

Then choose column for the 'then' box. and pick the 'Available' column. Do the same for the 'else' box.

Then turn on your formula bar (click on View tab in Power Query) and manually edit the formula to provide the conditions using the same syntax and pattern already put in place for you.

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

I am a learner to powerbi can you please help me in sharing in screen shot or share the logic applied in Powerbi

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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