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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Laura1996
Helper I
Helper I

wrong measure total including if statement

I have a table "Komm", which looks like following. Right now my Total of "Factor final" is wrong and I cant wrap my head arount how to do it correctly:

 

NameProductDescriptionAmountUnitFactor (comes from another table called "Factors")Factor final
TomAApple2ST36
JackBRope30M66
JackCApple5ST315
ColeDPumpkin8KG44

 

Basically what I want is, that if in my visual I click on the name of the Person, for example Jack he sums up 6 + 15 = 21. So the result is 21. When Jack is not selected it should show the whole sum (over all lines). Additionally to that I have different calculations depending on the Unit, so for "M", "GA", "L" and "KG" he should calculate the Factor * 1, for all the other units the Factor should be multiplied with the Amount.

What I tried is following:


Faktor final =

VAR collecting = SELECTEDVALUE(Komm[Unit])

RETURN

SWITCH(TRUE(),

collecting= "M" || collecting="GA" || collecting="L" || collecting="KG",

SUMX('Factors', 'Factors'[Factor]*1),
SUMX(Komm, Komm[Amount]* RELATED('Factors'[Factor])))


Hope somebody can help me 🙂


1 ACCEPTED SOLUTION
v-jiewu-msft
Community Support
Community Support

Hi @Laura1996 ,

Based on the description, try to use the following DAX formula.

Faktor final = 
VAR IsSpecificUnit = 
    SUMX(
        Komm, 
        IF(
            Komm[Unit] IN {"M", "GA", "L", "KG"}, 
            RELATED('Factor'[Factor]), 
            Komm[Amount] * RELATED('Factor'[Factor])
        )
    )
RETURN 
    IF (
        HASONEVALUE(Komm[Name]),
        IsSpecificUnit,
        SUMX(
            Komm,
            IF (
                Komm[Unit] IN {"M", "GA", "L", "KG"},
                RELATED('Factor'[Factor]),
                Komm[Amount] * RELATED('Factor'[Factor])
            )
        )
    )

Then, select the Jack name.

vjiewumsft_0-1726042416771.png

vjiewumsft_1-1726042462091.png

 

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.

View solution in original post

22 REPLIES 22
Nonessential
Frequent Visitor

Hi, try this @Laura1996  - Data is the table name so replace with your table name (KOMM) and I have called the Measure 'New Factor' instead of 'Final Factor';

Nonessential_0-1727103906627.png

 

New Factor =
VAR _Tbl =
ADDCOLUMNS(
SELECTCOLUMNS(Data,Data[Name],Data[Description],Data[Amount],Data[Factor (comes from another table called "Factors")],Data[Unit]),
"Final Factor", IF(
    Data[Unit] IN {"M", "GA", "L","KG"},
    CALCULATE(SUM(Data[Factor (comes from another table called "Factors")])),
    CALCULATE(SUMX(Data,Data[Amount]*Data[Factor (comes from another table called "Factors")]))
))

RETURN SUMX(_Tbl,[Final Factor])

hey @Nonessential 🙂

thank you for your try, unfortunately, The Factor is not in the same table, so the code is not working. I marked the problem in red, tried i with the relate function or to summarize, however don't know how to make it work:

New Factor =
VAR _Tbl =
ADDCOLUMNS(
SELECTCOLUMNS(Data,Data[Name],Data[Description],Data[Amount],Data[Factor (comes from another table called "Factors")],Data[Unit]),
"Final Factor"IF(
    Data[Unit] IN {"M""GA""L","KG"},
    CALCULATE(SUM(Data[Factor (comes from another table called "Factors")])),
    CALCULATE(SUMX(Data,Data[Amount]*Data[Factor (comes from another table called "Factors")]))
))

 

RETURN SUMX(_Tbl,[Final Factor])

Hi @Laura1996 , Could you bring the Factor from your Factors table into your KOMM table using a calculated column? Something like; 

LOOKUPVALUE(
        'Factors'[Factor],
        'KOMM'[Description],
        'Factors'[Description])
This assumes that there are 2 matching/correlating columns, one in KOMM and another in Factors - in the example above the matching columns are Description but you could swap these out if necessary.
v-jiewu-msft
Community Support
Community Support

Hi @Laura1996 ,

Based on the description, try to use the following DAX formula.

Faktor final = 
VAR IsSpecificUnit = 
    SUMX(
        Komm, 
        IF(
            Komm[Unit] IN {"M", "GA", "L", "KG"}, 
            RELATED('Factor'[Factor]), 
            Komm[Amount] * RELATED('Factor'[Factor])
        )
    )
RETURN 
    IF (
        HASONEVALUE(Komm[Name]),
        IsSpecificUnit,
        SUMX(
            Komm,
            IF (
                Komm[Unit] IN {"M", "GA", "L", "KG"},
                RELATED('Factor'[Factor]),
                Komm[Amount] * RELATED('Factor'[Factor])
            )
        )
    )

Then, select the Jack name.

vjiewumsft_0-1726042416771.png

vjiewumsft_1-1726042462091.png

 

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.

Hey, thank you for the answer.

Unfortunately something with the calculation still doesn't go right:

Here some results:

Laura1996_1-1726552746895.png

 

The strange thing is that sometimes the calculation is correct and sometimes not. I have no other sums in my visual...so I don't know what it could be... The sum here should be 53,50 considering that I added also M2 into your code... any ideas what it could be?

Hi @Laura1996 ,
I would like to know how 53.5 is calculated? Which row values are added together to get the result?

What's more, try to check if any other filters are being applied.

 

Best Regards,

Wisdom Wu

5 M * 10 should equal 10, because thats what the formula says. But it equals 20. The same thing with 6 M. 

Because I have to be able to filter also the month / week / day of a persons collected products this triggers the mistake - at least this is what I think. Because when I remove the filter its correct. I never had this problem in any other reports. Is there any way to also include month / week / day into the calculation?

here a screenshot of the date:

Laura1996_0-1726836194085.png

the week number I extracted by myself, so I can use it in a calculation. Do I have to do the same stuff for day/month, and how would the code look like, because HASONEVALUE() can work just with one value as far as Im concerned.

Thank you very much





Hi @Laura1996 ,

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

Faktor final = 
VAR IsSpecificUnit = 
    SUMX(
        Komm, 
        IF(
            Komm[Unit] IN {"M", "GA", "L", "KG"}, 
            RELATED('Factor'[Factor]), 
            Komm[Amount] * RELATED('Factor'[Factor])
        )
    )
RETURN 
    IF (
        HASONEVALUE(Komm[Name]),
        CALCULATE(
        SUMX(Komm, Komm[Amount] * RELATED('Factors'[Factor])),
        FILTER(
            Komm,
            Komm[Name] = SELECTEDVALUE(Komm[Name]) &&
            Komm[Month] = SELECTEDVALUE(Komm[Month]) &&
            Komm[Week] = SELECTEDVALUE(Komm[Week]) &&
            Komm[Day] = SELECTEDVALUE(Komm[Day])
        )
    ), IsSpecificUnit

    )

 

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.

hi @v-jiewu-msft 🙂

well, now its even worse 😄 this is the result:

Laura1996_1-1727082485403.png

so now its 2.818,00 instead of 53,50 and it kind of ignores the rule for "M, M2, KG,.."

this report makes me crazy 



Hi @Laura1996 ,

I forget the rule for M, M2. Please try using the following DAX formula.

Faktor final = 
VAR IsSpecificUnit = 
    SUMX(
        Komm, 
        IF(
            Komm[Unit] IN {"M", "GA", "L", "KG"}, 
            RELATED('Factor'[Factor]), 
            Komm[Amount] * RELATED('Factor'[Factor])
        )
    )
