No More Manual Dates! Get Shopify Orders from 'X' Days Ago with Flow & GraphQL

Hey store owners! Ever found yourself needing to pull a report of all orders (or even draft orders) from, say, two weeks ago? Or maybe everything from the last 30 days, excluding the most recent week? It's a common scenario, especially when you're trying to analyze trends, follow up with customers, or run specific campaigns. But manually calculating those 'start' and 'end' dates every single time? That's a real pain, and honestly, a waste of your valuable time.

This exact challenge popped up in the Shopify Community forums recently, with a user, ctevan, asking a brilliant question: "How can I get all draft orders from 2 weeks ago, so between 7 and 14 days ago? All example queries I see using created_at use exact timestamps, not relative time." It's a fantastic point! Many examples out there focus on static dates, which isn't always practical for dynamic reporting.

Luckily, the community, as always, came through with some seriously helpful insights. What I love about these discussions is how they often reveal multiple paths to the same solution, catering to different comfort levels and technical needs. Let's dive into the two main approaches that emerged from that thread, one for the automators using Shopify Flow, and another for those who love to get their hands dirty with code via GraphQL.

For the Automators: Using Shopify Flow to Grab Dynamic Data

If you're looking for a no-code (or low-code) way to automate tasks and data pulls, Shopify Flow is your best friend. It's an incredibly powerful tool for setting up workflows based on events in your store. The brilliant folks in the community, specifically ecom-4all, pointed out that Flow's "Get data" action is perfect for this, especially because its query field understands Liquid!

Liquid is Shopify's templating language, and it's what allows us to dynamically calculate those tricky relative dates. Flow has a super handy date_minus filter that can subtract a specified duration (like "14 days" or "7 days") from a given date, such as "now".

Step-by-Step with Shopify Flow:

  1. Start a New Flow: In your Shopify admin, go to Apps > Shopify Flow. Click "Create workflow."
  2. Choose a Trigger: This depends on when you want this data pull to happen. For a daily or weekly report, you might use a "Scheduled time" trigger.
  3. Add an Action: After your trigger, add an action. Search for "Get data" and select it. You'll likely want to "Get orders" or "Get draft orders," depending on your specific need.
  4. Input the Query: This is where the magic happens. In the "Query" field, you'll use Liquid to build your dynamic date range. Here's the exact snippet shared in the thread:

    created_at:>='{{ "now" | date_minus: "14 days" | date: "%Y-%m-%dT%H:%M:%SZ" }}' created_at:<='{{ "now" | date_minus: "7 days" | date: "%Y-%m-%dT%H:%M:%SZ" }}'

    This query tells Flow to find orders (or draft orders) where the created_at timestamp is greater than or equal to 14 days ago, AND less than or equal to 7 days ago. The date: "%Y-%m-%dT%H:%M:%SZ" part formats the date into an ISO 8601 string, which is what Shopify's API expects.

  5. Test and Confirm: Before you set your Flow live and pointing it at anything "destructive" (as ecom-4all wisely put it), run it once! Then, open the run log for that step. Flow will show you the resolved query after Liquid has done its work, so you can confirm the exact date range it used. This is a critical debugging step!
  6. Continue Your Flow: What do you want to do with this data? Send it in an email? Add it to a Google Sheet? The possibilities are endless with Flow!

For the Developers: Diving into GraphQL for Custom Solutions

If you're building a custom app, integration, or script, Shopify's GraphQL Admin API is incredibly powerful. For developers, thestoremechanic from the community provided excellent solutions using GraphQL, specifically for querying draftOrders (though the principles apply to regular orders too).

GraphQL allows for highly specific data requests, and crucially, its search syntax is quite flexible when it comes to dates.

GraphQL Approaches:

1. Using Relative Date Strings Directly in the Query

This is arguably the simplest method if your GraphQL client or tool supports passing relative date strings directly. Shopify's search syntax for created_at (and other date fields) understands values like -14d for "14 days ago."

query GetDraftOrders {
  draftOrders(query: "created_at:>=-14d created_at:<=-7d") {
    edges {
      node {
        id
        name
        createdAt
        # ... other fields you need
      }
    }
  }
}

Here, created_at:>=-14d means "created at or after 14 days ago" and created_at:<=-7d means "created at or before 7 days ago." It's wonderfully concise!

2. Calculating ISO Timestamps in Your Code

If you prefer more explicit control or your environment doesn't perfectly parse relative date strings in the query argument, you can calculate the exact ISO 8601 timestamps in your code before making the API call. This gives you absolute precision.

// Example JavaScript (Node.js) to calculate dates
const now = Date.now();
const fourteenDaysAgo = new Date(now - 14 * 864e5).toISOString(); // 864e5 = 24 * 60 * 60 * 1000 (milliseconds in a day)
const sevenDaysAgo = new Date(now - 7 * 864e5).toISOString();

const query = `
  query GetDraftOrders {
    draftOrders(query: "created_at:>='${fourteenDaysAgo}' created_at:<='${sevenDaysAgo}'") {
      edges {
        node {
          id
          name
          createdAt
          # ... other fields you need
        }
      }
    }
  }
`;

// Then execute this query via your GraphQL client

This approach involves a little more setup in your code, but it's robust and ensures you're sending exact, formatted timestamps to the API.

Which Method Should You Choose?

The best method really depends on your role and what you're trying to achieve:

  • If you're a store owner or operations manager looking to automate internal reports, email notifications, or data exports without writing custom code, Shopify Flow is likely your easiest and most powerful option. It's built right into your Shopify admin, and with a little Liquid magic, you can achieve sophisticated automations. If you're considering starting a new venture or enhancing your current operations, exploring Shopify's full suite of tools, including Flow, can be a game-changer. You can start your Shopify journey here and see how it can streamline your business.
  • If you're a developer building a custom application, a dashboard, or a complex integration that needs to pull specific data dynamically, then the GraphQL API offers unparalleled flexibility and control. You can integrate these queries directly into your codebase for seamless operations.

It's awesome to see how the Shopify platform, combined with the collective wisdom of its community, provides such versatile solutions to common challenges. Whether you're leveraging built-in tools like Flow or diving deep with the API, there's always a way to make your store run smarter, not harder. Don't be afraid to experiment with these methods and see which one fits your workflow best!

Share:

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools