xero-api

How to fetch previous months data with months over 30 days?


I want the previous 6 complete months profit and loss report data.

Using: ` GET https://api.xero.com/api.xro/2.0/Reports/ProfitAndLoss?periods=6&timeframe=MONTH

`

Will fetch this correctly, however alongside the current MTD, which I don't want.

Add a fromDate and toDate parameters (considering we're the Dec, and Nov was the last complete month) allows us in initially fetch the previous complete month, however limits the 5 prior months to 30 days data:

e.g. GET https://api.xero.com/api.xro/2.0/Reports/ProfitAndLoss?periods=5&timeframe=MONTH&toDate=2024-11-30&fromDate=2024-11-01

returns:

          "Cells": [
            {
              "Value": ""
            },
            {
              "Value": "30 Nov 24"
            },
            {
              "Value": "30 Oct 24"
            },
            {
              "Value": "30 Sep 24"
            },
            {
              "Value": "30 Aug 24"
            },
            {
              "Value": "30 Jul 24"
            },
            {
              "Value": "30 Jun 24"
            }
          ]
        },

Solution

  • That's as per the documentation, isn't it?

    Note: If you use the periods parameter in combination with the fromDate and toDate parameters then the specified date range will apply to each period (i.e. if the specified date range is for a 30 day month, each prior period will only include the first 30 days). To ensure you always get a full month of data in previous periods you would need to start in a month with 31 days (e.g. start in July instead of June)

    From: https://developer.xero.com/documentation/api/accounting/reports#profit-and-loss

    Sounds like a strange "feature", but not unexpected behaviour.