Daily DAX : Day 156 WINDOW

 The WINDOW function in Power BI's DAX (Data Analysis Expressions) language is a powerful addition introduced to enhance analytical capabilities, particularly for working with ordered data sets and performing calculations over dynamic ranges or "windows" of rows. It’s commonly used in scenarios where you need to compute values based on a specific subset of rows relative to the current row, such as running totals, moving averages, or rankings within a defined range.

Syntax

dax


WINDOW(

    <StartOffset>,

    <StartRelation>,

    <EndOffset>,

    <EndRelation>,

    [OrderBy],

    [Blanks],

    [PartitionBy]

)


Parameters:


    StartOffset: An integer defining the starting position of the window relative to the current row. Positive values move forward, negative values move backward.

    StartRelation: Specifies how the start of the window relates to the data (e.g., RELATIVE for a relative offset from the current row).

    EndOffset: An integer defining the ending position of the window relative to the current row.

    EndRelation: Specifies how the end of the window relates to the data (e.g., RELATIVE for a relative offset).

    OrderBy (optional): Defines the column(s) and sort order (ASC or DESC) to determine the sequence of rows.

    Blanks (optional): Specifies how blank values are treated (e.g., KEEP or SKIP).

    PartitionBy (optional): Divides the data into partitions (like groups), and the window calculation is applied within each partition separately.


What It Does

The WINDOW function defines a dynamic range of rows (a "window") based on the specified offsets and relationships. This window can then be used with aggregation functions like CALCULATE, SUMX, or AVERAGEX to perform calculations over that range.

Use Case Example: Calculating a 3-Day Moving Average

Suppose you have a sales table with columns Date and SalesAmount, and you want to calculate a 3-day moving average of sales (average of the current day and the two previous days).

Sample Data:

Date                    SalesAmount    

2025-03-13        100

2025-03-14        150

2025-03-15        200

2025-03-16        250

2025-03-17        300


DAX Formula:

dax


ThreeDayMovingAvg = 

CALCULATE(

    AVERAGE('Sales'[SalesAmount]),

    WINDOW(

        -2, RELATIVE,  -- Start 2 rows before the current row

        0, RELATIVE,   -- End at the current row

        ORDERBY('Sales'[Date], ASC)

    )

)


Result:

Date                    SalesAmount        ThreeDayMovingAvg

2025-03-13        100                            100

2025-03-14        150                            125

2025-03-15        200                            150

2025-03-16        250                            200

2025-03-17        300                            250


    * On March 13, only one value exists, so the average is 100.

    * On March 14, it averages 100 and 150 = 125.

    * On March 15, it averages 100, 150, and 200 = 150.

    * And so on, always considering the current row and the two prior rows.


Key Use Cases


    Running Totals: Calculate cumulative sums up to the current row.

        Example: CALCULATE(SUM('Sales'[SalesAmount]), WINDOW(0, RELATIVE, -9999, RELATIVE, ORDERBY('Sales'[Date], ASC)))

    Moving Averages: Compute averages over a fixed or dynamic range (e.g., 7-day or 30-day averages).

    Rankings Within Groups: Use with PartitionBy to rank items within categories.

    Comparing Values: Analyze differences between the current row and a previous row (e.g., sales growth over time).


Why It’s Useful


    Dynamic Windows: Unlike static ranges, WINDOW adjusts based on the current row and sorting order.

    Flexibility: Works with any aggregation function and supports partitioning for group-level analysis.

    Time Intelligence: Perfect for time-based calculations without complex filtering logic.


In summary, the WINDOW function simplifies advanced analytical tasks in Power BI by allowing you to define and compute over custom ranges of data efficiently. It’s especially valuable for financial, sales, or trend analysis where context over a sequence of rows is critical.

Comments

Popular posts from this blog

Daily DAX : Day 65 INFO.TABLEPERMISSIONS

Daily DAX : Day 55 PV