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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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