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 inputs
Section titled “The inputs”The workbook starts from two kinds of input.
Debtors
Section titled “Debtors”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.



FX rates
Section titled “FX rates”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.

Building the queries
Section titled “Building the queries”1. Import the data
Section titled “1. Import the data”- Import all four source tables into the Power Query Editor:

- 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:

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

- 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:

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

- 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:

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

- 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.
3. Build a debtors by invoice query
Section titled “3. Build a debtors by invoice query”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](/_astro/finance-debtors-12.BQdAmE0M_ZN0Ykj.webp)
- 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:

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

- 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:

- 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](/_astro/finance-debtors-16.DzoiVvRv_Zl3RK.webp)
- 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:

- 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](/_astro/finance-debtors-18.CZ0xdJSN_ZSdkAu.webp)
- 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:

- 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:

- 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:

4. Add calculated and free-text columns
Section titled “4. Add calculated and free-text columns”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.
Calculated columns
Section titled “Calculated columns”- 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")Free-text columns
Section titled “Free-text columns”- 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.

- 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):

- 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](/_astro/finance-debtors-24.Pp1frJ0a_Z114LNE.webp)
- 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:

6. The debtors by invoice query
Section titled “6. The debtors by invoice query”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 data now looks like this:

- 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:

- 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:

- 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:

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]](/_astro/finance-debtors-31.DhzT_8Jb_SyuvQ.webp)
- 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:

- 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 same approach gives other summaries. Grouping by customer alone, for example, would show each customer’s total across all offices.
8. The debtors by customer query
Section titled “8. The debtors by customer query”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"9. Download the workbook
Section titled “9. Download the workbook”The complete workbook, with all the M code and the Excel workings, is available here: Debtors - Excel File