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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
danextian
Super User
Super User

SUMX shows incorrect grand total but correct at row level

Hello, 

 

I am stuck at doing a sumx with this formula. The formula below what it does is compute for the remaining invoice balance as of a given date but the numbers are in transaction currency (USD, NZD) and need to be converted in AUD. The converted to AUD balances of these invoices are then summed up to get the total balance.  In the InvoiceBalance table, there is a column indicating the transaction currency. The Currency, Rate and Month is in a separate table. 

 

InvoiceBalance - Current (Debtor) tc =
VAR __END_DATE =
    MIN ( TODAY (), [Max Date with Data] )
VAR __START_DATE =
    EOMONTH ( __END_DATE, -1 ) + 1
VAR __INVOICE =
    SELECTEDVALUE ( InvoiceBalance[Invoice ID] )
VAR __MAX_DATE = __END_DATE
VAR __MIN_DATE =
    EOMONTH ( __MAX_DATE, -1 ) + 1
VAR __INV =
    ROUND (
        CALCULATE (
            [TotalInvoiceAmount TC],
            ALL ( Dates ),
            FILTER (
                ALL ( InvoiceBalance[Date] ),
                InvoiceBalance[Date] >= __START_DATE
                    && InvoiceBalance[Date] <= __END_DATE
            ),
            InvoiceBalance[Type] = "ACCREC"
        ),
        2
    )
VAR __DEDUCTIONS =
    ROUND (
        CALCULATE (
            [TotalInvoiceDeductions TC],
            ALL ( Dates ),
            FILTER (
                ALL ( InvoiceBalance[Date] ),
                InvoiceBalance[Date] >= __START_DATE
                    && InvoiceBalance[Date] <= __END_DATE
            ),
            FILTER ( ALL ( InvoiceBalance[Dates] ), InvoiceBalance[Dates] <= __MAX_DATE ),
            InvoiceBalance[Type] = "ACCREC"
        ),
        2
    )
RETURN
    __INV - __DEDUCTIONS

 

I tried doing a SUMX but the total is unexpected --way overstated. Instead of:

RETURN
    __INV - __DEDUCTIONS


I entered:

 

RETURN
    SUMX (
        InvoiceBalance,
        IF (
            InvoiceBalance[Currency] = "AUD",
            __INV - __DEDUCTIONS,
            ( __INV - __DEDUCTIONS )
                * CALCULATE (
                    MAX ( FXRate[Rate] ),
                    FILTER (
                        FXRate,
                        FXRate[Month] = MAX ( Dates[Year Month] )
                            && FXRate[Currency] = InvoiceBalance[Currency]
                    )
                )
        )
    )

The result is correct at  invoice level but total seems to show the total of all rows instead of what is the balance multiplied by currency rate.

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
4 REPLIES 4
parry2k
Super User
Super User

@danextian I guess following will do

 

RETURN
    SUMX (
       VALUE ( InvoiceBalance[InvoiceNumber] ),
        IF (
            InvoiceBalance[Currency] = "AUD",
            __INV - __DEDUCTIONS,
            ( __INV - __DEDUCTIONS )
                * CALCULATE (
                    MAX ( FXRate[Rate] ),
                    FILTER (
                        FXRate,
                        FXRate[Month] = MAX ( Dates[Year Month] )
                            && FXRate[Currency] = InvoiceBalance[Currency]
                    )
                )
        )
    )

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hello @parry2k ,

 

Thank you for the reply.  

If i use VALUEs ( InvoiceBalance[InvoiceNumber] ), i won't be able to use any other columns from InvoiceBalance table in my IF argument. They won't show up as available columns when entering the formula.

 

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

@danextian that was just a suggestion, wihtout looking at sample dataset, it si very hard to provide a solution.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi @parry2k ,

Happy new year. A little late but would like to post that I figured out what went wrong. Instead of wrapping __INV - __DEDUCTIONS variable in a SUMX , I created another measure to  do the SUMX. The first formula ended with RETURN __INV - __DEDUCTIONS then the other formula references the first formula 

   SUMX (
        InvoiceBalance,
        IF (
            InvoiceBalance[Currency] = "AUD",
            [First Formula],
            [First Formula]
                * CALCULATE (
                    MAX ( FXRate[Rate] ),
                    FILTER (
                        FXRate,
                        FXRate[Month] = MAX ( Dates[Year Month] )
                            && FXRate[Currency] = InvoiceBalance[Currency]
                    )
                )
        )
    )












Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.