WINDOW in DAX: running totals and moving averages

How the WINDOW function returns a range of rows in DAX, absolute and relative positions, running totals, moving averages and first-to-current windows over sorted rows, PARTITIONBY for groups, and how WINDOW compares with DATESINPERIOD and RUNNINGSUM.

Short answer: WINDOW ( from, from_type, to, to_type, relation, ORDERBY ( ... ) [, PARTITIONBY ( ... )] ) returns a range of rows of a sorted table. from and to are REL (relative to the current row: -2 is two rows back) or ABS (position in the partition: 1 is the first row, -1 the last). In CALCULATE it gives running totals (1, ABS, 0, REL), moving windows (-2, REL, 0, REL) and whole-partition totals (1, ABS, -1, ABS) over any order, dates or not.

Running totals by date are easy with time intelligence. Running totals over visible rows only, over a sorted code or price, or restarting per group are where WINDOW helps.

Which windows can you express?

Windowfrom, from_typeto, to_type
running total, first row to current1, ABS0, REL
last 3 rows including current-2, REL0, REL
previous 3 rows only-3, REL-1, REL
whole partition1, ABS-1, ABS
current to last0, REL-1, ABS
WINDOW positions

ABS counts from 1 (the first row); negative ABS counts from the end. REL is the default type.

How do you write a running total?

Running Revenue =
CALCULATE (
    [Revenue],
    WINDOW ( 1, ABS, 0, REL, ALLSELECTED ( 'Date'[Year Month] ), ORDERBY ( 'Date'[Year Month] ) )
)

-- restarts each year
Running Revenue in Year =
CALCULATE (
    [Revenue],
    WINDOW (
        1, ABS, 0, REL,
        ALLSELECTED ( 'Date'[Year], 'Date'[Month Number] ),
        ORDERBY ( 'Date'[Month Number] ),
        PARTITIONBY ( 'Date'[Year] )
    )
)

-- 3-row moving average of monthly revenue
Revenue 3 Month Avg =
AVERAGEX (
    WINDOW ( -2, REL, 0, REL, ALLSELECTED ( 'Date'[Year Month] ), ORDERBY ( 'Date'[Year Month] ) ),
    [Revenue]
)

How do you build a Pareto curve?

A Pareto curve sorts customers by revenue and shows the cumulative share. Window functions match the current row through the columns of the relation, so they are at their best with column orders. For an order by a measure, the dependable model measure is the classic FILTER pattern, and on one visual the visual calculation RUNNINGSUM ( [Revenue] ) on a table sorted by revenue does it directly:

Cumulative Revenue by Rank =
VAR R = [Revenue]
RETURN
    CALCULATE ( [Revenue], FILTER ( ALLSELECTED ( Customer[Customer] ), [Revenue] >= R ) )

Cumulative Share =
DIVIDE ( [Cumulative Revenue by Rank], CALCULATE ( [Revenue], ALLSELECTED ( Customer[Customer] ) ) )
02040608010010%20%30%40%50%60%70%80%90%100%
Cumulative share of revenue by top share of customers (%)

Read it as "the top 20% of customers bring 68% of revenue". For the x-axis as a share, divide each customer's rank by the customer count. Customers with equal revenue share a point, as with ties in a rank.

WINDOW, DATESINPERIOD or RUNNINGSUM?

UseWhen
DATESINPERIOD, DATESYTDcalendar windows: the last 12 months, even if months are hidden
WINDOWwindows over rows: visible months only, or a non-date order such as rank
RUNNINGSUM, MOVINGAVERAGEthe same on one visual, as visual calculations

What goes wrong?

  • Ties in ORDERBY. Equal sort values make the order ambiguous, and running totals jump; add a tie-breaker column.
  • Visual sorted differently. The window follows ORDERBY, not the visual's sort; a running total in a table sorted by name looks random.
  • Relation and visual columns differ. Include every grouping column of the visual in the relation.
  • Large relations. Each cell evaluates the window over the whole relation; keep relations to the columns you need.

Where can you check this?

Where do you start?

Add Running Revenue in Year to a monthly table, then build the Pareto curve for your customers; it is one of the most useful charts in sales analysis. INDEX, RANK and ROWNUMBER pick single positions in the same kind of table.

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.