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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

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
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.