Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This collection of Excel lambda functions will quickly convert currency values stored as text with a scaling suffix to a numerical representation of the same number in a standard scale
/*
CLEANCURRENCYTEXT
This lambda function will quickly convert currency values stored as text with a scaling suffix such as "B" (for billions) or "M" (for millions) and so on, to a number all in the same scale
inputs:
- val, a single value to be converted as described above
- [mapping], a two-column array or range of suffix:power pairs such as {"b",9} (see _defaultarray for more examples)
returns:
a single value where, for example, $180B is converted to 180000000000
For more details:
*/
CLEANCURRENCYTEXT =LAMBDA(val,[mapping],
LET(
_curr,LOWER(LEFT(val,1)),
_nocurr,SUBSTITUTE(val,_curr,""),
_nonnumeric,GETNONNUMBERS(_nocurr,FALSE),
_filtered,FILTER(_nonnumeric,(_nonnumeric<>".")*(_nonnumeric<>",")),
_joined,TEXTJOIN("",TRUE,_filtered),
_suffix,IFERROR(_joined,"nope"),
_defaultmapping,{
"b",9;
"bn",9;
"bns",9;
"m",6;
"mm",6;
"mn",6;
"k",3;
"nope",0
},
_mapping,IF(ISOMITTED(mapping),_defaultmapping,mapping),
_multiplier,POWER(
10,
XLOOKUP(
_suffix,
INDEX(_mapping,,1),
INDEX(_mapping,,2),
0
)
),
_nosuffix,SUBSTITUTE(_nocurr,_suffix,""),
_output,_nosuffix*_multiplier,
_output
)
);
/*
GETNONNUMBERS
inputs:
- rng, a cell or array containing text for which you want to return non-numeric characters
- vertical, TRUE if you want the return to be in a vertical array, FALSE for horizontal
returns:
an array of non-numeric characters
*/
GETNONNUMBERS =LAMBDA(rng,vertical,
LET(
c,CHARACTERS(rng,vertical),
nums,INT(c),
FILTER(c,ISERR(nums))
)
);
/*
CHARACTERS
inputs:
- rng, a cell or array containing text for which you want to return characters
- vertical, TRUE if you want the return to be in a vertical array, FALSE for horizontal
returns:
an array of characters
*/
CHARACTERS =LAMBDA(rng,vertical,
LET(
chars,MID(rng,SEQUENCE(LEN(rng)),1),
IF(vertical,chars,TRANSPOSE(chars))
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment