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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
DouweMeer
Post Prodigy
Post Prodigy

Running total, Mono poly style

So, I have the problem that if you start of with, say, 200 and then per turn have the option to earn some or lose some, randomly at turn, for example, 50 or 67, you may hit rock bottom, lost all your money and have to start over. After you have hit rock bottom, the counting start again from 200. 

If the only thing you have is your start position and the lost or earned values per turn, how can you define how much money one has in turn X? 

 

Turn123456789
Capital20018013019002000200230
Earn/ Loss0-20-5060-2500-250030

 

Like, till turn 5, you can just sum all earned and losses as a running total and add 200 starting capital to it. But at turn 6, that no longer works, neither at 7. In my brain, I know how to solve this. In Excel, I know how to solve this. Power BI on the other hand... not so much. 

 

How would you make it at turn 8 state you're starting a new run when all you have is this:

Start: 200

Turn123456789
Earn/ Loss0-20-5060-2500-250030

 ?

 

EDIT:

It be like this in Excel:

DouweMeer_0-1713198552992.png

 

1 ACCEPTED SOLUTION
v-zhengdxu-msft
Community Support
Community Support

Hi @DouweMeer 

 

Thanks for the reply from @Sahir_Maharaj and @Greg_Deckler , please allow me to provide another insight:

Please try this measure:

 

MEASURE =
VAR _currentTurn =
    MAX ( 'Table'[Turn] )
VAR _vtable =
    ADDCOLUMNS (
        SELECTCOLUMNS (
            ALLSELECTED ( 'Table' ),
            'Table'[Earn/ Loss],
            "_Turn", 'Table'[Turn]
        ),
        "_CurrentBN",
            ROUND (
                SUMX (
                    FILTER ( ALLSELECTED ( 'Table' ), [Turn] <= EARLIER ( [_Turn] ) ),
                    'Table'[Earn/ Loss]
                ) / 200,
                0
            )
    )
VAR _vtable2 =
    ADDCOLUMNS (
        _vtable,
        "_previoudN",
            IF (
                MAXX ( FILTER ( _vtable, [_Turn] = EARLIER ( [_Turn] ) - 1 ), [_CurrentBN] ) > [_CurrentBN],
                1
            )
    )
VAR _vtable3 =
    ADDCOLUMNS (
        _vtable2,
        "Sort", RANKX ( FILTER ( _vtable2, [_previoudN] <> BLANK () ), [_Turn],, ASC )
    )
RETURN
    MAXX (
        FILTER (
            ADDCOLUMNS (
                _vtable3,
                "_outcome",
                    IF (
                        200
                            + SUMX (
                                FILTER (
                                    _vtable3,
                                    [Sort] = EARLIER ( [Sort] )
                                        && [_Turn] <= EARLIER ( [_Turn] )
                                ),
                                [Earn/ Loss]
                            ) < 0,
                        0,
                        200
                            + SUMX (
                                FILTER (
                                    _vtable3,
                                    [Sort] = EARLIER ( [Sort] )
                                        && [_Turn] <= EARLIER ( [_Turn] )
                                ),
                                [Earn/ Loss]
                            )
                    )
            ),
            _currentTurn = [_Turn]
        ),
        [_outcome]
    )

 

The result is as follow:

vzhengdxumsft_0-1713258359254.png

 

 

Best Regards

Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-zhengdxu-msft
Community Support
Community Support

Hi @DouweMeer 

 

Thanks for the reply from @Sahir_Maharaj and @Greg_Deckler , please allow me to provide another insight:

Please try this measure:

 

MEASURE =
VAR _currentTurn =
    MAX ( 'Table'[Turn] )
VAR _vtable =
    ADDCOLUMNS (
        SELECTCOLUMNS (
            ALLSELECTED ( 'Table' ),
            'Table'[Earn/ Loss],
            "_Turn", 'Table'[Turn]
        ),
        "_CurrentBN",
            ROUND (
                SUMX (
                    FILTER ( ALLSELECTED ( 'Table' ), [Turn] <= EARLIER ( [_Turn] ) ),
                    'Table'[Earn/ Loss]
                ) / 200,
                0
            )
    )
