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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
LoryMenCR
Helper I
Helper I

Measure total won't work

Dear All,

I have the following measure:

 

ValoreDistribuito100 =
var total100 = CALCULATE([Saldo_ANALITICA_ TOTALI CORRETTI], 'db'[CEN_CODICE] = "100")
var currentcode = MAX('db'[CEN_CODICE])
var allocationpercent =
LOOKUPVALUE(
    'CEN_Weight%'[%],
    'CEN_Weight%'[CEN_END], currentcode,
    'CEN_Weight%'[CEN_START], "100"
)

RETURN
total100 * allocationpercent / 100
 
 
Measure is working properly but it returns an incorrect total (it always returns the value of the last row):
LoryMenCR_0-1730970468898.png

 

Do you know how can I fix this?

Thanks a lot

lor

5 REPLIES 5
v-jiewu-msft
Community Support
Community Support

Hi @LoryMenCR ,

Based on the description, try using the following DAX formula.

ValoreDistribuito100 =
SUMX(
    'db',
    var total100 = CALCULATE([Saldo_ANALITICA_ TOTALI CORRETTI], 'db'[CEN_CODICE] = "100")
    var currentcode = 'db'[CEN_CODICE]
    var allocationpercent =
    LOOKUPVALUE(
        'CEN_Weight%'[%],
        'CEN_Weight%'[CEN_END], currentcode,
        'CEN_Weight%'[CEN_START], "100"
    )
    RETURN
    total100 * allocationpercent / 100
)

Besides, I want to know if having relationship between two tables. Is this [Saldo_ANALITICA_ TOTALI CORRETTI] a column or a metric? Can you provide the sample data?

 

Best Regards,

Wisdom Wu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

LoryMenCR
Helper I
Helper I

Not working 😞

SolomonovAnton
Solution Supplier
Solution Supplier

The issue with your measure returning an incorrect total is likely due to context evaluation. In Power BI, row-level calculations can sometimes lead to unexpected totals because the total row doesn’t have the same row context as individual rows. Instead, you need to adjust the measure to calculate totals differently from individual row calculations.

You can fix this by wrapping your calculation in an IF statement that checks if it’s being evaluated in a row context or a total context. The HASONEVALUE function is useful here, as it returns TRUE if only one value is in the context (i.e., a row context), and FALSE for the total context. Here’s how to modify your measure:

ValoreDistribuito100 =
VAR total100 = CALCULATE([Saldo_ANALITICA_ TOTALI CORRETTI], 'db'[CEN_CODICE] = "100")
VAR allocationpercent =
SUMX(
'CEN_Weight%',
IF(
'CEN_Weight%'[CEN_START] = "100" && 'CEN_Weight%'[CEN_END] = MAX('db'[CEN_CODICE]),
'CEN_Weight%'[%],
0
)
)

RETURN
IF(
HASONEVALUE('db'[CEN_CODICE]),
total100 * allocationpercent / 100,
SUMX(
ALL('db'[CEN_CODICE]),
total100 * allocationpercent / 100
)
)



If my message helped solve your issue, please mark it as Resolved! If it was helpful, consider giving it a Kudos!

Dear @SolomonovAnton , thank you for your super fast answer.

I fear I cannot use the HASONEVALUE here.

My 'CEN_Weight%' table is as follows: 

LoryMenCR_0-1730972065372.png

 

Do you think I can use some other solution?

 

 

 

if you need create calculation column tru to use this code:
ValoreDistribuito100_Column =
VAR total100 = CALCULATE([Saldo_ANALITICA_ TOTALI CORRETTI], 'db'[CEN_CODICE] = "100")
VAR currentcode = 'db'[CEN_CODICE]
VAR allocationpercent =
LOOKUPVALUE(
'CEN_Weight%'[%],
'CEN_Weight%'[CEN_END], currentcode,
'CEN_Weight%'[CEN_START], "100"
)
RETURN
IF(NOT(ISBLANK(allocationpercent)), total100 * allocationpercent / 100, BLANK())

If my message helped solve your issue, please mark it as Resolved! If it was helpful, consider giving it a Kudos!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.