Go straight to the guide
LaneMile

SUMIFS with 2 criteria: Total truck miles in Excel

LaneMile Editorial Team · Published · 12 minute read

Sources checked

Scope: use the jurisdiction stated in the guide and its sources.

LaneMile drafting rules: editorial standards.

Use SUMIFS with two criteria to add miles only where both the truck number and load type match your report.

Key takeaways

  • Keep each trip on its own row.
  • Match the truck number and load type to total loaded miles.
  • Lock data ranges before copying formulas.
  • Check empty miles with a separate formula.

If you keep a truck's miles in a spreadsheet, you probably log one row for each trip. Each row holds a date, a truck number, a driver, a mileage figure, and a note on whether the truck was carrying freight. Adding up one truck's miles is simple. Filter its rows and total the miles. The trouble starts when you need more than one answer from the same log. You might want every truck's total for the week, only the loaded miles for one truck, or the miles one driver ran in one truck. Each of those questions asks the sheet to add up only the rows that meet a set of conditions.

In this post we will set up a small truck-miles log, write a SUMIFS formula that uses two criteria, and check the result against the raw data. Everything here works in a standard Excel worksheet, with no macros or add-ins. A pivot table is another option once you are comfortable with the basics, but a formula is easier to copy, read, and audit.

Set up the log so formulas can read it

Put every trip on one sheet, one trip on each row, with a header row at the top. Avoid merged cells, blank rows in the middle of the data, and subtotal lines inside the log. A formula can only add what it can see, and a subtotal line between two trips is just another row to it. Keep your subtotals in a separate report area.

Use these five columns:

  • A: Date, the day the trip started or finished. Pick one rule and use it for every row.
  • B: Truck, the truck number, in the same format on every row.
  • C: Driver, which is optional but useful when you audit a month.
  • D: Miles, the miles for that trip, entered as a plain number.
  • E: Type, either loaded or empty, so you can separate miles with a load from miles run without one.

For practice, build a log in rows 2 to 8, with the header in row 1. Your log will be longer, but the logic is the same. One truck usually appears on several days, and some of its miles carry freight while others do not. That mix is what makes a two-criteria formula worth writing.

Accounting textbooks use the same idea of one record for each unit of work. OpenStax's managerial accounting text shows a single job cost sheet: "Figure 4.20 shows the job cost sheet for Job MAC001." ( OpenStax) Your truck log works the same way. Each trip is a record, and the totals come from adding up the records that match.

Read the formula one piece at a time

A SUMIFS formula has a fixed order. The first argument is the range of numbers to add up. After that, the formula takes pairs: a range to test, then the condition to test it against. Microsoft's SUMIFS reference gives the arguments this way: "Criteria_range1, criteria_range2... - One or more ranges in which to evaluate the associated criteria."

For this log, the pieces are:

  • Sum range: the miles column, D.
  • First criteria range: the truck column, B.
  • First criteria: the truck number you want, held in a cell.
  • Second criteria range: the type column, E.
  • Second criteria: the type you want, typed as text or held in a cell.

Reading the formula this way helps when something looks wrong. Check each pair on its own and confirm it points at the column you meant. A good habit is to write the pieces in plain words first, such as "add miles where the truck is the one in this cell," and only then type the formula. Plain words are easier to check than a long string of dollar signs and commas.

Start with one criterion

Before you add a second condition, get the single-condition version working. It shows you the pattern. Microsoft describes the function in plain terms: it "Adds the cells in a range that meet multiple criteria." The single-condition version uses one pair, and it is the right starting point for any truck report.

On the right side of the sheet, in column G, list the truck numbers you want to report, one on each row, starting at G2. In H2, enter:

=SUMIFS($D$2:$D$8, $B$2:$B$8, G2)

The formula says: add the miles in column D, but only in rows where column B matches the truck in G2. Copy it down, and each truck gets its own total. This is usually the first report a dispatcher needs. It does not show how much of that total carried freight. For that you need a second condition.

The dollar signs lock the ranges. When you copy the formula down, the ranges stay put and only the truck reference changes from row to row. Without the dollar signs, the ranges shift with each copy, and the totals quietly drift.

Add the second criterion

Here is the two-criteria version. In I2, enter:

=SUMIFS($D$2:$D$8, $B$2:$B$8, G2, $E$2:$E$8, "loaded")

Microsoft says each criterion can be "One or more criteria in the form of a number, expression, cell reference, or text that define which cells will be added." In this formula, the first criterion is the truck in G2 and the second is the text loaded. For each row, the function asks two questions. Is the truck in column B the one in G2? Is the type in column E loaded?

Microsoft's rule for combining the two questions is simple: "Each cell in sum_range is summed only if all the corresponding criteria specified are true for that cell."

Take one truck and walk through its rows. A row marked loaded passes both tests, so its miles go into the total. A row marked empty passes the truck test but fails the type test, so its miles are skipped.

Every range has to cover the same trips, row by row. The sum range, truck range, and type range should all run over rows 2 to 8 in this example. If you extend one range, extend them all. A mismatch is the first thing to check when a total looks wrong.

After you copy both formulas down the truck list, you have a small report with one row for each truck and two columns: all miles and loaded miles. If every trip is labelled loaded or empty, the gap between those columns is the empty mileage. A truck that ran only loaded trips shows the same figure in both columns. A truck with a lot of repositioning shows a wide gap.

Why the type label needs quotation marks

The quotation marks around loaded tell Excel to treat it as text typed directly into the formula. Microsoft's examples show the range of what a criterion can be: "For example, criteria can be expressed as 32, "32", " > 32", "apples", or B4." Typing "apples" in quotes and pointing at cell B4 are both valid.

Pointing at a cell is often better in practice. If you put the word loaded in K2, you can change the type you total by editing one cell instead of rewriting the formula. The second criterion then becomes K2 rather than "loaded". The formula reads the same, and the report stays flexible when your labels change.

In a truck log, the safest habit is to pick one format for truck numbers and use it everywhere, so the value you type in the report matches the values in the log.

Keep loaded and empty miles separate

Empty miles still cost something. The truck burns fuel, the driver spends hours on the road, and the equipment wears down, even though no freight rides on those miles. That is why many small carriers want both numbers. Loaded miles show the miles that carried freight. Total miles show the full distance the truck ran. For a separate cost exercise, see how to total an out-of-service day.

The two-criteria report gives you both figures from one log. You do not need a second table or a copy of the data. Label the columns so nobody has to guess. "All miles" and "Loaded miles" are clearer than "Total" and "Loaded."

Keeping the type column accurate makes this work. If a driver marks a trip as loaded when it was not, the loaded total will be wrong no matter how good the formula is. Give the people who fill in the log a short list of allowed values, and check the column once a week. A dropdown list in the Type column can help keep labels consistent.

Partial matches with wildcards

Sometimes a type label is more than one word. You may record loads as "loaded - dry van" and "loaded - reefer", and you want both in the total. Microsoft's guidance on wildcards covers this: "A question mark matches any single character; an asterisk matches any sequence of characters." So the criterion "loaded*" matches both labels, along with any other label that starts with loaded.

Use the wildcard in the same position as the plain text:

=SUMIFS($D$2:$D$8, $B$2:$B$8, G2, $E$2:$E$8, "loaded*")

Be careful, because wildcards match more than you might expect. A criterion of "load*" would also match "loaded" and any other label that begins with those letters. Test a wildcard on a few rows first and confirm the totals against a hand count.

Sometimes you need to search for the wildcard characters themselves. Microsoft explains the escape: "If you want to find an actual question mark or asterisk, type a tilde (~) before the character." Suppose your Type column sometimes says "late " with a real asterisk to flag a trip that ran late. The criterion "late~ " finds that literal asterisk. Without the tilde, the criterion would match any label that starts with late, and you would get more rows than you wanted.

Adding a date range

Comparison operators work in criteria too. Microsoft's example of " > 32" shows the form, and the same idea applies to dates. A criterion such as " > =" followed by a date restricts a total to trips from a certain day onward. Put the start date in a cell, join it to the operator, and point the formula at that cell.

A date window adds two more criteria pairs, one for the start date and one for the end date, because each boundary is its own condition. Every row must pass all of them. That is more to read and more to check, so keep the truck and type report simple until you need date limits.

Accounting textbooks face the same question with job records. OpenStax gives a clean example: "Job MAC 001: This job was started and completed during the month." ( OpenStax) That is the easy case, because the whole job falls inside one period. A trip that starts on the last day of one window and ends in the next needs a rule before you total it. When you add date limits, test the boundary dates by hand, because a trip on the edge of a window is the one most likely to be counted wrong.

Check the totals before you trust them

A formula can return a number that looks right and is still wrong. Three checks take a few minutes and catch most problems:

  1. Add the all-miles column. The sum of the truck totals should equal the total of column D across the whole log. If the two differ, check for missing or repeated trucks and incorrect ranges.
  2. Add loaded and empty miles together. Calculate empty miles with a separate SUMIFS using "empty" in the Type column; add that result to loaded miles and compare with the whole-log total. If they differ, check for type labels other than loaded or empty and incorrect ranges.
  3. Pick one truck and add its rows by hand. Filter the log to that truck, sum its loaded rows with a calculator, and compare the result with the formula. Doing this once on a small batch builds confidence before you rely on the formula for a larger log.

Run the checks every time you change the formulas, add a column, or paste in a new batch of trips.

Common mistakes that cause wrong totals

Several errors make totals look reasonable when they are not. Watch for these:

  • Ranges that do not cover the same rows. When you add trips, extend every range manually to include them. A trip typed below the last row of your ranges is ignored.
  • References that shift when copied. Lock the data ranges, and leave the truck reference relative so it changes from row to row.
  • Mixed truck formats. Enter truck numbers the same way in every row so each criterion matches the values in the log.
  • Stray spaces. A type label with a trailing space, such as "loaded ", can fail to match "loaded" and drop miles from the total. It looks identical on screen. Trim the column or check a few rows by eye.
  • TRUE and FALSE in the sum range. Microsoft notes that "Cells in sum_range that contain TRUE evaluate as 1; cells in sum_range that contain FALSE evaluate as 0 (zero)." If you point the sum range at a column of checkbox flags by mistake, you get counts instead of miles. Check that the sum range always points at the miles column.
  • Subtotal lines inside the log. A subtotal row inside the data can be picked up by a range that covers it, and it may not carry the truck or type label you filter on.

Adapt the layout to your own log

The examples use trucks and load status, but the same pattern works with other pairs. You could total one driver's miles on one truck, or total a load type for one customer. Change the column letters to match your sheet, keep the sum range on the miles column, and keep every criteria range the same height. The logic does not change.

Before you change anything, write down in one sentence what the new report should answer. That sentence tells you which columns the formula needs, and it keeps the report from growing into something nobody reads.

If your log is long, put the report on its own sheet, with the truck list in one column and each formula in the next. Keep the raw trips on their own sheet and avoid typing over them. When the report sits apart from the data, it is easy to see which numbers come from formulas and which were entered by hand.

For a separate task before booking, use the load board rate checks.

Put it together

Today, copy a few realistic rows into a blank sheet and build the formulas there. Once the totals match the hand counts, move the same formulas onto your real log and run the three checks again.

Related guides: Truck axle weight limits: 80,000 pounds, 5 sources · Heavy truck 12% excise tax: check 3 weight limits · Large truck crash rate: 2024 figures explained.

Questions about truck-mile totals

Can I change the load type without rewriting the formula?

Yes: put the type in K2 and use K2 as the second criterion. Edit that cell to change the type being totalled.

How do I check for unexpected type labels?

Compare independently calculated loaded and empty totals with all miles. A difference calls for checking labels and formula ranges.

Sources for this guide

This guide provides general information, not legal, tax, financial or safety advice. LaneMile is not a regulator, load broker, dispatch service or compliance certifier. Apply the authority and vendor guidance for your own operation.