Skip to content

Finance - Debtors

Anyone who has worked in a finance department knows how much effort goes into keeping track of unpaid invoices. This page builds a debtors workbook that takes a live feed of debtors from an ERP system, lets the team add their own notes and inputs in Excel, and adds the calculations needed to see what is overdue and by how long.

The workbook combines several techniques, which let it do the following:

  • fetch structured data directly from an ERP system or an API whenever it is refreshed
  • feed data typed into Excel back into the queries, so that a table can refer to itself
  • build lists that maintain themselves, and that keep manual inputs matched to the right rows of the live feed by matching on one or more columns
  • combine values from several rows into a single cell when summarising at a coarser level, such as from invoices to customers, which the Owner and Status columns in the customer summary show
  • convert amounts from the contract currency to the country’s currency and to a common currency

The workbook starts from two kinds of input.

Most ERP systems can provide a live feed of debtors for each country, listing each customer’s outstanding invoices. The example uses three feeds, one for each office.

The UK debtors feed, listing each invoice with its office, country, customer, date, transaction reference, invoice number, narrative, currency and amount

The US debtors feed, in the same layout as the UK feed

The China debtors feed, in the same layout as the UK feed

FX rates are usually just as easy to get, from the ERP system or from the internet. The table below holds an average rate for each month of the year, converting each country’s currency into GBP. Month-end balance sheet rates would work equally well, but this example uses average rates.

An FX rates table with columns for type, month, country, currency, home currency and rate, showing monthly average rates for the UK and the US

  • Import all four source tables into the Power Query Editor:

The Queries pane with a Source folder holding the UK_Debtors, US_Debtors, China_Debtors and FX_Rates queries

  • Each of these queries needs only one step, which links to the source table. Setting the data types at this stage is optional.

2. Build a customer list that maintains itself

Section titled “2. Build a customer list that maintains itself”

The payment terms for each customer come from contracts rather than from the ERP feed, so they have to be typed in by hand. This step builds a customer list from the feeds, so that any new customer appears on it automatically, ready for its payment terms to be entered.

  • Append the three debtors tables with the following step, which lists every invoice from every office:
= Table.Combine({UK_Debtors, US_Debtors, China_Debtors})
  • After the append, the data looks like this:

The appended debtors from all three offices in the Power Query Editor

  • Remove every column except Customer.
  • Remove duplicates, which leaves one row per customer:

A single Customer column listing each customer once

  • Name the query All Customers.
  • Load it to Excel. In Excel, add a Days column to the table, and type in the payment terms for each customer:

The payment terms table in Excel, with a Days column filled in by hand for each customer

  • Load this Excel table, including the new column, back into the Power Query Editor.
  • Name the new query All Customers (with Payment Days).

The Queries pane with a Payment Terms folder holding the All Customers and All Customers (with Payment Days) queries

  • Go back to the All Customers query and add a Merge Queries step that matches its Customer column to the Customer column of All Customers (with Payment Days), with a left outer join:

The Merge dialog joining All Customers to All Customers (with Payment Days) on the Customer column

  • Expand only the Days column, which gives this result:

Each customer with its payment terms in a Days column

  • Refresh the table in Excel. The query already loads to Excel, so the merge and expand steps appear there without being set up again.
  • Excel now shows two columns, Days and Days2. Delete Days2. The payment terms that were typed in now come from the query, so they stay with the right customer when the list changes.

The main table lists every invoice from every office, converted into GBP and into the office’s local currency, with the customer’s payment terms attached.

  • Start a new query that appends the three debtors feeds:
= Table.Combine({UK_Debtors, US_Debtors, China_Debtors})
  • Rename the Amount column to Amount [CCY], which makes it clear that the amounts are in the contract currency:

The renamed Amount [CCY] column

  • Add a column holding today’s date:
