Forum Discussion

Sriku's avatar
Sriku
Icon for Helper IV rankHelper IV
6 years ago

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

  • FrankAT's avatar
    FrankAT
    Icon for Community Champion rankCommunity Champion

    Hi Sriku 

    take a look at the following solution:

     

     

    // 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)

      • Sriku's avatar
        Sriku
        Icon for Helper IV rankHelper IV

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

         

  • AllisonKennedy's avatar
    AllisonKennedy
    Icon for Community Champion rankCommunity Champion

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

     

    Then edit the formula bar so it looks like this: 

     

    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.

    • FrankAT's avatar
      FrankAT
      Icon for Community Champion rankCommunity Champion

      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)

  • AllisonKennedy's avatar
    AllisonKennedy
    Icon for Community Champion rankCommunity Champion
    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.
    • Sriku's avatar
      Sriku
      Icon for Helper IV rankHelper IV

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