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
WishAskedSooner
Continued Contributor
Continued Contributor

DAX Help with tricky CALCULATE

Hi Experts!

 

My fact table looks like this:

EIDAIDVAL
1180
120
13100
210
22170
23200
31270
32270
33300
41380
43400

Currently, this is my Measure:

 

DIVIDE(
   CALCULATE(
      SUM('Fact'[VAL]),
      'Fact'[AID] IN {1, 2}
   ),
   CALCULATE(
      SUM('Fact'[VAL]),
      'Fact'[AID] = 3
   )
)

 

which results in the following matrix visual:

EIDPerc
180%
285%
3180%
495%

However, Perc for EID = 3 is wrong. What I want is a Measure that produces the following matrix:

EIDPerc
180%
285%
390%
4

95%

The complication comes in with AID = 2 which can sometimes be either zero (e.g. EID = 1), greater than zero (EID = 2 and 3), or missing (e.g. EID -= 4).

 

So I need a measure that performs like the pseudo-code below where if the VAL is greater than zero when AID = 2 and not missing then calculate the percentage with AID = 2 only. Otherwise, calculate the percentage with AID = 1 only.

IF(
   'Fact'[VAL] > 0 WHEN 'Fact'[AID] = 2, // The same row
   DIVIDE(
      CALCULATE(
         SUM('Fact'[VAL]),
         'Fact'[AID] = 2
      ),
      CALCULATE(
         SUM('Fact'[VAL]),
         'Fact'[AID] = 3
      )
   ),
   DIVIDE(
      CALCULATE(
         SUM('Fact'[VAL]),
         'Fact'[AID] = 1
      ),
      CALCULATE(
         SUM('Fact'[VAL]),
         'Fact'[AID] = 3
      )
   )
)

Thanks in advance for the help!

1 ACCEPTED SOLUTION
Tutu_in_YYC
Super User
Super User

Try this:

Measure =
DIVIDE(
   VAR _AID2 =
   CALCULATE(
      SUM('Fact'[VAL]),
      'Fact'[AID] = 2
   )
   RETURN
   IF(
        _AID2 > 0,
        _AID2,
        CALCULATE(
            SUM('Fact'[VAL]),
            'Fact'[AID] = 1
        )
   )
   ,
   CALCULATE(
      SUM('Fact'[VAL]),
      'Fact'[AID] = 3
   )
)

 

View solution in original post

3 REPLIES 3
Tutu_in_YYC
Super User
Super User

Try this:

Measure =
DIVIDE(
   VAR _AID2 =
   CALCULATE(
      SUM('Fact'[VAL]),
      'Fact'[AID] = 2
   )
   RETURN
   IF(
        _AID2 > 0,
        _AID2,
        CALCULATE(
            SUM('Fact'[VAL]),
            'Fact'[AID] = 1
        )
   )
   ,
   CALCULATE(
      SUM('Fact'[VAL]),
      'Fact'[AID] = 3
   )
)

 

@Tutu_in_YYC 

 

Wow! That worked perfectly and is beautiful DAX code. Exactly what I needed. Many Kudos!

Glad to hear!

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.