1

I have number field (Price) that I would like to:

  1. Label numbers between 1000 - 999,999 as $1k - $999k
  2. Label numbers >=1,000,000 as $1M
  3. Numbers <1000 remain as $1 - $999

so far, I got an iif statement to work with this:

IIf($feature.price>=1000,"$" + $feature.price /1000, 0)+"k" 

that labels everything above 1000 as $1k

How can I get all three steps listed above completed?

3 Answers 3

2

I think you should be able to use nested iif statements to label your data the way you want. Something along these lines should work:

iif($feature.price < 1000, "$" + $feature.price, iif($feature.price < 1000000, "$" + $feature.price/1000 + "K", iif($feature.price < 1000000000, "$" + $feature.price/1000000, "")))

With this statement, prices below 1000 would remain unaltered. Prices above 1000 would then be checked to determine if they are less than 1000000. If so, labels follow the $1K - $999K format. If not, we check the next level up, and if necessary we can repeat this process to format values in the billions, trillions, etc (by inserting more iif statement(s) in place of the final argument).

UPDATE:

I modified the line of code slightly to make the pattern of nested iif statements a bit more apparent. As such, I deleted your edit describing the parse error, since the code each of us has is (almost) identical.

In reference to what you said you are wanting in terms of rounding the results, this can be accomplished via the "round" function. This function takes two arguments, and it rounds the number in the first argument to the number of decimal places specified by the second argument. It seems you are wanting to round your numbers to one decimal place, and the following statement should accomplish this:

iif($feature.price < 1000, "$" + round($feature.price, 1), iif($feature.price < 1000000, "$" + round($feature.price/1000, 1) + "K", iif($feature.price < 1000000000, "$" + round($feature.price/1000000, 1), "")))

Note that this is exactly the same structure as the original set of nested iif statements.

2
  • Would it be possible for you to provide the whole line of code you used? If you copied the line of code I gave you, the last two arguments are "..." to indicate that you would continue the pattern set in the earlier nested iif statements. I could edit it for clarity if that would help.
    – Benjamin
    Commented Oct 6, 2019 at 20:39
  • My apologies for the late reply. I tried the above suggested expression and it worked flawlessly! Well explained... This functions exactly as I planned. Thanks much. Commented Oct 10, 2019 at 19:03
1

I have a couple of suggestions. First, I think the expression can be simplified to the following:

function formatValue(value){
  return When (
    value >= 1000000, Text((value / 1000000), "$##.#m"),
    value >= 100000, Text((value / 1000), "$##.#k"),
    value >= 1000, Text((value / 1000), "$#.#k"),
    Text(value, "$#.#0")
  );
}

formatValue($feature.price);

That should format the value properly without having to write the nested iif() statements.

The second thing I wanted to point out is instead of using iif ( exp, iif(), val), the expression will be more readable if you use When():

var price = $feature.price;;

When( 
    price < 1000, "$" + round(price, 1), 
    price < 1000000, "$" + round(price/1000, 1) + "K",
    price < 1000000000, "$" + round(price/1000000, 1),
"")

I would go with the first expression, but in the future when you are checking multiple expressions, I would stick with When().

I hope that helps.

1
  • Awesome! I test both expressions and they both returned the desired results! thanks Commented Oct 10, 2019 at 19:11
0

Benjamin,

I was able to make one slight modification to your expression, see below:

iif($feature.price < 1000, "$" + $feature.price, iif($feature.price < 1000000, "$" + $feature.price/1000 + "K", iif($feature.price < 1000000000,"$" + $feature.price/1000000 + "M",0)))

This works; however, I noticed it does not shorten the number (ex. 1427 shows up as $1.427k - see attached). How can I modify the expression to have my output show like this: *1427 to $1.4K

Thanks in advance

[![With this statement, prices below 1000 would remain unaltered. Prices above 1000 would then be checked to determine if they are less than 1000000. If so, labels follow the $1K - $999K format. If not, we check the next level up, and if necessary we can repeat this process to format values in the billions, trillions, etc.

** I have tried this, but no luck. I was given a parse error (See attached)1]1

1
  • It might be good to modify your original question to describe the rounding you are wanting to do -- it would make things a bit clearer for anyone coming to this question in future
    – Benjamin
    Commented Oct 8, 2019 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.