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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
AdityaG
Helper I
Helper I

Reply to earlier post on calculated column from two col. to add previous rows only if 1st col. is 0

https://community.powerbi.com/t5/DAX-Commands-and-Tips/Calculated-column-from-two-columns-to-add-pre...

 

Reference to the above query. Somehow my replies are getting automatically deleted, so created a new post here.

 

Thanks for the solution to the above-linked problem.

But my database is large and complex in comparison to what I shared, hence it's not quite working for me. Also, it's not the case where values would appear only for Q1.

 

Can someone replace quarters with index number and send me the revised output according to below table.

 

1 4280428
2 01429
3 4313431
4 02433
5 00433
6 4351435
7 02437
8 04441

 

Basically in Excel, column D would be: if(B2>0, B2, C2+D1)

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1684066974580.png

 

 

Expected result CC = 
VAR _table =
    ADDCOLUMNS (
        Data,
        "@condition",
            IF (
                Data[Value] <> 0
                    && MAXX ( FILTER ( Data, Data[Index] = EARLIER ( Data[Index] ) - 1 ), Data[Value] )
                        IN { 0, BLANK () },
                1,
                0
            )
    )
VAR _grouptable =
    ADDCOLUMNS (
        _table,
        "@group", SUMX ( FILTER ( _table, Data[Index] <= EARLIER ( Data[Index] ) ), [@condition] )
    )
VAR _resulttable =
    ADDCOLUMNS (
        _grouptable,
        "@result", IF ( Data[Value] > 0, Data[Value], Data[Add] )
    )
VAR _resultaccumulate =
    ADDCOLUMNS (
        _resulttable,
        "@accumulate",
            SUMX (
                FILTER (
                    _resulttable,
                    [@group] = EARLIER ( [@group] )
                        && Data[Index] <= EARLIER ( Data[Index] )
                ),
                [@result]
            )
    )
RETURN
    MAXX (
        FILTER ( _resultaccumulate, Data[Index] = EARLIER ( Data[Index] ) ),
        [@accumulate]
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

2 REPLIES 2
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1684066974580.png

 

 

Expected result CC = 
VAR _table =
    ADDCOLUMNS (
        Data,
        "@condition",
            IF (
                Data[Value] <> 0
                    && MAXX ( FILTER ( Data, Data[Index] = EARLIER ( Data[Index] ) - 1 ), Data[Value] )
                        IN { 0, BLANK () },
                1,
                0
            )
    )
VAR _grouptable =
    ADDCOLUMNS (
        _table,
        "@group", SUMX ( FILTER ( _table, Data[Index] <= EARLIER ( Data[Index] ) ), [@condition] )
    )
VAR _resulttable =
    ADDCOLUMNS (
        _grouptable,
        "@result", IF ( Data[Value] > 0, Data[Value], Data[Add] )
    )
VAR _resultaccumulate =
    ADDCOLUMNS (
        _resulttable,
        "@accumulate",
            SUMX (
                FILTER (
                    _resulttable,
                    [@group] = EARLIER ( [@group] )
                        && Data[Index] <= EARLIER ( Data[Index] )
                ),
                [@result]
            )
    )
RETURN
    MAXX (
        FILTER ( _resultaccumulate, Data[Index] = EARLIER ( Data[Index] ) ),
        [@accumulate]
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Thanks for the solution. It seems to work, but if you don't mind can you just change the last 2 entries as the below number and see if the desired output comes up when you run the same code:

 

7  438  2   438

8  440  4   440

 

Again, apologies for not entering the table, as the replies are automatically getting deleted.

 

Edit: No worries, I found the small bug, that was giving erroneous error. The var _table should be just this:

var _table = addcolumns(data,"@condition",if(data[value]<>0,1,0))

Thanks a ton for the effort though. Really appreciate it.

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors