cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Greg_Deckler
Super User
Super User

Dealing with Measure Totals

This one has come up quite a bit recently. The issue surrounds using Measures in Table visualizations with a Total row. The complaint is that the "Total" row is "wrong" for the measure. Technically, the total row is correct for the measure, it's just not what most people expect. What people expect is for the "Total" to display the sum of the values in the column. Measures do not do this. Measures respect the context of the Total row and is calculated within that context. Therefore, a Measure used in a column in a table visualization will likely have an unexpected value in the Total column.

 

There are a couple ways of fixing this. The easiest is to turn off the Total row.

 

Assuming that is not what you want, you can use the HASONEFILTER function to get around this issue. However, the ultimate solution will depend on how your measure is calculated.

 

For example, given the following data:

 

Year Amount

Year1500
Year21500
Year32000
Year4100
Year5800

 

We wish to find the total extra Amount spent above 1000 for each year. If the amount is not over 1000, we wish to display 0. To this end, we create a measure:

 

MyMeasure = IF(SUM(Table[Amount])<1000,0,SUM(Table[Amount])-1000) 

Adding this to a Table visualization along with Year, we get the correct answer for each of the rows, but the Total line displays 3900, not 1500 as we would expect. The figure 3900 is calculated because the Measure is performing its calculation for ALL of the rows in the table, so the calculation is (500 + 1500 + 2000 + 100 + 800) - 1000 = 3900.

 

Correct, but not what was expected.

 

To get around this problem, use HASONEFILTER to calculate the Measure one way within a row context and another way within the Total row context, such as:

 

MyMeasure2 = IF(HASONEFILTER(Table[Year]),
IF(SUM(Table[Amount])<1000,0,SUM(Table[Amount])-1000),
SUMX(FILTER(Table,[Amount]>1000),[Amount]-1000)
)

Breaking this down, we essentially wrap our original measure in an IF statement that has the HASONEFILTER function as the logical test. If HASONEFILTER equals true, we calculate our Measure as before. However, if HASONEFILTER is false, we know that we have a Total row and we calculate our Measure a different way.

 

 

 

 

 

 

 

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...
97 REPLIES 97
Brad_Roberts
Frequent Visitor

Greg,

I'm brand new to Power BI and I'm attempting to apply your solution.  My row dax works fine but the total is not working.  Change SF is a measure of (Sum of All Area - Start SF) and I want to split that between two new measures (Move-In and Move-Out).

 

Brad_Roberts_0-1670441670754.png

Any help is appreciated.

 

Thanks,

Tony

 

 

rmckenna594
New Member

Hi, 

I am having some difficulties with linking two separate data tables with a CALCULATE(SUM) calculated column. I want to bring in the total production cost at item level using the calculated column, but then apply a formula on this to generate the cost per unit in stock.

 

I have tried to do this using a calculated measure but the Total of the Measure does not equate to the sum of all of my table row values.

Urgent help on this would be greatly appreciated 🙂

 

Regards,

Ryan

Patrick95
Frequent Visitor

Hi,

 

I've created a measure to calculate the distinct "Document No." when they have a certain value (FILTER ON: '42-CHECK-SERIALNO'). 

It could be that the same Document No. will have the value (42-...) on a different date. 

 

The totals are corrrect in the matrix, but the subtotals will count the value even if the Document No. has already hit the value (42-CHECK-SERIALNO) already on a different date. Herefor if you count the subtotals, you will get a different total als the one that is listed correctly in the matrix.

 

If the "Document No." occurs several times on different dates by value (42-...), then I do not want to count this (double) in the subtotals. It should only count the first time the "Document No." hits the status (42-...)

 

Patrick95_0-1661518906275.png

 

 

Patrick95_1-1661518906292.png

 

 

I hope someone can help me with this issue

 

Hi,

What is the problem there.  The subtotal shows 34.  Are you expecting a different result there?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish,

 

Thank you for replying! 

 

As you can see, the total is 34 which is correct. If you add up the subtotals (10+17+12+9 = 48), you get a different result. The addition of the subtotals is incorrect The subtotals will count the value even if the same Document No. has the value (42-CHECK-SERIALNO) on a different date.

 

Therefor, I would like to create a measure: If the "Document No." occurs several times on different dates by value (42-...), then I do not want to count this (double) in the subtotals. It should only count the first time the "Document No." hits the status (42-...).

 

Unfortunately, I cannot figure out how to do this.

 

Do you have experience how to do this?

 

Thanks in advance.

 

Patrick

Hi,

Share the link from where i can download your PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
eas
Frequent Visitor

It is shocking that after a number of years Power BI still does not offer an option to show the sum of the above columns as the total. Outside of a table - e.g. KPI - this "solution" breaks. This is disappointing.

100% agree. Ridiculous that a BI tool would behave this way. 

I'm totally agréé with you. In a visual table or others, the total row should have to be correct anyway the formula measure. Like in Excel with pivot table for instance. I would like PBI to correct this point to focus my reflection on the row ans not on the total.

jfsanchez
Frequent Visitor

.

bibee
Regular Visitor

Hello @Greg_Deckler  @Ashish_Mathur @Otto_Luvpuppy , 

 

I am lookin at this thread , to solve a similar problem and I tried the Sumx soln as well has HASONEVALUE but overall sum I am still not able to sum up to expected. Can you please Advise what wrong with the my formula . 

Source Data(Calculation in Excel)  :

DistriEAN Local Actual ForecastMAEWeightMAPE x WeightWMAPE
BD4740011550719.2517.564429%3.1779%0.28%16%
BD47400179240116.0995122.7436%19.1665%1.10% 
BD4740017934934.44125.1486327%5.6858%1.53% 
BD30142600078362.00164.536926127%0.3304%0.42% 
BD30142600078674.39245.48000125%0.7251%0.18% 
BD30142607810331.33445.009387275%0.2203%0.61% 
BD490243004423332.4667230.182357%5.3598%0.38% 
BD490243004425726.000317.2386234%4.2923%1.45% 
BD4902430102247227.6804247.69159%37.5871%3.30% 
BD4902430102254100.3049128.697228%16.5591%4.69% 
BD490243011865116.1025112.5100522%2.6583%0.59% 
BD490243018893713.6665320.0886947%2.2562%1.06% 
BD4902430188951128.7106727%1.9810%0.54% 
      16% 

 

Ouput in PowerBI :

bibee_0-1660592847603.png

 

Using SUMX:

MAPE * Weight =
VAR _MAE = CALCULATE(IF(temp_ean[Measure_Actual]=0,1,ABS(temp_ean[Measure_Actual]-temp_ean[Measure_Forecast])/temp_ean[Measure_Actual]))
VAR _WEIGHT = DIVIDE(temp_ean[Measure_Actual] , temp_ean[TotalActual])
RETURN

SUMX(VALUES(temp_ean[Distro]) , _MAE * _WEIGHT)

Using HASONEVALUE:
Measure1 =
VAR __table = SUMMARIZE(temp_ean,temp_ean[Distro],"__value",temp_ean[MAPE * Weight])
RETURN
IF (
    HASONEVALUE(temp_ean[Distro]),
    temp_ean[MAPE * Weight],
    SUMX(__table,[__value])
    )

Any help is appreciated!

Thanks

Hi,

It is very difficult to help you like this.  Describe the question and share the download link of your PBI file.  Also share the download link of your MS Excel file with your formulas there so that your logic can be understood and translated into the DAX language.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Buen dia, me considero nuevo en esto, 

si me puede ayudar por favor, tengo la siguiente tabla

jfsanchez_0-1660785094150.png

los archivos estan relacionados entre si.

1.- el MON. PPTO lo obtengo de un archivo, de la columna de totales

2.- el MON. REAL lo obtengo de otro archivo, de la columna de totales

3.- la diferencia la obtengo de la resta entre estas dos columnas de diferentes archivos usando esta medida -- 

DIFERENCIA = SUM('PRESUPUESTO DIRECTO'[MON_TOTAL]) - SUM('COSTO DIRECTO'[MON_TOTAL])
(si se pudiera una solucion desde esta medida para que me sume solo los positivos seria excelente)
 
el problema es que quiero obtener una columna mas con la suma unicamente de los valores positivos, que es el dato de la columna sin color en la imagen, el resultado de la medida es el correcto pero no el esperado por mi persona, ya que la medida esta haciendo su funcion lineal. 
esta es la medida usada
PRESUPUESTO POR EJECUTAR = if(SUMX('PRESUPUESTO DIRECTO','PRESUPUESTO DIRECTO'[MON_TOTAL]) > sum('COSTO DIRECTO'[MON_TOTAL]),SUMX('PRESUPUESTO DIRECTO','PRESUPUESTO DIRECTO'[MON_TOTAL]) - SUM('COSTO DIRECTO'[MON_TOTAL]))
 
ME PODRIA AYUDAR DANDOME UNA SOLUCION PARA QUE EL TOTAL ME SUME UNICAMENTE LOS VALORES POSITIVOS, YA REALICE VARIAS MEDIDAS PERO NINGUNA ME DA EL RESULTADO ESPERADO,  ESPERO DARME A ENTERDER
debasnhu
Frequent Visitor

@Greg_Deckler  Hasonevalue is working if we are dealing with Existing Colomn I have tried on top of my measure its not working as x function we can not use in case of masure.

My problam is:-

1) we have two masure in table visualization with total like masure A & B

2) requirement is if (A-B<0,0,A-B), so i need to takeout the all +ve value from A-B(and assign -ve and 0 as 0), Then the total should be there,I am able to achive The above but Total is wrong (when i am adding all values indivually total is different which is actual )

using following masure----

masure C= A-B, Total = SUMX(FILTER(ALLSELECTED(Table[column]),[C]>0),[C]) note-: Working wrong at Total level

here Table[column] is ref. coloum for row filter... Thanks 

it works for me after doing some R&D.

measure C = meaasure A- Measure B.

+ve total = Calculate([C],FILTER(SUMMARIZE(FACTTABLE,PARENTDIMTABLE[COLUMN],FACTTABLE[COLUMN]),[C]>0))

zahirr
Regular Visitor

 

I want to calculate this 

zahirr_0-1650980093658.png

 

And created the following measure - 

But says somewhere is wrong, but can't find it 😞 

 

 

IF(SUM ( 'MasterTbl'[Sett] ) = 1,

                                SUMX (
                (IF (
            SUM ( 'MasterTbl'[Sett] ) = 0,
            CALCULATE (
                SUM ( 'MasterTbl'[Total number of beneficiaries] ),
                ALLEXCEPT ( MasterTbl, 'MasterTbl'[Activities and Indicators.Activities] )
            ) )),

                 IF

            (
                VAR _categoryfirst =
                    CALCULATETABLE (
                        ADDCOLUMNS (
                            VALUES ( 'MasterTbl'[List of refugee settlements-Iran.settlements name] ),
                            "MaxValue",
                                CALCULATE (
                                    MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                                    ALL ( 'MasterTbl'[Total number of beneficiaries] )
                                )
                        ),
                        ALL ( 'MasterTbl'[Total number of beneficiaries] )
                    )
                RETURN
                    CALCULATE (
                        MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                        KEEPFILTERS (
                            TREATAS (
                                _categoryfirst,
                                'MasterTbl'[List of refugee settlements-Iran.settlements name],
                                'MasterTbl'[Total number of beneficiaries]
                            )) )) >= SUM('Settlement name'[population]), SUM('Settlement name'[population])),
            (
                VAR _categoryfirst =
                    CALCULATETABLE (
                        ADDCOLUMNS (
                            VALUES ( 'MasterTbl'[List of refugee settlements-Iran.settlements name] ),
                            "MaxValue",
                                CALCULATE (
                                    MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                                    ALL ( 'MasterTbl'[Total number of beneficiaries] )
                                )
                        ),
                        ALL ( 'MasterTbl'[Total number of beneficiaries] )
                    )
                RETURN
                    CALCULATE (
                        MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                        KEEPFILTERS (
                            TREATAS (
                                _categoryfirst,
                                'MasterTbl'[List of refugee settlements-Iran.settlements name],
                                'MasterTbl'[Total number of beneficiaries]
                            ) ) ) ) ),

                            (   IF (
            SUM ( 'MasterTbl'[Sett] ) = 0,
            CALCULATE (
                SUM ( 'MasterTbl'[Total number of beneficiaries] ),
                ALLEXCEPT ( MasterTbl, 'MasterTbl'[Activities and Indicators.Activities] )
            ),
    IF
        (
            (
                VAR _categoryfirst =
                    CALCULATETABLE (
                        ADDCOLUMNS (
                            VALUES ( 'MasterTbl'[List of refugee settlements-Iran.settlements name] ),
                            "MaxValue",
                                CALCULATE (
                                    MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                                    ALL ( 'MasterTbl'[Total number of beneficiaries] )
                                )
                        ),
                        ALL ( 'MasterTbl'[Total number of beneficiaries] )
                    )
                RETURN
                    CALCULATE (
                        MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                        KEEPFILTERS (
                            TREATAS (
                                _categoryfirst,
                                'MasterTbl'[List of refugee settlements-Iran.settlements name],
                                'MasterTbl'[Total number of beneficiaries]
                            )
                        )
                    )
            ) >= SUM('Settlement name'[population]), SUM('Settlement name'[population]),
            (
                VAR _categoryfirst =
                    CALCULATETABLE (
                        ADDCOLUMNS (
                            VALUES ( 'MasterTbl'[List of refugee settlements-Iran.settlements name] ),
                            "MaxValue",
                                CALCULATE (
                                    MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                                    ALL ( 'MasterTbl'[Total number of beneficiaries] )
                                )
                        ),
                        ALL ( 'MasterTbl'[Total number of beneficiaries] )
                    )
                RETURN
                    CALCULATE (
                        MAX ( 'MasterTbl'[Total number of beneficiaries] ),
                        KEEPFILTERS (
                            TREATAS (
                                _categoryfirst,
                                'MasterTbl'[List of refugee settlements-Iran.settlements name],
                                'MasterTbl'[Total number of beneficiaries]
                            ) )  ) ) )  )))

Otto_Luvpuppy
Helper II
Helper II

Hi Greg,
I realise this is an old post but it seems to still be, very much, relevent as I know loads of people who struggle with this concept.

I've tried to replicate the method you use for my own problem and kind of get half way but then hit the gravel.
Is there any chance you could apply your process to the issue I posted?
https://community.powerbi.com/t5/Desktop/Totals-Calculations-using-a-a-real-problem/m-p/2250407

Thanks

Paul

Michella
Frequent Visitor

Hi everyone, 

 

I'm also having a problem with column totals with the following measure:

 

Zusatzumsatz = 
 
VAR Zielgruppe = 
    CALCULATE(
    [Umsatz pro Käufer],
    FILTER('Table1''Table1'[CONTROLGROUP] = "Zielgruppe")
    )

VAR Datum =
    MAX(Table1[SENDDATE])

VAR ProgrammID = 
    MIN(Table1[Program ID])

VAR Kontrollgruppe =
    CALCULATE(
    [Umsatz pro Käufer],
    Table2[PROGRAMID] = ProgrammID,
    Table1[SENDDATE] = Datum,
    Table1[CONTROLGROUP] = "Kontrollgruppe",
    ALL()
    )

VAR IstZielgruppe =
    MAX(Table1[CONTROLGROUP])="Zielgruppe"

VAR Auflage_Final = 
    CALCULATE( SUM(Table1[AUFLAGE]), KEEPFILTERS(Table1[CONTROLGROUP]="Zielgruppe"))

RETURN
    IF(IstZielgruppe,
    (Zielgruppe - Kontrollgruppe)* Auflage_Final
    )

Michella_0-1637922501035.png

As you can see - the column total is wring. I understand that the measure is calculating in row and not column context. Is there a way that the calculation in the value part of the table is evaluated per row but the total is just the sum of the column?

Hi,

Drag this measure to your visual

Measure = SUMX(VALUES(Data[kampagnenname]),[Zusatzumsatz])

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
DanH2000
Frequent Visitor

Good Morning.

 

I'm also having a similar problem in that the total for the measure i've created isn't adding up 

 

DanH2000_0-1634812496359.png

I have tried adding the "IsFiltered" and "SUMX"  functions within my measurement however so far haven't had success (the measurement does contain switch statements too) can you possibly advise? The DAX I'm using for the original measurement is below,

Measure = IF(MAX('Core Data'[Change Impact Size (Major/Medium/Minor/Cosmetic])="Major",
IF(COUNT('Core Data'[Change Impact Size (Major/Medium/Minor/Cosmetic])>1,13,
SWITCH(
DATEDIFF(max('Core Data'[Go Live Date]),max('Date Table'[Date]),WEEK)
, -12,1,-11,2,-10,3,-9,4,-8,5,-7,6,-6,8,-5,10,-4,10,-3,10,-2,11,-1,12,0,12,1,12,2,12,3,12,4,12,5,11,6,10,7,9,8,8,9,7,10,6,11,4,12,2)
)
,
IF(max('Core Data'[Change Impact Size (Major/Medium/Minor/Cosmetic])="Medium",
IF(COUNT('Core Data'[Change Impact Size (Major/Medium/Minor/Cosmetic])>1,13,
SWITCH(
DATEDIFF(max('Core Data'[Go Live Date]),max('Date Table'[Date]),WEEK)
, -12,0,-11,1,-10,2,-9,3,-8,4,-7,5,-6,7,-5,8,-4,8,-3,8,-2,9,-1,10,0,10,1,10,2,10,3,10,4,9,5,9,6,8,7,7,8,6,9,5,10,4,11,2,12,1))
,

IF(max('Core Data'[Change Impact Size (Major/Medium/Minor/Cosmetic])="Minor",
IF(COUNT('Core Data'[Change Impact Size (Major/Medium/Minor/Cosmetic])>1,13,
SWITCH(
DATEDIFF(max('Core Data'[Go Live Date]),max('Date Table'[Date]),WEEK)
, -12,0,-11,0,-10,0,-9,1,-8,2,-7,3,-6,4,-5,5,-4,6,-3,7,-2,7,-1,8,0,8,1,8,2,8,3,7,4,6,5,6,6,5,7,4,8,3,9,2,10,1,11,0,12,0))

)))

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

Thank you 2022 Review

2022 Monthly Feature Releases

We had a great 2022 with a ton of feature releases to help you drive a data culture.

Top Solution Authors
Top Kudoed Authors