Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get 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

Reply
rpai27
Frequent Visitor

Is there a way to achieve a Selected row based cumulative sum using DAX?

I already have the data sorted and indexed. Is there a way to do the cummulative sum using DAX for the set of row that have the state set to True? 

 

Something like this:

 

rpai27_0-1607881617516.png

 

 

Any help is greatly appreciated! 

 

Note:

1. The data doens't have data field that's why I created the index field for the cummulative sum and getting the previous state value on BI.

2. I saw an example of this being done using m-code but my dataset is too big to incorporate that. The powerQuery never loaded and crashed the report. 😞 And for some reason I get better performance writing DAX than having the caculations done on powerQuery.

 

 

2 ACCEPTED SOLUTIONS
mahoneypat
Microsoft Employee
Microsoft Employee

Please try this column expression.

 

CumulativeIndex =
VAR vThisIndex = State[Index]
VAR vLastFalse =
    CALCULATE (
        MAX ( State[Index] ),
        State[State]
            FALSE (),
        State[Index] <= vThisIndex
    )
VAR vSum =
    CALCULATE (
        SUM ( State[Index] ),
        State[Index] <= vThisIndex,
        State[Index] > vLastFalse
    )
RETURN
    IF (
        State[State]
            TRUE (),
        vSum
    )

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

AlB
Super User
Super User

@rpai27 

You need to add  ALL(Sample_Data) to the CALCULATE in both vLastFalse and vSum to account for the effects of context transition

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

View solution in original post

4 REPLIES 4
rpai27
Frequent Visitor

Thank you @AlB @mahoneypat 


For anyone looking for a solution to a similar problem, here's the DAX formula after implementing ALL(Sample_data) suggested by

 

 

CumulativeIndex =
VAR vThisIndex = Sample_data[Index]
VAR vLastFalse =
CALCULATE (
MAX (Sample_data[Index] ),ALL(Sample_data),
Sample_data[State]
= FALSE (),
Sample_data[Index] <= vThisIndex
)
VAR vSum =
CALCULATE (
SUM ( Sample_data[Data]),ALL(Sample_data),
Sample_data[Index] <= vThisIndex,
Sample_data[Index] > vLastFalse
)
RETURN
IF (
Sample_data[State]
= TRUE (),
vSum
)

AlB
Super User
Super User

@rpai27 

You need to add  ALL(Sample_Data) to the CALCULATE in both vLastFalse and vSum to account for the effects of context transition

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

mahoneypat
Microsoft Employee
Microsoft Employee

Please try this column expression.

 

CumulativeIndex =
VAR vThisIndex = State[Index]
VAR vLastFalse =
    CALCULATE (
        MAX ( State[Index] ),
        State[State]
            FALSE (),
        State[Index] <= vThisIndex
    )
VAR vSum =
    CALCULATE (
        SUM ( State[Index] ),
        State[Index] <= vThisIndex,
        State[Index] > vLastFalse
    )
RETURN
    IF (
        State[State]
            TRUE (),
        vSum
    )

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


I modified the code a little bit to sum of the Data field instead of the index on line 12. But it doesn't look like it's doing the cummulative sum. Looks like it's just showing the data from the data field.

 

rpai27_0-1607901054141.png

 

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

Check out the October 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.