RETURN 
    IF (
        HASONEVALUE(Komm[Name]),
        IF(
            Komm[Unit] IN {"M", "GA", "L", "KG"}, 
            RELATED('Factor'[Factor]), 
            CALCULATE(
                SUMX(Komm, Komm[Amount] * RELATED('Factors'[Factor])),
                FILTER(
                Komm,
                Komm[Name] = SELECTEDVALUE(Komm[Name]) &&
                Komm[Month] = SELECTEDVALUE(Komm[Month]) &&
                Komm[Week] = SELECTEDVALUE(Komm[Week]) &&
                Komm[Day] = SELECTEDVALUE(Komm[Day])
            )
        ), 
        IsSpecificUnit
    )

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.

hi @v-jiewu-msft 🙂

unfortunately now dax is telling me that I have too many arguments and that there can be a max. of 3:

it specifically underlines (in the paragraph after the RETURN):

Komm[Unit] IN {"M", "GA", "L", "KG"}, 
            RELATED('Factor'[Factor]), 

&

 IsSpecificUnit

also he's not finding me the variable "Factor" or "Unit" in that if statement
wish I could finally mark this as resolved...but its giving me a headache 😄

Hi @Laura1996 ,

Is your date data column inside the komm table? How are you filtering the dates? slicer or Filters pane.

Try using the following DAX formula.

 

Faktor final = 
SUMX(
    Komm,
    SWITCH(
        TRUE(),
        Komm[Unit] IN {"M", "GA", "L", "KG"}, Komm[Factor] * 1,
        Komm[Amount] * RELATED('Factor'[Factor])
    )
)

 

 

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.

hey @v-jiewu-msft ,

in the end, your first code was the solution 🙂 the problem of the wrong calculation was another column, which I didn't use in the report. It was like an invoice number. 

So let's say there are invoices from July, August and September.
In July John bought 2 Apples, in August 3 and in September 1.
What PowerBI did was: show 1 Apple for September but count 1 times 6, took this as the "amount" and multiplied it by the factor. Why 6? Well it took me some time to figure that out. Because the amount of all the Apples over all receipts is 6.

anyway I fixed this and now your solution works like a charm 🙂 just wanted to post the solution here, because someone might stumble over the same problem. Watch your columns guys !! also if you're not using them in a specific report 🙂

Thank you so much for the help

Laura1996
Helper I
Helper I

@Irwan Anyway - sorry for the confusion 🙂

@Laura1996 

 

No worries, i thought i misunderstood your requirement.

Irwan
Super User
Super User

hello @Laura1996 

 

please check if this accomodate your need.

Irwan_0-1725860417705.png

Irwan_1-1725860432289.png

 

create a new measure with following DAX.

Measure = 
var _Name = SELECTEDVALUE('Table'[Name])
Return
IF(
    HASONEVALUE('Table'[Product]),
    SUMX(
        ALLSELECTED('Table'),
        IF(
            'Table'[Unit]="ST",
            'Table'[Amount]*'Table'[Factor (comes from another table called "Factors")],
            'Table'[Factor (comes from another table called "Factors")]
        )
    ),
    SUMX(
        FILTER(
            ALLSELECTED('Table'),
            'Table'[Name]=_Name
        ),
        IF(
            'Table'[Unit]="ST",
            'Table'[Amount]*'Table'[Factor (comes from another table called "Factors")],
            'Table'[Factor (comes from another table called "Factors")]
        )
    )
)
 
Hope this will help.
Thank you.

hello @Irwan 🙂

Unfortunately it doesn't do what it should...as the field "Factor" comes from another table and this table is not mentioned in "ALLSELECTED" he cant find the field. Also ALLSELECTED allows just one argument.

I tried to solve it over related, but its making strange calculations. With my measure they were right and just the total was wrong for the rows selected, now he calculates 6* 0,88 = 3.709,60 which makes no sense. In fact the result is 3709,60 for all the rows.

Also in your Screenshot he calculates 6+3 as 21, when it should be 9. And when you dont select anything he calculates it as 31, which is also not correct...
it actually should be a super simple measure but it became lemon difficult 😄 thank you anyway for trying, hope someone can provide me a working solution

Also I need to focus on this "collecting= "M" || collecting="GA" || collecting="L" || collecting="KG"

the measure shouldn't work with "ST", because there are many more units than just those mentioned up. So when I make an if statement, I have to define which to "exclude" rather than which to consider.

Wait, you said in your first post, Jack has value of 21 which is 6+15. 

I thought 6 (2*3 because ST) and 15 (5*3 because ST), no?

 

What value for Jack, 9 or 21?

 

And when not selected, 6 (2*3 ST) + 15 (5*3 ST) + 6 (6*1 M) + 4 (4*1 KG) = 31

 

Also those M, GA, L, KG in if statement, as far as i can tell, this only for differentiate which will be multiplied and not. I only put ST for multiplying and you can add the rest.

Yes sorry, that was my mistake now because I didn't seen the number 15 in your table. 21 should be correct. Also 31 is correct, however its not working for my data, because my result is always 3709,60. And also in your code I can't add the other table for the Factor (Table "Factor").

Actually trying to add the rest is not working or at least I didn't still figure out how to make it work...

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!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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