Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
PowerMark123
New Member

Double Nested Loop

Hi, I'm completely new to M Language so on a steep learnign curve here, I could use a helping hand!!

 

I'm trying to calculate the repayment schedules of a number of loans. Using List.Generate, I've got the repayment schedule working for a loan in SampleData, but I now need to move to the next loan in my SampleData table. So, I'm attempting a nested loop, the inner calculates the repayment schedule and the outer loop moves to the next loan. Here's what I have so far, anyone got any ideas to point me in teh right direction? I suspect it's my absolute lack of knowledge about what is fed into the outer loop that's holding me back!

 

let

 

Source = SampleData,

 

OuterLoop = (Source, RepaymentSchedule) =>

 

List.Generate

(

    ()=>[Index = 0],

    each [Index] < Table.RowCount(Source),

    each [Index = [Index]+1],

    each RepaymentSchedule([Index])

),

 

ContractualInstalments = (Index) =>

 

List.Generate

(

 

()=>

[Payment Number = 1,

APR = Source[AmortisingRate]{Index},

Term = Source[Term]{Index},

RemainingTerm = Source[RemainingTerm]{Index},

LoanAmount = Source[OpeningBalance]{Index},

Division = Source[Division.1]{Index},

Rate = Number.Power( (1+APR), (1/12) ) - 1,

Payment = Number.Round( (LoanAmount * Rate) / ( (1 - Number.Power( (1 + Rate), -(Term - 1)) )), 2),

Funder = Division,

#"Opening Balance" = LoanAmount,

#"Monthly Payment" = Payment,

#"Monthly Interest" = Number.Round(#"Opening Balance" *Rate, 2),

#"Monthly Principal" = Payment - #"Monthly Interest",

#"Balance Remaining" = #"Opening Balance"- #"Monthly Principal"],

 

 

each [Payment Number] < [Term],

 

 

each [Payment Number = [#"Payment Number"] +1,

APR =  Source[AmortisingRate]{Index},

Term =  Source[Term]{Index},

RemainingTerm =  Source[RemainingTerm]{Index},

LoanAmount =  Source[OpeningBalance]{Index},

Division = Source[Division.1]{Index},

Rate = Number.Power( (1+APR), (1/12) ) - 1,

Payment = Number.Round( (LoanAmount * Rate) / ( (1 - Number.Power( (1 + Rate), -(Term - 1)) )), 2),

Funder = Division,

#"Opening Balance" = [#"Balance Remaining"],

#"Monthly Payment" = Payment,

#"Monthly Interest" = Number.Round(#"Opening Balance" *Rate ,2),

#"Monthly Principal"= Payment - #"Monthly Interest",

#"Balance Remaining"= #"Opening Balance" - #"Monthly Principal"

]

 

),

 

LastN = Table.LastN(Table.FromRecords(ContractualInstalments), RemainingTerm),

RemoveColumns = Table.RemoveColumns(LastN, {"Opening Balance","Monthly Payment","Monthly Interest","Balance Remaining"}),

AddIndex = Table.AddIndexColumn(RemoveColumns, "Index", 0, 1)

RepaymentSchedule = Table.ReorderColumns(AddIndex,{"Funder","Index","Monthly Principal","Payment Number"})

 

OutputSchedule = OuterLoop(Source, ContractualInstalments),

 

FinalSchedules = Table.FromList(OutputSchedule)

 

in

 

FinalSchedules

2 REPLIES 2
wdx223_Daniel
Super User
Super User

it is hard to get your point just depending on the code.

could you provide some original data and the desired output?

Hi, Thanks for your response, much appreciated! Here is the Sample Data that's the Source in the Query.

 

AmortisingRateApplicationIDAPRDivision.1FinalFeeContractualInstalmentOpeningBalanceRemainingTermTerm
0.17802419370.18812Division 1160196.668000161
0.13414593800.160837Division 2160288.228596.5337
0.150768252820.159187Division 3160338.114490161

 

 

This is the Output from running the inner loop on just one account (AccountID 9380 in the Sampe Data).

 

Payment NumberOpening BalanceMonthly PaymentMonthly PrincipalMonthly InterestBalance RemainingFunder
18596.5288.22197.5790.658398.93Division 2
28398.93288.22199.6588.578199.28Division 2
38199.28288.22201.7686.467997.52Division 2
47997.52288.22203.8984.337793.63Division 2
57793.63288.22206.0482.187587.59Division 2
67587.59288.22208.2180.017379.38Division 2
77379.38288.22210.477.827168.98Division 2
87168.98288.22212.6275.66956.36Division 2
96956.36288.22214.8673.366741.5Division 2
106741.5288.22217.1371.096524.37Division 2
116524.37288.22219.4268.86304.95Division 2
126304.95288.22221.7366.496083.22Division 2
136083.22288.22224.0764.155859.15Division 2
145859.15288.22226.4361.795632.72Division 2
155632.72288.22228.8259.45403.9Division 2
165403.9288.22231.2456.985172.66Division 2
175172.66288.22233.6754.554938.99Division 2
184938.99288.22236.1452.084702.85Division 2
194702.85288.22238.6349.594464.22Division 2
204464.22288.22241.1447.084223.08Division 2
214223.08288.22243.6944.533979.39Division 2
223979.39288.22246.2641.963733.13Division 2
233733.13288.22248.8539.373484.28Division 2
243484.28288.22251.4836.743232.8Division 2
253232.8288.22254.1334.092978.67Division 2
262978.67288.22256.8131.412721.86Division 2
272721.86288.22259.5228.72462.34Division 2
282462.34288.22262.2525.972200.09Division 2
292200.09288.22265.0223.21935.07Division 2
301935.07288.22267.8120.411667.26Division 2
311667.26288.22270.6417.581396.62Division 2
321396.62288.22273.4914.731123.13Division 2
331123.13288.22276.3811.84846.75Division 2
34846.75288.22279.298.93567.46Division 2
35567.46288.22282.245.98285.22Division 2
36285.22288.22285.213.010.01Division 2

 

 

I basically want this output for each of the accounts in the sample data. The query I posted performs a few further operations to remove payments from the schedule that are before the RemainingTerm and also adds an index number for the payments that are still valid.

 

Hope that helps but please do let me know if you have any other questions!!!

 

Cheers.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.