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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
dlagos
Regular Visitor

Sum a total in var

I have this dax, how to sum because the next dax is not working. how to fix.

VAR Dia = SELECTEDVALUE('Maestro Libro Diario Balance'[Accounting Date]) - SELECTEDVALUE('Maestro Libro Diario Balance'[Batch Created Date])
VAR RSC = IF(AND(Dia < 20, Dia > -20), 1, 0)
VAR TablaCondicional =
    ADDCOLUMNS(
        FILTER(
            ALL('Maestro Libro Diario Balance'),
            AND(
                ('Maestro Libro Diario Balance'[Accounting Date] - 'Maestro Libro Diario Balance'[Batch Created Date]) < 20,
                ('Maestro Libro Diario Balance'[Accounting Date] - 'Maestro Libro Diario Balance'[Batch Created Date]) > -20
            )
        ),
        "ValorCondicionado", RSC
    )
VAR TOTAL = SUMX(TablaCondicional, [ValorCondicionado])
RETURN
    TOTAL
1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your semantic model looks like, but try something like below if it suits your requirement.

 

expected result measure: =
VAR TablaCondicional =
    ADDCOLUMNS (
        FILTER (
            ALL ( 'Maestro Libro Diario Balance' ),
            AND (
                ( 'Maestro Libro Diario Balance'[Accounting Date] - 'Maestro Libro Diario Balance'[Batch Created Date] ) < 20,
                ( 'Maestro Libro Diario Balance'[Accounting Date] - 'Maestro Libro Diario Balance'[Batch Created Date] ) > -20
            )
        ),
        "ValorCondicionado",
            CALCULATE (
                IF (
                    AND (
                        SELECTEDVALUE ( 'Maestro Libro Diario Balance'[Accounting Date] )
                            - SELECTEDVALUE ( 'Maestro Libro Diario Balance'[Batch Created Date] ) < 20,
                        SELECTEDVALUE ( 'Maestro Libro Diario Balance'[Accounting Date] )
                            - SELECTEDVALUE ( 'Maestro Libro Diario Balance'[Batch Created Date] ) > -20
                    ),
                    1,
                    0
                )
            )
    )
VAR TOTAL =
    SUMX ( TablaCondicional, [ValorCondicionado] )
RETURN
    TOTAL

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
TheoC
Super User
Super User

Hi @dlagos 

 

It looks as though the RSC and Dia variables are not iterated in row context but calculated based on the filter context, and the additional column 'ValorCondicional' is always equal to the RSC variable and won't vary across the different rows. Also, the Total variable is attempting to sum over 'TablaCondicional' but because of the 'ValorCondicionado' being the same for every row, it is essentially multiplying the same value by the number of rows in 'TablaCondicional.

 

Try the below and hopefully this will help.

 

VAR TablaCondicional =
    ADDCOLUMNS (
        FILTER (
            ALL ( 'Maestro Libro Diario Balance' ) ,
            AND (
                ( 'Maestro Libro Diario Balance'[Accounting Date] - 'Maestro Libro Diario Balance'[Batch Created Date] ) < 20 ,
                ( 'Maestro Libro Diario Balance'[Accounting Date] - 'Maestro Libro Diario Balance'[Batch Created Date] ) > -20
            )
        ) ,
        "ValorCondicionado" , IF ( AND ( ( 'Maestro Libro Diario Balance'[Accounting Date] - 'Maestro Libro Diario Balance'[Batch Created Date] ) < 20 , ( 'Maestro Libro Diario Balance'[Accounting Date] - 'Maestro Libro Diario Balance'[Batch Created Date] ) > -20 ) , 1 , 0 )
    )
VAR TOTAL = SUMX ( TablaCondicional , [ValorCondicionado] )

RETURN

    TOTAL

 

Hopefully this helps (or at least the logic of it).

 

Theo 🙂

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your semantic model looks like, but try something like below if it suits your requirement.

 

expected result measure: =
VAR TablaCondicional =
    ADDCOLUMNS (
        FILTER (
            ALL ( 'Maestro Libro Diario Balance' ),
            AND (
                ( 'Maestro Libro Diario Balance'[Accounting Date] - 'Maestro Libro Diario Balance'[Batch Created Date] ) < 20,
                ( 'Maestro Libro Diario Balance'[Accounting Date] - 'Maestro Libro Diario Balance'[Batch Created Date] ) > -20
            )
        ),
        "ValorCondicionado",
            CALCULATE (
                IF (
                    AND (
                        SELECTEDVALUE ( 'Maestro Libro Diario Balance'[Accounting Date] )
                            - SELECTEDVALUE ( 'Maestro Libro Diario Balance'[Batch Created Date] ) < 20,
                        SELECTEDVALUE ( 'Maestro Libro Diario Balance'[Accounting Date] )
                            - SELECTEDVALUE ( 'Maestro Libro Diario Balance'[Batch Created Date] ) > -20
                    ),
                    1,
                    0
                )
            )
    )
VAR TOTAL =
    SUMX ( TablaCondicional, [ValorCondicionado] )
RETURN
    TOTAL

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.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.