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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Need To Calculate A Total For A Column That Is Based On Multiple Rows In The Database Table

EDIT - Corrected Subject (sorry about that)

Hi folks,

I have a table of data that isn't cooperating. I can get some of my calculations to work (eg To be purchase in year), but not all. It may be a data modelling issue, but this is the one calculation that I haven't been able to fix.

I have a series of producers who sell product to us. From those Producers, we may get a number for the acres on which they produce a particular commodity (sometimes that data isn't provided to us). We can make multiple contracts with these producers.

I will bring up an example table of data based on a customer:

 

 

IDYearCommodityAcres SeededPotential ProductionTotal Contract QuantityTo be Purchased in yearContract Count
123456782015Durum  190.507-190.5071
123456782015Wheat  122.469-122.4691
123456782016Canola650599.258263.650335.6083
123456782016Durum450510.08780.821429.2661
123456782016Wheat8501074.538703.783370.7553
123456782017Canola  731.159-731.1597
123456782017Wheat  1135.037-1135.0379
123456782018Canola  229.848-229.8486
123456782018Wheat  758.472-758.4725
123456782019Canola800737.54979.412658.1371
123456782019Peas009.691-9.6911
123456782019Wheat13001643.411114.8831528.5282

 

The issue is that I want to get a total of the Acres Seeded, but if I do a SUMX, I get 8,350 because it multiplies the rows by the number of contracts before giving me the summed result.

 

If I hit the 2019 slicer, it gives me 3,400 acres instead of the desired 2,100 acres. The 2016 slicer gives me 4,950 acres instead of 1,950.

 

I've tried variations of dividing with no success. This is probably (and hopefully) something really simple to fix. It's been very frustrating because I got my To be purchased in year working:

 

To Be Purchased In Year =

    CALCULATE(
        DIVIDE(
            SUMX('Agrosoft With Seeded Acres','Agrosoft With Seeded Acres'[Potential Production]),
            [Contract Count]
        )       
    )
     -    
    CALCULATE(
        SUMX('Agrosoft With Seeded Acres','Agrosoft With Seeded Acres'[Total Contract Quantity])
    )


Can anybody help me? I removed some other data. Hope this makes sense.

Thank you!

1 ACCEPTED SOLUTION

Hello @Anonymous 

This measure is giving me the results I expected

Seeded Acres = 
SUMX ( SUMMARIZE('Table','Table'[ID],'Table'[Year],'Table'[Grain]), CALCULATE( MAX ( 'Table'[Acres Seeded] ) ) )

I expanded your sample with another year and another contract to test:

SeededAcres.jpg

I have also attached my sample .pbix for you to look at.

 

View solution in original post

6 REPLIES 6
jdbuchanan71
Super User
Super User

@Anonymous 

Does a straight SUM(YourTable[Acres Seeded]) not return the correct amount?  Not sure why you are using SUMX, what am I missing?

Anonymous
Not applicable

No, a simple SUM doesn't work. It gives the same result.

 

This data is based on filtering out over 64,000 lines of contracts - each contract line may have a seeded acres value based on another table's data that list seeded acres by producer, year and commodity.

 

The actual background table has multiple lines that make up the total contract tonnage (thus the contract count column). That background table is based on the original contracts table combined with a seeded acres table (that table has Producer ID, Year, Commodity and Acres Seeded).

The Contract Table is like this (stripped down to relevant columns):

 

IDYearGrainContract QuantityAcres Seeded
123456782018Canola100500
123456782018Canola50500
123456782018Wheat1501000
123456782018Wheat2501000

 

As you can see, the total contract quantity for Canola is 150 tonnes, but the seeded acres should only be 500. Wheat has 400 tonnes total but only 1,000 seeded acres. Thus, summing doesn't work because it sums the entire column when I only need one value per producer per year per commodity.

 

I did it this way to correct another issue, but it now causes issues on another tab.

 

Thank you!

Hello @Anonymous 

This measure is giving me the results I expected

Seeded Acres = 
SUMX ( SUMMARIZE('Table','Table'[ID],'Table'[Year],'Table'[Grain]), CALCULATE( MAX ( 'Table'[Acres Seeded] ) ) )

I expanded your sample with another year and another contract to test:

SeededAcres.jpg

I have also attached my sample .pbix for you to look at.

 

Anonymous
Not applicable

@jdbuchanan71 That's awesome, thank you!

 

Would the below also work? - I figured this out a short time before you posted your solution. It seems to give me what I need.

Your solution is a bit more elegant and I can use that for some other things I need to show!

 

Seeded Acres =

    SUMX(
        FILTER(
            GROUPBY('Table','Table'[Acres Seeded],'Table'[Year],'Table'[Grain],'Table'[ID]),
            'Table'[Acres Seeded]
        ),
        'Table'[Acres Seeded]
    )

 

 

@Anonymous 

Yes, that works also and you don't need the filter portion of your measure:

Seeded Acres = 
    SUMX(
        GROUPBY('Table','Table'[Acres Seeded],'Table'[Year],'Table'[Grain],'Table'[ID]),
        'Table'[Acres Seeded]
    )

Although I am having a hard time wrapping my head around what is happening with the context and why it is not doubling up on the [Acres Seeded].  It must have something to do with including it in the GROUPBY that is forcing it to consider the grouped value instead of each lines value.

Very interesting, thank you for sharing.

Anonymous
Not applicable

Thank you for the correction - I saw that on another solution and included it because I was focused on adding in the additional columns so I could group the data properly. I thought the filter seemed a little strange - I would have tried removing it when I had time, but I have several things in my job that I needed to handle first.

As you indicated, the GROUPBY is the key in that. I don't need four lines for a single seeded acres value for the same ID, commodity, grain and year - I just need one. The GROUPBY resolves that by only giving me one.

Once again, thank you. You have no idea how frustrating it's been for me. A few hours ago, I had zero solutions, now I have two, including one that will be perfect on some other data with which I need to work!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.