Visual calculations and UDFs
Choose the calculation scope
Model measures and calculation items operate on semantic-model context. A visual calculation operates on aggregated cells within one visual; changing sort, axis or visible members can change its answer. A DAX user-defined function packages logic for reuse. The lab uses a modern Import model, with the function example optional when the engine lacks support. Do not mistake a visual-local calculation for a shared business metric.
Understand windows and feature gates
OFFSET selects relative positions; INDEX selects an absolute position; WINDOW returns a range. ORDERBY defines sequence, PARTITIONBY restarts the sequence by group, MATCHBY identifies the current row. RANK supports dense or skip ties. Use explicit relation and keys; a window does not imply a calendar period. On totals without a unique current row, relative functions may evaluate more than one row; guard measures or use detail-only output. See the lab support matrix for feature scope, DirectQuery and RLS limitations.
Example 1 — partitioned ordered rows
Import the disconnected WindowRows table. In DAX Query View run:
EVALUATEADDCOLUMNS(WindowRows, "PreviousAmount", MAXX(OFFSET(-1, WindowRows, ORDERBY(WindowRows[Sequence], ASC), DEFAULT, PARTITIONBY(WindowRows[Team]), MATCHBY(WindowRows[Sequence])), WindowRows[Amount]), "LastTwo", SUMX(WINDOW(-1, REL, 0, REL, WindowRows, ORDERBY(WindowRows[Sequence], ASC), DEFAULT, PARTITIONBY(WindowRows[Team]), MATCHBY(WindowRows[Sequence])), WindowRows[Amount]))ORDER BY WindowRows[Team], WindowRows[Sequence]A amounts 10,20,20 yield previous blank,10,20 and rolling sums 10,30,40; B amounts 7,9 yield blank,7 and 7,16. INDEX(1, ...) with the same partition/order returns the first row; amount-based dense ranks for A are 2,1,1 (descending). If a chronological key has ties, add a stable secondary row key; do not add a tie-breaker to a business rank that intentionally shares ranks.
Example 2 — visual order versus reusable function
Create a table of 2025 Revenue by Category sorted A,B,C. Add visual calculation Previous = PREVIOUS([Revenue]) (use rows axis, no hierarchy reset, hide totals). Predicted values blank,150,80. Sort by Revenue ascending instead: C,B,A, predictions blank,30,80. For a reusable ratio rule, test in DAX Query View on a compatible build:
DEFINE FUNCTION LabGrowth = (cur: NUMERIC, prior: NUMERIC) => IF(ISBLANK(cur) || ISBLANK(prior), BLANK(), DIVIDE(cur-prior,prior))EVALUATEROW("Growth", LabGrowth(150,100), "ZeroBase", LabGrowth(30,0))Expected 0.5 and blank. Do not click Update model unless working in an approved local copy and intentionally persisting the function.
Avoid cross-scope assumptions
Visual calculations cannot access model relationships through RELATED, RELATEDTABLE or USERELATIONSHIP; they also have export, filtering and visual-type limitations. PREVIOUS is visual-only. UDFs require compatibility 1702 or later; Microsoft documents GA from June 2026 in Desktop and Service. No recursion, overloading or automatic OLS propagation. Do not infer DirectQuery or RLS support from a successful Import query. Verify every target build and role before publishing. See support matrix for the remaining unverified combinations.
Practice — distinguish rank from position
For A amounts 10,20,20, predict dense versus skip rank in descending amount order, and first-row amount under sequence order. Remove sequence 2 and predict the two-row rolling sum at sequence 3. Explain why the previous visible row may not mean previous month. References: https://learn.microsoft.com/en-us/power-bi/transform-model/desktop-visual-calculations-overview; https://learn.microsoft.com/en-us/dax/best-practices/dax-user-defined-functions; https://learn.microsoft.com/en-us/dax/offset-function-dax; https://learn.microsoft.com/en-us/dax/window-function-dax; https://learn.microsoft.com/en-us/dax/best-practices/dax-understand-orderby