Implementing Running SUM in Power BI Visual Calculation (Part 2) WINDOW Function

In the previous post, we explored how to calculate cumulative sales and ABC classification by brand using the simplest and most intuitive RUNNINGSUM function. However, in business practice, beyond simple end-to-end accumulation, various calculation requirements arise depending on the analytical objective.

"What if you only want to calculate the total sales for the top 3 brands?"

"What if you want to group and calculate only the previous step and the current step together?"

Moments like these inevitably arrive when you need to freely control the calculation boundary (sliding window). The master key in Visual Calculations for these scenarios is the WINDOW function. In Part 2, based on the same brand sales data, we will examine the powerful control of the WINDOW function and how it carves out visual space to execute calculations.

[Link]

Basic Example Data

Sample table

1. Understanding the Structure of the WINDOW Function

While RUNNINGSUM is an ultra-convenient template built exclusively for cumulative calculations, the WINDOW function is a precision tool that allows users to specify the starting and ending points of a calculation boundary (window) within a visual object as desired.

WINDOW (

    from, [from_type],

    to, [to_type],

    [relation], [orderBy], [blanks], [partitionBy]

)

Rather than directly returning a scalar value, this function returns a 'table (visual space)' corresponding to the scope specified by the user.

2. Core Parameters: ABS, REL, and ORDERBY

To master the WINDOW function, you must understand three foundational concepts that determine the spatial coordinate system:

  • ABS (Absolute): Finds a fixed position based on the beginning or end of the visual object.
    • 1, ABS unconditionally signifies the "first row (topmost)" regardless of the underlying data.
  • REL (Relative): Denotes a relative distance based on the current row where the calculation is being evaluated.
    • 0, REL signifies "the current row where I am standing," while -1, REL signifies the "immediately preceding row."
  • ORDERBY: Explicitly designates the sort order that serves as the baseline for accumulation. To avoid "The Sorting Trap" discussed in Part 1, you must explicitly specify descending order by sales volume.

3. Combining SUMX with the WINDOW Function

Because the WINDOW function returns a space (table), to derive the actual sum, it must be paired with an iterator function like SUMX, which reads through this space row by row and aggregates the values.


Window RunningSales =  SUMX(     WINDOW(         1, ABS,                                0, REL,                                ORDERBY([Total Sales], DESC)       ),     [Total Sales] )


Window Running Sales Calculation Process


Window Running Sales Calculation Process


By fixing the starting point at 1, ABS (fixed) and setting the ending point to 0, REL (current row), the WINDOW expands as it moves down each row, cumulatively summing revenue from the first row down to the current row.

Wrapping Up

In Part 2, we covered the fundamental concepts of the WINDOW function along with the ABS and REL coordinate systems, and explored how to compute a cumulative total (Running Total) by locking the starting position.

What happens, then, if instead of locking the first row (1, ABS), we change the starting point to a relative position (-2, REL) that moves dynamically alongside the current row?

In the upcoming Part 3, we will cover how to utilize the WINDOW function to implement Moving Sums and 3-row Moving Averages—some of the most frequently used analytical techniques in real-world business practice.

Go to Next Post


<Other posts on the blog>

Comments

Popular posts from this blog

DAX Deep Dive 02 : Calculated Column vs. Measure – The Essential Difference Every Power BI Pro Must Know

DAX CALENDAR Function Deep Dive and Practical Usage Guide

Standard Deviation (Part 2): Strategic Limitations and Complementary Perspectives in Standard Deviation Analysis