March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
Register NowGet certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now
Hi everyone,
I'm trying to create a running sum measure in DAX that changes dynamically based on the applied filters. The running sum should accumulate values according to the rank of each row. I've already created separate tables, and they work as expected. However, when I attempt to implement this using a measure with virtual tables, it doesn't produce the desired result.
Here's the code I'm working with. Each part seems to work independently (e.g., ranking and filtering), but as a combined measure, it fails to return the correct running sum per rank.
CodigoArticulo | porc |
abc234hj21 | 0.001269819292983 |
bc234hj21 | 0.023401928347459 |
bc234hj223 | 0.032123394040404 |
bc234hj24gn | 0.011001110932900 |
If anyone has experience with handling ranks with tie-breakers in a measure like this, I’d appreciate any advice on how to make this running sum work dynamically with filters and rankings.
Thank you in advance for any help you can provide!
Solved! Go to Solution.
Hi @PipeRey
To create a dynamic running sum in DAX that respects ranks with tie-breakers and responds dynamically to filters, it can be helpful to streamline the virtual tables and focus on minimizing row context complexity in the RunningSum calculation.
Here's an optimized version of your Running_sum_measure:
Here’s an alternative DAX code that should improve efficiency and correct any potential ranking conflicts:
Running_sum_measure =
VAR tbl_1 =
ADDCOLUMNS(
FILTER(ALL(sales_table[CodigoArticulo]), [CQ1] > 0),
"porc", [PorcentajeParticipacionCQ1]
)
// Step 1: Create a table with ranks based on the "porc" values
VAR tbl_1_sorted =
ADDCOLUMNS(
tbl_1,
"Rank",
RANKX(
tbl_1,
[porc] + RAND() * 0.0001, // Ensures unique rank with small random adjustment
,
DESC,
Dense
)
)
// Step 2: Calculate the Running Sum based on the "Rank"
VAR running_sum_table =
ADDCOLUMNS(
tbl_1_sorted,
"RunningSum",
VAR CurrentRank = [Rank]
RETURN
CALCULATE(
SUMX(
FILTER(tbl_1_sorted, [Rank] <= CurrentRank),
[porc]
)
)
)
// Step 3: Sum up the running sums for the current filter context
RETURN
SUMX(running_sum_table, [RunningSum])
tbl_1: This table filters sales_table by [CQ1] > 0 and adds the porc value for each item.
tbl_1_sorted: Adds a Rank column to tbl_1 by ranking porc values in descending order. Adding RAND() * 0.0001 ensures uniqueness and avoids ties.
running_sum_table: This virtual table adds a RunningSum column that calculates the cumulative sum of porc values based on the current Rank. It does this by summing porc values for ranks less than or equal to the current rank within the table.
Return Statement: The final SUMX aggregates the RunningSum values for the entire table in the current filter context.
This approach should yield a dynamic running sum that respects filters, while reducing complexity in ranking and tie-breaking. Let me know if it works as expected!
Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂
Kind Regards,
Poojara
Data Analyst | MSBI Developer | Power BI Consultant
YouTube: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS
Hi @PipeRey
To create a dynamic running sum in DAX that respects ranks with tie-breakers and responds dynamically to filters, it can be helpful to streamline the virtual tables and focus on minimizing row context complexity in the RunningSum calculation.
Here's an optimized version of your Running_sum_measure:
Here’s an alternative DAX code that should improve efficiency and correct any potential ranking conflicts:
Running_sum_measure =
VAR tbl_1 =
ADDCOLUMNS(
FILTER(ALL(sales_table[CodigoArticulo]), [CQ1] > 0),
"porc", [PorcentajeParticipacionCQ1]
)
// Step 1: Create a table with ranks based on the "porc" values
VAR tbl_1_sorted =
ADDCOLUMNS(
tbl_1,
"Rank",
RANKX(
tbl_1,
[porc] + RAND() * 0.0001, // Ensures unique rank with small random adjustment
,
DESC,
Dense
)
)
// Step 2: Calculate the Running Sum based on the "Rank"
VAR running_sum_table =
ADDCOLUMNS(
tbl_1_sorted,
"RunningSum",
VAR CurrentRank = [Rank]
RETURN
CALCULATE(
SUMX(
FILTER(tbl_1_sorted, [Rank] <= CurrentRank),
[porc]
)
)
)
// Step 3: Sum up the running sums for the current filter context
RETURN
SUMX(running_sum_table, [RunningSum])
tbl_1: This table filters sales_table by [CQ1] > 0 and adds the porc value for each item.
tbl_1_sorted: Adds a Rank column to tbl_1 by ranking porc values in descending order. Adding RAND() * 0.0001 ensures uniqueness and avoids ties.
running_sum_table: This virtual table adds a RunningSum column that calculates the cumulative sum of porc values based on the current Rank. It does this by summing porc values for ranks less than or equal to the current rank within the table.
Return Statement: The final SUMX aggregates the RunningSum values for the entire table in the current filter context.
This approach should yield a dynamic running sum that respects filters, while reducing complexity in ranking and tie-breaking. Let me know if it works as expected!
Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂
Kind Regards,
Poojara
Data Analyst | MSBI Developer | Power BI Consultant
YouTube: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
Check out the November 2024 Power BI update to learn about new features.
User | Count |
---|---|
27 | |
21 | |
11 | |
11 | |
11 |
User | Count |
---|---|
60 | |
31 | |
22 | |
19 | |
19 |