Skip to content

Instantly share code, notes, and snippets.

@ImkeF

ImkeF/Xls.PV.pq Secret

Last active January 1, 2023 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ImkeF/9583ba901437fc46c7ba205fe7539597 to your computer and use it in GitHub Desktop.
Save ImkeF/9583ba901437fc46c7ba205fe7539597 to your computer and use it in GitHub Desktop.
Returns the present value of a loan or an investment, based on a constant interest rate. Optional future value and type of payment can be used.
let func =
(Rate as number, Nper as number, Pmt as number, optional Fv_ as number, optional Type_ as number) =>
let
// Source for calculation logic: https://community.powerbi.com/t5/Community-Blog/P-Q-Excel-to-DAX-Translation/ba-p/1061107
Type = if Type_ is null then 0 else Type_,
Fv = if Fv_ = null then 0 else Fv_,
Custom3 =
if Rate = 0
then -1 * ( (Pmt * Nper) + Fv)
else ( Pmt * ( 1 + Rate * Type) *
(( Number.Power(1 + Rate, Nper) -1 )/ Rate ) + Fv )
/ Number.Power(1 + Rate , Nper) * -1
in
Custom3 ,
documentation = [
Documentation.Name = " Xls.PV ",
Documentation.Description = " Returns the present value of a loan or an investment, based on a constant interest rate. ",
Documentation.LongDescription = " Returns the present value of a loan or an investment, based on a constant interest rate. Optional future value and type of payment can be used. ",
Documentation.Category = " Xls.Financial ",
Documentation.Source = " www.TheBIcountant.com . ",
Documentation.Version = " 1.0 ",
Documentation.Author = " Imke Feldmann ",
Documentation.Examples = {[Description = " ",
Code = " let
Rate = 0.08,
Nper = 20 ,
Pmt = 500,
Fv_ = 10000,
Type_ = 1,
FunctionCall = fnPV(Rate, Nper, Pmt, Fv_, Type_)
in
FunctionCall ",
Result = " -7447,28167406318
"]}]
in
Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment