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?
| Window | from, from_type | to, to_type |
|---|---|---|
| running total, first row to current | 1, ABS | 0, REL |
| last 3 rows including current | -2, REL | 0, REL |
| previous 3 rows only | -3, REL | -1, REL |
| whole partition | 1, ABS | -1, ABS |
| current to last | 0, REL | -1, ABS |
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] ) ) )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?
| Use | When |
|---|---|
DATESINPERIOD, DATESYTD | calendar windows: the last 12 months, even if months are hidden |
WINDOW | windows over rows: visible months only, or a non-date order such as rank |
RUNNINGSUM, MOVINGAVERAGE | the 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?
- Microsoft Learn: WINDOW function (DAX)
- Microsoft Learn: PARTITIONBY function (DAX)
- Microsoft Learn: RUNNINGSUM function (DAX)
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.