= Table.AddColumn(#"Changed Type", "Today", each DateTime.LocalNow() as datetime)
  • Give the new column the Date data type:

The data type menu for the Today column with Date selected

  • Add a Start of Month column calculated from the Today column:

The Custom Column dialog creating a Start of Month column with the formula Date.StartOfMonth applied to Today

  • Merge with FX_Rates on two columns, matching Start of Month to Month and Currency to Currency. This picks up the rate for each invoice’s contract currency in the current month:

The Merge dialog joining the invoices to FX_Rates on start of month and currency

  • Expand only the Rate column.
  • Add a custom column that converts the amount to GBP:

The Custom Column dialog creating Amount [GBP] by dividing Amount [CCY] by Rate

  • Merge with FX_Rates again on two columns, this time matching Start of Month to Month and Country to Country. This picks up the rate for the office’s local currency:

The Merge dialog joining the invoices to FX_Rates on start of month and country

  • Expand only the Rate column. Because the table already has a Rate column, the new one may arrive as Rate.1.
  • Add a custom column that converts the GBP amount into local currency:

The Custom Column dialog creating Amount [LCY] by multiplying Amount [GBP] by Rate.1

  • Remove both rate columns and the Today column, because they are no longer needed.
  • Add a conditional column that flags inter-company balances, where the customer is another company in the same group. Here the rule marks any customer whose name begins with the group’s name:

The Add Conditional Column dialog creating an Inter-Company column, which returns Inter-Company when the customer name begins with the group’s name and null otherwise

  • Reorder the columns, so that the amount columns sit together and the Inter-Company column sits near the front.
  • Merge with All Customers (with Payment Days) on the Customer column to bring in the payment terms:

The Merge dialog joining the invoices to All Customers (with Payment Days) on the Customer column

  • Expand the Days column, and name it Payment Terms.
  • Name the query All Debtors - by Invoice.
  • Load it to Excel, where it looks like this:

The debtors by invoice table in Excel, showing each invoice with its office, inter-company flag, customer, amounts in contract currency, local currency and GBP, and payment terms, with a total row

The table in Excel now needs two more kinds of column:

  • Calculated columns, which age each invoice and allow for cash received. These could be built in the query instead, but in Excel they are easier to change, because changing one means editing a formula.
  • Free-text columns, where the team records who is chasing each invoice and what they have heard. These can only be added in Excel, because their contents are typed in.

Add the following columns to the table in Excel, in this order.

  • Today
=TODAY()
  • Days since Invoiced
=[@Today]-[@Date]
  • Days Overdue / (Underdue)
=[@[Days since Invoiced]]-[@[Payment Terms]]
  • Not Due Yet
=IF([@[Days Overdue / (Underdue)]]<=0,[@[Amount '[GBP']]], 0)
  • 1 - 30 Days Overdue
=IF(AND([@[Days Overdue / (Underdue)]]>0,[@[Days Overdue / (Underdue)]]<=30),[@[Amount '[GBP']]],0)
  • 31 - 60 Days Overdue
=IF(AND([@[Days Overdue / (Underdue)]]>30,[@[Days Overdue / (Underdue)]]<=60),[@[Amount '[GBP']]],0)
  • 61 - 90 Days Overdue
=IF(AND([@[Days Overdue / (Underdue)]]>60,[@[Days Overdue / (Underdue)]]<=90),[@[Amount '[GBP']]],0)
  • 91 - 120 Days Overdue
=IF(AND([@[Days Overdue / (Underdue)]]>90,[@[Days Overdue / (Underdue)]]<=120),[@[Amount '[GBP']]],0)
  • 121 - 150 Days Overdue
=IF(AND([@[Days Overdue / (Underdue)]]>120,[@[Days Overdue / (Underdue)]]<=150),[@[Amount '[GBP']]],0)
  • 151 - 180 Days Overdue
=IF(AND([@[Days Overdue / (Underdue)]]>150,[@[Days Overdue / (Underdue)]]<=180),[@[Amount '[GBP']]],0)
  • Over 180 Days
=IF([@[Days Overdue / (Underdue)]]>180,[@[Amount '[GBP']]],0)
  • Cash Received [GBP], which converts the cash received from the contract currency to GBP, using the rate for the invoice’s currency and month:
=IFERROR(
[@[Cash Received '[CCY']]]/(INDEX(
FX_Rates[Rate],
MATCH(1,
( FX_Rates[Month] = [@[Start of Month]]) * ( FX_Rates[Currency] = [@Currency] ),
0)
) ),
""
)

