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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
NMOORE
Helper II
Helper II

incorrect Total Selected Value alternative

Hi,

I need to sum quanities differently depending on my column values. The working example is much more complex with info I cannot share so I have drafted up a basic example using the Table below.

 

In the measure, I simply Sum up the quantities, but where the type is a Donkey I add the quanitites from Cow's (VAR _A).

 

Table

  Type      Quantity

Donkey5
Donkey5
Duck3
Duck5
Dog4
Dog5
Cow3
Cow5

 

Example Measure
VAR _A =
    CALCULATE(
        SUM('Example Table'[Quantity]),
        FILTER(
            ALL('Example Table'),
            'Example Table'[Type] = "Cow")
    )
 
VAR _B =
    SUM('Example Table'[Quantity])
 
RETURN
    IF(
        SELECTEDVALUE('Example Table'[Type]) = "Donkey",
        _A + _B,
        _B
    )
 
 
 Table Visual
 NMOORE_2-1706095146410.png

 

Everything works how I want it to except the total is incorrect as I am using selected value (I underdstand why). Can anyone please assist? I cannot use a calculated column in the real example.

 

Thanks for any help.

 

1 ACCEPTED SOLUTION

Hi, 

so, leave your first calculation like this

Example = VAR _A =
    CALCULATE(
        SUM('Example Table'[Quantity]),
        FILTER(
            ALL('Example Table'),
            'Example Table'[Type] = "Cow")
    )
 
VAR _B =
    SUM('Example Table'[Quantity])


    RETURN
 IF(
        SELECTEDVALUE('Example Table'[Type]) = "Dog" ||
        SELECTEDVALUE('Example Table'[Type]) = "Donkey",
        _B / _A,
        0
    )

Add one more measure:
Example2 =

SUMX(SUMMARIZE('Example Table', 'Example Table'[Type], "Example Correct", 'Example Table'[Example]), [Example Correct])

olgad_0-1706649870793.png

 

 

DID I ANSWER YOUR QUESTION? PLEASE MARK MY POST AS A SOLUTION! APPRECIATE YOUR KUDO/LIKE!
PROUD TO BE A SUPER USER!
Best Stories, Interesting Cases: PowerBI Storytime Newsletter
Linkedin Profile: Linkedin
YouTube Channel: PowerBI Storytime

View solution in original post

8 REPLIES 8
olgad
Super User
Super User

Hi, 
I dont know if it is gonna fit  your real example, 
but here it is 

Example = VAR _A =
    CALCULATE(
        SUM('Example Table'[Quantity]),
        FILTER(
            ALL('Example Table'),
            'Example Table'[Type] = "Cow")
    )
 
VAR _B =
    SUM('Example Table'[Quantity])
 
RETURN
    If(HASONEVALUE('Example Table'[Type]), IF(
        SELECTEDVALUE('Example Table'[Type]) = "Donkey",
        _A + _B,
        _B
    ),_B+_A
 )

olgad_0-1706102533346.png

 


DID I ANSWER YOUR QUESTION? PLEASE MARK MY POST AS A SOLUTION! APPRECIATE YOUR KUDO/LIKE!
PROUD TO BE A SUPER USER!
Best Stories, Interesting Cases: PowerBI Storytime Newsletter
Linkedin Profile: Linkedin
YouTube Channel: PowerBI Storytime

Hi Olgad,

Thanks again for the above. It solves the issue listed but I didnt distill the complexity of my issue into the example very well.

 

I'm hoping this might be more in line with my issue. If we use the same measure but using your method of HASONEVALUE there are more cases which need to be involved.

 

Because I need to Filter ALL on some tables I cannot then make it sum properly. I've been trying to create seperate measures and Sum them just in a total but the SelectedValue always follows.

 
    RETURN

    IF(
        HASONEVALUE('Example Table'[Type]),
    IF(
        SELECTEDVALUE('Example Table'[Type]) = "Dog" ||
        SELECTEDVALUE('Example Table'[Type]) = "Donkey",
        _B / _A,
        0
    ),_B / _A
    )
 
NMOORE_0-1706644122313.png

I hope I'm missing something obvious. Let me know if you get the chance?

 

Thanks

Hi, 

so, leave your first calculation like this

Example = VAR _A =
    CALCULATE(
        SUM('Example Table'[Quantity]),
        FILTER(
            ALL('Example Table'),
            'Example Table'[Type] = "Cow")
    )
 
VAR _B =
    SUM('Example Table'[Quantity])


    RETURN
 IF(
        SELECTEDVALUE('Example Table'[Type]) = "Dog" ||
        SELECTEDVALUE('Example Table'[Type]) = "Donkey",
        _B / _A,
        0
    )

Add one more measure:
Example2 =

SUMX(SUMMARIZE('Example Table', 'Example Table'[Type], "Example Correct", 'Example Table'[Example]), [Example Correct])

olgad_0-1706649870793.png

 

 

DID I ANSWER YOUR QUESTION? PLEASE MARK MY POST AS A SOLUTION! APPRECIATE YOUR KUDO/LIKE!
PROUD TO BE A SUPER USER!
Best Stories, Interesting Cases: PowerBI Storytime Newsletter
Linkedin Profile: Linkedin
YouTube Channel: PowerBI Storytime

Much appreciated thanks. I've used Summarize to create tables in my data models but not wrapped inside SUMX like that so a very useful piece of learning, it worked in the large working model.

 

I tried to add the Example2 code as a RETURN - referencing Example as a variable (so it was all in the same measure) but the total was in the billions? I was looking at a neat way to put it all in one measusure but obviously not essential.

 

Thanks again

lately i had to correct totals a lot, just doest work out within a variable (that was my intention too to just have it in one place), alas, you have to create a second measure.


DID I ANSWER YOUR QUESTION? PLEASE MARK MY POST AS A SOLUTION! APPRECIATE YOUR KUDO/LIKE!
PROUD TO BE A SUPER USER!
Best Stories, Interesting Cases: PowerBI Storytime Newsletter
Linkedin Profile: Linkedin
YouTube Channel: PowerBI Storytime

Hi Olgad,

 

Hope you're well.

 

I wondered if you knew what might be going wrong in a new scenario I have. Have you ever had only the grand totals wrong? I'm using HASONEVALUE to flick between different summarizes and everything is great accept the grand total (see pic). I'm aware this is limited info as I cannot share anything but thought it might be something you're used to seeing. I'm summarizing here against the periods/dates on the left...The grand total in the corner is off by circa £3m...

 

NMOORE_0-1710850944026.png

 

Thanks for any tips

I've had to on some running totals as that's how it was done on some working examples online so I think it is the only way in some scenarios. Cheers

Thanks so much Olgad, just away from my work at the moment I'll give it a whirl on Monday. Cheers

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors