Opening and closing balances in DAX: stock and accounts

How to calculate opening and closing balances and other semi-additive measures in DAX, stock levels, account balances and headcount that must not be summed over time, CLOSINGBALANCEMONTH, OPENINGBALANCEMONTH, LASTDATE, LASTNONBLANK, and the last date with data.

Short answer: stock levels, account balances and headcount are semi-additive: you can add them across products or accounts, but not across time (100 units on Monday and 100 on Tuesday is not 200). Show the value on the last date of the period: CLOSINGBALANCEMONTH ( SUM ( Stock[Units] ), 'Date'[Date] ), or CALCULATE ( SUM ( Stock[Units] ), LASTDATE ( 'Date'[Date] ) ) for any period. The opening balance is the previous period's closing: OPENINGBALANCEMONTH. If snapshots are missing on the last calendar day, use the last date with data (LASTNONBLANK).

A stock table with one row per product per day, summed over October, shows 31 times the real stock. It is one of the most common wrong numbers in inventory and finance reports.

Why can't you sum over time?

DateUnits in stock
1 Oct120
2 Oct110
......
31 Oct95
SUM for October3,410
Closing stock, 31 Oct95
Opening stock (30 Sep)125
Daily stock snapshots of one product

Across products the units add up (product A 95 plus product B 40 is 135 units in stock). Across days they do not.

Which measures do you need?

Units = SUM ( Stock[Units] )

-- closing: the last date of the period in context
Closing Units = CALCULATE ( [Units], LASTDATE ( 'Date'[Date] ) )

-- the same for month, quarter and year
Closing Units Month = CLOSINGBALANCEMONTH ( [Units], 'Date'[Date] )
Closing Units Quarter = CLOSINGBALANCEQUARTER ( [Units], 'Date'[Date] )
Closing Units Year = CLOSINGBALANCEYEAR ( [Units], 'Date'[Date] )

-- opening: the end of the previous period
Opening Units Month = OPENINGBALANCEMONTH ( [Units], 'Date'[Date] )

Change in Month = [Closing Units Month] - [Opening Units Month]

-- average stock over the period (sum of days divided by days)
Avg Units = AVERAGEX ( VALUES ( 'Date'[Date] ), [Units] )

LASTDATE works at any level: a day shows that day, a month its last day, a year 31 December. CLOSINGBALANCEMONTH always evaluates at the last date of the month in context.

What if the last day has no snapshot?

Snapshots are often taken on working days only, or the current month is not over. On 31 October (a Saturday) there is no row, and LASTDATE returns blank. Use the last date that has data:

Closing Units =
CALCULATE (
    [Units],
    LASTNONBLANK ( 'Date'[Date], [Units] )
)

LASTNONBLANK ( column, expression ) returns the last value of the column for which the expression is not blank. For a whole portfolio where each product has its own last date, decide what is right: the last date of any product (above) or each product's own last balance:

Closing Units per Product Last =
SUMX (
    VALUES ( 'Product'[Product] ),
    CALCULATE ( [Units], LASTNONBLANK ( 'Date'[Date], [Units] ) )
)

Where else is this needed?

  • Account balances in a trial balance table: closing balance per account and month.
  • Headcount: employees on the last day; average headcount over the year for turnover.
  • Open orders or tickets: the backlog at the end of each day.
  • Prices and exchange rates: the rate at the end of the period, not the sum of rates.

If you only have transactions (receipts and issues), compute the balance as a running sum of movements up to the date, then treat it the same way.

What goes wrong?

  • Default summarization. A stock column dragged into a visual is summed over days; set it to Don't summarize and use measures.
  • Closing balance blank for the current month. The month's last day is in the future; use LASTNONBLANK.
  • Totals across time. The year total of Closing Units is the 31 December value, which is correct; readers sometimes expect a sum. Label it "closing".
  • Averages of balances. The average stock is the average over days, not the average of month-end balances; pick one and name it.

Where can you check this?

Where do you start?

Find every measure on a snapshot table and replace sums over time with Closing Units using LASTNONBLANK. Time intelligence functions lists the quarter and year versions.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.