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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
efstel
Helper I
Helper I

Subtotal and totals not computing correctly

I have two tables and want to use the values in the table/column 'JCCD'[ActualCost] except when in the values in table/column 'bihj HeavyJob_CostSummary'[HJCost] are higher than in the 'JCCD'[ActualCost] table/column only when 'HeavyJob_CostSummary'[LMSEO] or 'JCCD'[LMSEO] has a value of "Labor" or "Material". This works with the following: 

 

NewColumn =
IF(
    [HJCost minus ActualCost] > 0,
    SUM('bihj HeavyJob_CostSummary'[HJCost]),
    SUM('JCCD'[ActualCost])    
)
 
or: 
 
NewMeasureCO =
VAR LMSEO_Value = SELECTEDVALUE('bihj HeavyJob_CostSummary'[LMSEO])
VAR HJCost = SUM('bihj HeavyJob_CostSummary'[HJCost])
VAR ActualCost = SUM('JCCD'[ActualCost])

RETURN
IF(
    LMSEO_Value IN {"Labor", "Material"} && HJCost > ActualCost,
    HJCost,
    ActualCost
)
 
However, when you subtotal or total all the values they are not correct. How do I get the subtotal and total to compute the correct value?
 
Screenshot_2.png
4 REPLIES 4
v-yohua-msft
Community Support
Community Support

Hi, @efstel 

Based on your information, I create sample tables:

vyohuamsft_0-1738919290720.png

vyohuamsft_1-1738919300278.png

 

Create a new measure and try the following DAX:

CorrectedMeasureCO = 
SUMX(
    'bihj HeavyJob_CostSummary',
    VAR LMSEO_Value = 'bihj HeavyJob_CostSummary'[LMSEO]
    VAR HJCost = 'bihj HeavyJob_CostSummary'[HJCost]
    VAR ActualCost = CALCULATE(SUM('JCCD'[ActualCost]), 'JCCD'[LMSEO] = LMSEO_Value)
    RETURN
    IF(
        LMSEO_Value IN {"Labor", "Material"} && HJCost > ActualCost,
        HJCost,
        ActualCost
    )
)

 

Also create relationship:

vyohuamsft_2-1738919366348.png

 

It compares two tables of data and takes the largest one. Here is my preview:

vyohuamsft_3-1738919484350.png

 

How to Get Your Question Answered Quickly

Best Regards

Yongkang Hua

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

That is not working correclty. Also, I can't create a relationship between the 'JCCD' table and the 'bihj HeavyJob_CostSummary' table other than a many to many. The tables come from two different data sources and have millions of lines of data.  However, there is the following relationship between the tables. 

Screenshot_3.png

JCJP is a dimension table and contains unique JobNoCostCode. 'JCCD' and 'bihj HeavyJob_CostSummary' are both fact tables. 

Fowmy
Super User
Super User

@efstel 

You need to interate over the table, this will get the sub total right:

NewMeasureCO =
SUMX(
    'JCCD',
    VAR LMSEO_Value = LOOKUPVALUE('bihj HeavyJob_CostSummary'[LMSEO], 'bihj HeavyJob_CostSummary'[KeyColumn], 'JCCD'[KeyColumn])
    VAR HJCost = LOOKUPVALUE('bihj HeavyJob_CostSummary'[HJCost], 'bihj HeavyJob_CostSummary'[KeyColumn], 'JCCD'[KeyColumn])
    VAR ActualCost = 'JCCD'[ActualCost]
    
    RETURN
    IF(
        LMSEO_Value IN {"Labor", "Material"} && HJCost > ActualCost,
        HJCost,
        ActualCost
    )
)
Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

I'm not 100% sure what "KeyColumn" to use in the two tables. I've tried what I thougth was correct, but I keep getting an "Error fetching data" message that states "A table of multiple values was supplied where a sigle value was expected."

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!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

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