Variables
Name values, not deferred instructions
A DAX variable has a value determined by its definition context. Referring to it inside CALCULATE does not rerun its defining measure under the new filters. A variable can hold a scalar or a table; it has local scope and is not a model column. Use the isolated Import table DiscountSales from labs/dax-iterators/discount-sales.csv; no relationships or RLS are required. Text: LineID and Region. Whole numbers: Quantity, UnitPriceUSD, DiscountPercent and GrossUSD. DiscountPercent is in percentage points (10 means 10%, not 0.10). GrossUSD is the pre-discount line amount. Five ordinary sale lines and one synthetic zero-quantity test line are present. Fixed USD, no taxes, returns or rounding policy beyond this integer-valued fixture. All results below are expected from independent fixture arithmetic; native DAX execution is NOT RUN. Create each named measure separately. DAX Query View queries run separately and do not inherit report slicers. Reuse [Net Revenue] from lesson 1.
Example 1 — Capture the numerator and recompute the denominator
Region Net Share =VAR CurrentNet = [Net Revenue]VAR AllRegionNet = CALCULATE([Net Revenue], REMOVEFILTERS(DiscountSales[Region]))RETURN DIVIDE(CurrentNet, AllRegionNet)Wrong Region Net Share =VAR CurrentNet = [Net Revenue]RETURN DIVIDE(CurrentNet, CALCULATE(CurrentNet, REMOVEFILTERS(DiscountSales[Region])))The second formula is intentionally incorrect: CALCULATE receives a fixed scalar rather than a measure to reevaluate. Use only Region selections, no other filters for this comparison. North share = 101/158 ≈ 63.92%, South = 57/158 ≈ 36.08%, Zero = 0%, total = 100%. The incorrect share is 100% in North and South and BLANK in Zero. If other filters exist, the correct denominator removes only Region, not every filter. This example uses the fact Region column because the isolated table has no region dimension.
Example 2 — Bind a value inside the iterator
Net From Local Variables =SUMX(DiscountSales, VAR LineGross = DiscountSales[Quantity] * DiscountSales[UnitPriceUSD] VAR Rate = DiscountSales[DiscountPercent] / 100 RETURN LineGross * (1 - Rate))Repeated Region Net (wrong) =VAR RegionNet = [Net Revenue]RETURN SUMX(DiscountSales, RegionNet)The first formula evaluates LineGross and Rate for the current sale line and returns the same 158 USD as [Net Revenue]. The second repeats the already captured scalar once per visible row: 948 USD at total (6 × 158), 303 USD in North (3 × 101), and 114 USD in South (2 × 57). With the zero row the repeated total remains 0 in that region, so one passing edge case does not establish correctness. Do not move row-dependent variables outside the iterator. Conversely, move a genuinely invariant result outside if that makes the intended scope clearer.
Table variables and debugging
Discounted Row Net =VAR DiscountedRows = FILTER(DiscountSales, DiscountSales[DiscountPercent] > 0)RETURN SUMX(DiscountedRows, DiscountSales[GrossUSD] * (1 - DiscountSales[DiscountPercent] / 100))The table variable contains L02, L03 and L05, totaling 84 USD net. A table variable is not a model table name: use the original qualified column reference for these input columns, not DiscountedRows[GrossUSD]. Table-valued results must be consumed by a scalar expression in a measure. During debugging, temporarily RETURN a scalar intermediate or expose table rows in a separate EVALUATE query. Variables may reduce repeated computation, but the optimizer controls execution; do not infer a specific number of storage-engine scans from syntax.
Practice — trace scope
For North, list the values captured by CurrentNet and AllRegionNet. Predict the correct and wrong share. Predict Discounted Row Net for South. State why declaring a scalar variable outside SUMX differs from defining LineGross inside SUMX.