VAR _vtable2 =
    ADDCOLUMNS (
        _vtable,
        "_previoudN",
            IF (
                MAXX ( FILTER ( _vtable, [_Turn] = EARLIER ( [_Turn] ) - 1 ), [_CurrentBN] ) > [_CurrentBN],
                1
            )
    )
VAR _vtable3 =
    ADDCOLUMNS (
        _vtable2,
        "Sort", RANKX ( FILTER ( _vtable2, [_previoudN] <> BLANK () ), [_Turn],, ASC )
    )
RETURN
    MAXX (
        FILTER (
            ADDCOLUMNS (
                _vtable3,
                "_outcome",
                    IF (
                        200
                            + SUMX (
                                FILTER (
                                    _vtable3,
                                    [Sort] = EARLIER ( [Sort] )
                                        && [_Turn] <= EARLIER ( [_Turn] )
                                ),
                                [Earn/ Loss]
                            ) < 0,
                        0,
                        200
                            + SUMX (
                                FILTER (
                                    _vtable3,
                                    [Sort] = EARLIER ( [Sort] )
                                        && [_Turn] <= EARLIER ( [_Turn] )
                                ),
                                [Earn/ Loss]
                            )
                    )
            ),
            _currentTurn = [_Turn]
        ),
        [_outcome]
    )

 

The result is as follow:

vzhengdxumsft_0-1713258359254.png

 

 

Best Regards

Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Why does everyone always try to solve it with a measure and never calculated columns 🙂 ?

 

Haven't tested it yet, but by what you did seems pretty much what I had in mind. You're creating a dynamic table of the full range at every turn to get to the value, pretty much what Excel does. "EARLIER" is quite handy with this, wasn't aware of that expression.

 

It does answer my raised question, but not really my challenge. I did solve it however myself by creating a custom table with per custom column a turn referring back to the prior. Luckily there's a fixed maximum amount of turns within the challenge that's doable manually and can be less.

The main challenge I have is two folds. One is that "the game" isn't ran once, but like a competition like a 100 times. Each is unique and can't be interchanged, which is why I dislike the measure solution. For the intended purpose, it's not allowed to change by context and should be able to be summarized on turns between the games. Another reason I dislike the measure solution. The other problem, the threshold of <0 varies each "turn", though I can easily see that referred back in your last sumx expressions within the maxx. 

 

Anyway, the principle you proposed gives me some things to think about. It's a better scaling method than my manual custom columns. Thank you :). 

Sahir_Maharaj
Super User
Super User

Hello @DouweMeer,

 

Can you please try the following:

Capital = 
VAR StartCapital = 200
VAR RunningTotal = 
    CALCULATE(
        SUM(TurnData[Earn_Loss]),
        FILTER(
            ALL(TurnData),
            TurnData[Turn] <= MAX(TurnData[Turn])
        )
    )
VAR ResetCheck =
    SUMX(
        FILTER(
            ALL(TurnData),
            TurnData[Turn] <= MAX(TurnData[Turn])
        ),
        [Earn_Loss]
    )
RETURN
IF(
    RunningTotal + StartCapital < 0,
    StartCapital,
    RunningTotal + StartCapital
)

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,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution?
➤ Lets connect on LinkedIn: Join my network of 13K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

I'm not seeing within your measure at turn 8 the possibility for a third time a new starting capital. Wouldn't your measure do this?

 

Turn123456789
Earn/ Loss0-20-5060-2500-250030
Running Total0-20-70-10-260-260-510-510-480
Measure200180130190200200200200200

 

Like, besides at 5 it should turn 0 instead of 200, but the running total just keeps counting after reset. 

Greg_Deckler
Super User
Super User

@DouweMeer The Earn/Loss being 0 indicates the start of a new run.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

No, like with the game, you can also just have a turn without loss or profit. By itself it is insufficient. 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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