The next columns age the balance left after cash received, by taking the cash away from whichever ageing column holds the invoice’s amount.

  • Not Due
=IF([@[Not Due Yet]] <> 0, [@[Not Due Yet]] -[@[Cash Received '[GBP']]], [@[Not Due Yet]] )
  • 0 - 30 days
=IF([@[1 - 30 Days Overdue]] <> 0, [@[1 - 30 Days Overdue]]-[@[Cash Received '[GBP']]], [@[1 - 30 Days Overdue]])
  • 31 - 60 days
=IF([@[31 - 60 Days Overdue]]<>0, [@[31 - 60 Days Overdue]]-[@[Cash Received '[GBP']]], [@[31 - 60 Days Overdue]])
  • 61 - 90 days
=IF([@[61 - 90 Days Overdue]]<>0, [@[61 - 90 Days Overdue]]-[@[Cash Received '[GBP']]], [@[61 - 90 Days Overdue]])
  • 91 - 120 days
=IF([@[91 - 120 Days Overdue]]<>0,[@[91 - 120 Days Overdue]]-[@[Cash Received '[GBP']]],[@[91 - 120 Days Overdue]])
  • 121 - 150 days
=IF([@[121 - 150 Days Overdue]]<>0,[@[121 - 150 Days Overdue]]-[@[Cash Received '[GBP']]],[@[121 - 150 Days Overdue]])
  • 151 - 180 days
=IF([@[151 - 180 Days Overdue]]<>0,[@[151 - 180 Days Overdue]]-[@[Cash Received '[GBP']]],[@[151 - 180 Days Overdue]])
  • More than 180 days
=IF([@[Over 180 Days]]<>0,[@[Over 180 Days]]-[@[Cash Received '[GBP']]],[@[Over 180 Days]])
  • Balance [GBP] after Payments
=SUM(All_Debtors___by_Invoice[@[Not Due]:[More than 180 days]])
  • Fully Paid?, which treats any balance within 50p of zero as paid:
=IF(AND([@[Balance '[GBP'] after Payments]]<0.5,[@[Balance '[GBP'] after Payments]]>-0.5),"Yes","No")
  • Cash Received [CCY]
  • Date Last Chased
  • Date Chased Again
  • Owner
  • NOTES
  • Status
  • Estimated Payment Date
  • Amount [CCY] Est
  • Confirmed Payment Date
  • Amount [CCY] Cfd

5. Feed the manual inputs back into the query

Section titled “5. Feed the manual inputs back into the query”

The free-text columns sit beside a table that refreshes from a live feed. When the debtors change, the rows can move, and a note typed beside one invoice would then sit beside another. The calculated columns are not affected, because every row holds the same formula, but the free-text columns need tying to their invoices. The fix is to read the Excel table back into Power Query and merge the typed-in values onto each invoice by its transaction reference.

  • Load the Excel table, with all its extra columns, back into the Power Query Editor.
  • Name the new query All Debtors - by Invoice (with Manual Inputs).
  • If the Excel table has a total row, filter out the row labelled Total.

The Queries pane with a Debtors folder holding All Debtors - by Invoice and All Debtors - by Invoice (with Manual Inputs)

  • Go back to the All Debtors - by Invoice query, and add a Merge Queries step that matches its Transaction Ref column to the Transaction Ref column of All Debtors - by Invoice (with Manual Inputs):

The Merge dialog joining All Debtors - by Invoice to its own Excel output on the Transaction Ref column

  • Expand the free-text columns from the merged table. The picture shows only some of the ticked columns:

The expand dialog with Cash Received [CCY], the chasing dates, Owner, NOTES, Status and the estimated and confirmed payment columns ticked, and the calculated columns left unticked

  • Refresh the table in Excel. The free-text columns now appear twice, and the copies usually have a 1 or a 2 added to their names. Delete the duplicates, and check that the calculated columns still work, because some of their formulas may refer to the duplicated columns.
  • If the columns are not in the order you want, cut and paste them within the Excel table to rearrange them.

The result is a single table that holds the columns from the query, the calculated columns and the free-text columns, with each note tied to its invoice:

