SOLVED

Power Query Custom Function with IF statement

%3CLINGO-SUB%20id%3D%22lingo-sub-2242571%22%20slang%3D%22en-US%22%3EPower%20Query%20Custom%20Function%20with%20IF%20statement%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2242571%22%20slang%3D%22en-US%22%3E%3CP%3EI%20made%20the%20custom%20function%20below%20in%20Power%20query%2C%20but%20results%20are%20not%20what%20I%20expect.%20If%20I%20put%20in%200.1%20I%20get%2050%20instead%20of%200%2C%20for%20instance.%20Must%20be%20some%20stupid%20mistake%20or%20misunderstanding%20on%20my%20part%2C%20can%20anyone%20tell%20me%20what's%20wrong%3F%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3E(vGrowth%20as%20number)%20%3D%26gt%3B%20%0Aif%20vGrowth%20%26gt%3B%201%20then%20100%20%0Aelse%20if%20vGrowth%20%26gt%3B%200.5%20then%2050%20%0Aelse%20if%20vGrowth%20%26gt%3B%200.2%20then%2020%20%0Aelse%20if%20vGrowth%20%26gt%3B%20-0.2%20then%200%20%0Aelse%20if%20vGrowth%20%26gt%3B%20-0.5%20then%20-20%20%0Aelse%20if%20vGrowth%20%26gt%3B%20-1%20then%20-50%20%0Aelse%20-100%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2242571%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPower%20BI%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Frequent Contributor

I made the custom function below in Power query, but results are not what I expect. If I put in 0.1 I get 50 instead of 0, for instance. Must be some stupid mistake or misunderstanding on my part, can anyone tell me what's wrong?

(vGrowth as number) => 
if vGrowth > 1 then 100 
else if vGrowth > 0.5 then 50 
else if vGrowth > 0.2 then 20 
else if vGrowth > -0.2 then 0 
else if vGrowth > -0.5 then -20 
else if vGrowth > -1 then -50 
else -100

 

2 Replies
best response confirmed by bartvana (Frequent Contributor)
Solution

@bartvana 

It works correctly

image.png

function is

(vGrowth as number) =>
let result = 
    if vGrowth > 1 then 100 
    else if vGrowth > 0.5 then 50 
    else if vGrowth > 0.2 then 20 
    else if vGrowth > -0.2 then 0 
    else if vGrowth > -0.5 then -20 
    else if vGrowth > -1 then -50 
    else -100
in result
Yes, it does for me now too, very odd. Should've taken a screenshot. When I invoked the function, I got strange results. Thanks for looking into it...