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
Anonymous
Not applicable

Defining a "Winner" from Election results over State and Year

Hello

 

I am new to power BI and a beginner with DAX. Apologies if this post is in the wrong location.

 

I have USA presidential election data from 1976 onwards. Each row shows the state, the year, the candidate, the party, and the amount of votes the candidate recieved.

 

E.g.

Alabama, 1984, Ronald Regan, Republican, 872849,

Alabama, 1984, Mondale Walter, Democrat, 551899

Alabama, 1984, Other candidates, Other, [sum of votes of Other Candidates]

 

I would now like to create a column (or measure..?) showing which party won in each State and in each year (answer for table above is Republican).

 

I suspect this isn't too difficult to do but my own shortcomings are letting me down!

 

Any help is greatly appreciated.

 

1 ACCEPTED SOLUTION
kriscoupe
Solution Supplier
Solution Supplier

Hi @Anonymous,

 

You can create a new measure like this

 

 

Winning Party = 
VAR MaxVotes = MAX('Table'[Number of Votes])
VAR TopCandidate =
CALCULATE(
    SELECTEDVALUE( 'Table'[Party] ),
    'Table'[Number of Votes] = MaxVotes
)
RETURN
IF(
    HASONEVALUE( 'Table'[State] ) && HASONEVALUE( 'Table'[Year] ),
    TopCandidate
)

 

 

Then build a matrix with State and Year on the Axis and you should have your winning party.

 

Let me know if this helps. If this solves your problem, please mark this answer as a solution and drop a kudos 😉.

 

Thanks

Kris

View solution in original post

4 REPLIES 4
harshnathani
Community Champion
Community Champion

Hi @Anonymous ,

 

 

You may want to have a look at this in the Community gallery.

 

https://community.powerbi.com/t5/Data-Stories-Gallery/2016-Presidential-Election-Polls-Analysis/td-p/89664

 

The .pbix file is attached there too.

 

Regards,
Harsh Nathani

Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

kriscoupe
Solution Supplier
Solution Supplier

Hi @Anonymous,

 

You can create a new measure like this

 

 

Winning Party = 
VAR MaxVotes = MAX('Table'[Number of Votes])
VAR TopCandidate =
CALCULATE(
    SELECTEDVALUE( 'Table'[Party] ),
    'Table'[Number of Votes] = MaxVotes
)
RETURN
IF(
    HASONEVALUE( 'Table'[State] ) && HASONEVALUE( 'Table'[Year] ),
    TopCandidate
)

 

 

Then build a matrix with State and Year on the Axis and you should have your winning party.

 

Let me know if this helps. If this solves your problem, please mark this answer as a solution and drop a kudos 😉.

 

Thanks

Kris

Anonymous
Not applicable

Hi @kriscoupe  

 

Thanks for your help - I suspect you have the correct answer but (most likely due to my own lack of expertise) I have still failed to get the answer I wanted. 

 

Here is a snapshot from my table 'Election_Results' 

Staja809_0-1593507694010.png

 

And here is a snapshot of your formula with my variables incorporated. 

 

Staja809_1-1593507912415.png

 

However when I try to input Winning Party into a matrix it shows no result and I can't get a result with any other displays either.

 

Staja809_3-1593508334563.png

 

 

Now at this point you are probably shaking your head in disbelief at my ineptness with Power BI and DAX and tempted to give up on me, which I understand. However, if you see how the problem could be quickly fixed then let me know!

 

Thanks again for all your help! 

 

 

Hi @Anonymous,

 

Ah no sorry it seems I may have typo'd not thinking how your matrix would be set up. So the HASONEVALUE formula should be

 

IF(
    HASONEVALUE( Election_Results[State] ) && HASONEVALUE( Election_Results[Year] ),
    TopCandidate
)

 

Basically the HASONEVALUE makes sure that you have a state/year in the context of the measure. Otherwise if you have subtotals in the matrix you will get party names in the subtotals such as who had the highest voting total for a state in 2014. If you remove the subtotals from the matrix then you can actually RETURN TopCandidate without the IF I believe.

 

Let me know how you get on. I'll amend my original answer to include this new method so the answer is not split 🙂

 

Kris

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.

Top Solution Authors