The finished debtors by invoice sheet, with a check that its total matches the raw data, the columns from the ERP feed, the ageing columns, the balance after payments, and the columns for typed-in data

The finished query should look something like this:

let
Source = Table.Combine({UK_Debtors, US_Debtors, China_Debtors}),
#"Renamed Columns" = Table.RenameColumns(Source,{{"Amount", "Amount [CCY]"}}),
#"Inserted Today" = Table.AddColumn(#"Renamed Columns", "Today", each DateTime.LocalNow() as datetime),
#"Changed Type1" = Table.TransformColumnTypes(#"Inserted Today",{{"Today", type date}}),
#"Inserted Start of Month" = Table.AddColumn(#"Changed Type1", "Start of Month", each Date.StartOfMonth([Today]), type date),
#"Merged Queries" = Table.NestedJoin(#"Inserted Start of Month", {"Start of Month", "Currency"}, FX_Rates, {"Month", "Currency"}, "FX_Rates", JoinKind.LeftOuter),
#"Expanded FX_Rates" = Table.ExpandTableColumn(#"Merged Queries", "FX_Rates", {"Rate"}, {"Rate"}),
#"Added Custom" = Table.AddColumn(#"Expanded FX_Rates", "Amount [GBP]", each [#"Amount [CCY]"] / [Rate]),
#"Merged Queries1" = Table.NestedJoin(#"Added Custom", {"Start of Month", "Country"}, FX_Rates, {"Month", "Country"}, "FX_Rates", JoinKind.LeftOuter),
#"Expanded FX_Rates1" = Table.ExpandTableColumn(#"Merged Queries1", "FX_Rates", {"Rate"}, {"Rate.1"}),
#"Added Custom1" = Table.AddColumn(#"Expanded FX_Rates1", "Amount [LCY]", each [#"Amount [GBP]"] * [Rate.1]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Rate", "Rate.1", "Today"}),
#"Added Conditional Column" = Table.AddColumn(#"Removed Columns", "Inter-Company", each if Text.StartsWith([Customer], "VidStar") then "Inter-Company" else null),
#"Reordered Columns" = Table.ReorderColumns(#"Added Conditional Column",{"Office", "Country", "Inter-Company", "Customer", "Start of Month", "Date", "Transaction Ref", "Invoice No.", "Invoice Narrative", "Currency", "Amount [CCY]", "Amount [LCY]", "Amount [GBP]"}),
#"Merged Queries2" = Table.NestedJoin(#"Reordered Columns", {"Customer"}, #"All Customers (with Payment Days)", {"Customer"}, "All_Customers with Payment Days", JoinKind.LeftOuter),
#"Expanded All_Customers with Payment Days" = Table.ExpandTableColumn(#"Merged Queries2", "All_Customers with Payment Days", {"Days"}, {"Payment Terms"}),
#"Merged Queries3" = Table.NestedJoin(#"Expanded All_Customers with Payment Days", {"Transaction Ref"}, #"All Debtors - by Invoice (with Manual Inputs)", {"Transaction Ref"}, "All Debtors - by Invoice (with Manual Inputs)", JoinKind.LeftOuter),
#"Expanded All Debtors - by Invoice (with Manual Inputs)" = Table.ExpandTableColumn(#"Merged Queries3", "All Debtors - by Invoice (with Manual Inputs)", {"Cash Received [CCY]", "Date Last Chased", "Date Chased Again", "Owner", "NOTES", "Status", "Estimated Payment Date", "Amount [CCY] Est", "Confirmed Payment Date", "Amount [CCY] Cfd"}, {"Cash Received [CCY]", "Date Last Chased", "Date Chased Again", "Owner", "NOTES", "Status", "Estimated Payment Date", "Amount [CCY] Est", "Confirmed Payment Date", "Amount [CCY] Cfd"})
in
#"Expanded All Debtors - by Invoice (with Manual Inputs)"

7. Build a debtors by office, country and customer query

Section titled “7. Build a debtors by office, country and customer query”

