Power Query

Oleksandr Iakovenchuk 0 Reputation points
2024-03-29T13:30:40.0933333+00:00

Hi, pls help to rewrite Excel formula for Power Query

I have found that for power query I need to use then and else instead of comma

I got an error "token Eof expected", place of the error I have highlighted in bold "" else

Excel formula: =if(ISERROR((G6-F6)/F6100),"",IF((G6-F6)/F6100>999,"",IF((G6-F6)/F6100<-999,"",IF(AND(G6>0,F6>0),(G6-F6)/F6100,(1)(G6-F6)/F6100))))

Power query: =if(ISERROR((G6-F6)/F6100) then "" else IF((G6-F6)/F6100>999 then "" else IF((G6-F6)/F6100<-999 then "" else IF(AND(G6>0,F6>0),(G6-F6)/F6100,(1)(G6-F6)/F6100))))

Microsoft 365 and Office Excel For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. Andrew Wathana Hosakul 0 Reputation points
    2024-04-12T08:33:10.1766667+00:00

    Hi,

    It looks like the formula is intended to calculate variance/growth between two values with some added logic for errors, negative values and results over 999. If my assumption is correct then I think your formula is missing two things:

    1. an asterisk between F6 and 100
    2. the (1) should be (-1)

    For Excel:

    In Excel, you can use a SWITCH(TRUE()...) pattern instead of nested if statements and wrap that in an IFERROR. This might make it easier for you to read/interpret.

    User's image

    Excel Formula
    =
    IFERROR(
         SWITCH(
              TRUE(),
                   (G6-F6)/F6*100 > 999, "",
                   (G6-F6)/F6*100 < -999, "",
                   AND(G6>0,F6>0), (G6-F6)/F6*100,
                   ((G6-F6)/F6*100)*(-1)),
         "")
    
    

    For Power Query:

    In PQ, write the if/else statements and wrap them in a try/otherwise for the error handling.

    User's image

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUs7TT1TSUTI1UIrViVYywc40RbB1kTlgti6KDJgXCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value1 = _t, Value2 = _t]),
        PreviousStep = Table.TransformColumnTypes(Source,{{"Value1", type number}, {"Value2", type number}}),
        AddCol = Table.AddColumn(PreviousStep, "Result",  each try
          if ([Value2]-[Value1])/[Value1]*100 > 999 then "" else
          if ([Value2]-[Value1])/[Value1]*100 < -999 then "" else
          if ([Value1]>0 and [Value2]>0) then ([Value2]-[Value1])/[Value1]*100 else
          (([Value2]-[Value1])/[Value1]*100)*(-1) otherwise "", type number)
    
    in
    AddCol
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.