The last query summarises the invoice table at a coarser level, with one row for each office, country and customer. The amounts can simply be added up, but the Owner and Status columns hold text, so the summary lists the distinct values from each customer’s invoices in one cell.

  • Load the All Debtors - by Invoice table from Excel into the Power Query Editor.
  • If the Excel table has a total row, filter out the row labelled Total.
  • Use Group By, with the Advanced option, to group by Office, Country, Inter-Company and Customer, with the operation All Rows and the new column name Details:

The Group By dialog in Advanced mode, grouping by Office, Country, Inter-Company and Customer, with an All Rows operation into a Details column

  • The data now looks like this:

One row per office and customer, each holding a table of its invoices in the Details column

  • Add two columns that list the distinct owners and the distinct statuses in each group’s invoices, as two separate steps:
= Table.AddColumn(#"Grouped Rows", "Owner", each List.Distinct(Table.Column([Details], "Owner")))
= Table.AddColumn(#"Added Owner", "Status", each List.Distinct(Table.Column([Details], "Status")))

The data now looks like this:

The grouped rows with new Owner and Status columns, each holding a list

  • Turn each list into text, with the values separated by commas:
= Table.TransformColumns(#"Added Status", {"Owner", each Text.Combine(List.Transform(_, Text.From), ", "), type text})
= Table.TransformColumns(#"Extracted Owner", {"Status", each Text.Combine(List.Transform(_, Text.From), ", "), type text})

The data now looks like this:

The Owner and Status columns holding text, where one customer shows two owners’ initials and the statuses Medium and High

  • Remove the Details column, because it is no longer needed.

The amounts are summed in a second grouping, which starts again from the filtered invoices rather than from the table with the owners and statuses.

  • Click the fx button beside the formula bar to add a new step, and change its formula so that it refers to the second step of the query, where the Total row was filtered out. Then group that step by the same four columns. This time, instead of collecting all rows into Details, add a sum for each amount column that makes sense to total by office, country and customer, and an average for Payment Terms:

The Group By dialog in Advanced mode, grouping by Office, Country, Inter-Company and Customer, summing the amount columns and averaging Payment Terms

After the grouping, the data looks like this, though not all of its columns are shown:

The summed amounts for each office and customer, starting with Amount [CCY]

  • Merge the summed table with the step that holds the owners and statuses, matching on all four grouping columns. The Merge Queries dialog lists only other queries, not other steps of the same query, so write the step by hand:
= Table.NestedJoin(#"Grouped Rows 2", {"Office", "Country", "Inter-Company", "Customer"}, #"Removed Columns", {"Office", "Country", "Inter-Company", "Customer"}, "Grouped Rows 2", JoinKind.LeftOuter)
  • Expand the Owner and Status columns from the merged table. The last four columns now look like this:

The last four columns of the summary, showing More than 180 days, the balance after payments, and the Owner and Status text

  • Name the query All Debtors - by Customer.
  • Load it to Excel, where it looks like this. The screenshot was taken before a fix to the 151 - 180 days total, so its figure differs slightly:

The finished debtors by customer sheet, with a check that its total matches the raw data, the amounts by office and customer, the ageing columns, the balance after payments, and the owners and statuses

The same approach gives other summaries. Grouping by customer alone, for example, would show each customer’s total across all offices.

The finished query should look something like this:

let
Source = Excel.CurrentWorkbook(){[Name="All_Debtors___by_Invoice"]}[Content],
#"Filtered Rows" = Table.SelectRows(Source, each ([Office] <> "Total")),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Office", "Country", "Inter-Company", "Customer"}, {{"Details", each _, type table [Office=text, Country=nullable text, #"Inter-Company"=nullable text, Customer=nullable text, Start of Month=nullable datetime, Date=nullable datetime, Transaction Ref=nullable number, #"Invoice No."=nullable text, Invoice Narrative=nullable text, Currency=nullable text, #"Amount [CCY]"=number, #"Amount [LCY]"=number, #"Amount [GBP]"=number, Payment Terms=number, Today=nullable datetime, Days since Invoiced=number, #"Days Overdue / (Underdue)"=number, Not Due Yet=number, #"1 - 30 Days Overdue"=number, #"31 - 60 Days Overdue"=number, #"61 - 90 Days Overdue"=number, #"91 - 120 Days Overdue"=number, #"121 - 150 Days Overdue"=number, #"151 - 180 Days Overdue"=number, Over 180 Days=number, #"Cash Received [CCY]"=nullable number, #"Cash Received [GBP]"=number, Not Due=number, #"0 - 30 days"=number, #"31 - 60 days"=number, #"61 - 90 days"=number, #"91 - 120 days"=number, #"121 - 150 days"=number, #"151 - 180 days"=number, More than 180 days=number, #"Balance [GBP] after Payments"=number, Date Last Chased=any, Date Chased Again=any, Owner=any, NOTES=any, Status=any, #"Fully Paid?"=nullable text, Estimated Payment Date=any, #"Amount [CCY] Est"=nullable number, Confirmed Payment Date=any, #"Amount [CCY] Cfd"=nullable number]}}),
#"Added Owner" = Table.AddColumn(#"Grouped Rows", "Owner", each List.Distinct(Table.Column([Details], "Owner"))),
#"Extracted Owner" = Table.TransformColumns(#"Added Owner", {"Owner", each Text.Combine(List.Transform(_, Text.From), ", "), type text}),
#"Added Status" = Table.AddColumn(#"Extracted Owner", "Status", each List.Distinct(Table.Column([Details], "Status"))),
#"Extracted Status" = Table.TransformColumns(#"Added Status", {"Status", each Text.Combine(List.Transform(_, Text.From), ", "), type text}),
#"Removed Columns" = Table.RemoveColumns(#"Extracted Status",{"Details"}),
#"Grouped Rows 2" = Table.Group(#"Filtered Rows", {"Office", "Country", "Inter-Company", "Customer"}, {{"Amount [CCY]", each List.Sum([#"Amount [CCY]"]), type number}, {"Amount [LCY]", each List.Sum([#"Amount [LCY]"]), type number}, {"Amount [GBP]", each List.Sum([#"Amount [GBP]"]), type number}, {"Payment Terms", each List.Average([Payment Terms]), type number}, {"Not Due Yet", each List.Sum([Not Due Yet]), type number}, {"1 - 30 Days Overdue", each List.Sum([#"1 - 30 Days Overdue"]), type number}, {"31 - 60 Days Overdue", each List.Sum([#"31 - 60 Days Overdue"]), type number}, {"61 - 90 Days Overdue", each List.Sum([#"61 - 90 Days Overdue"]), type number}, {"91 - 120 Days Overdue", each List.Sum([#"91 - 120 Days Overdue"]), type number}, {"121 - 150 Days Overdue", each List.Sum([#"121 - 150 Days Overdue"]), type number}, {"151 - 180 Days Overdue", each List.Sum([#"151 - 180 Days Overdue"]), type number}, {"Over 180 Days", each List.Sum([Over 180 Days]), type number}, {"Cash Received [CCY]", each List.Sum([#"Cash Received [CCY]"]), type nullable number}, {"Cash Received [GBP]", each List.Sum([#"Cash Received [GBP]"]), type number}, {"Not Due", each List.Sum([Not Due]), type number}, {"0 - 30 days", each List.Sum([#"0 - 30 days"]), type number}, {"31 - 60 days", each List.Sum([#"31 - 60 days"]), type number}, {"61 - 90 days", each List.Sum([#"61 - 90 days"]), type number}, {"91 - 120 days", each List.Sum([#"91 - 120 days"]), type number}, {"121 - 150 days", each List.Sum([#"121 - 150 days"]), type number}, {"151 - 180 days", each List.Sum([#"151 - 180 days"]), type number}, {"More than 180 days", each List.Sum([More than 180 days]), type number}, {"Balance [GBP] after Payments", each List.Sum([#"Balance [GBP] after Payments"]), type number}}),
#"Merged Queries" = Table.NestedJoin(#"Grouped Rows 2", {"Office", "Country", "Inter-Company", "Customer"}, #"Removed Columns", {"Office", "Country", "Inter-Company", "Customer"}, "Grouped Rows 2", JoinKind.LeftOuter),
#"Expanded Grouped Rows 2" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows 2", {"Owner", "Status"}, {"Owner", "Status"})
in
#"Expanded Grouped Rows 2"

The complete workbook, with all the M code and the Excel workings, is available here: Debtors - Excel File