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

How do you get your IF statement model calculation to work correctly?

I have a disconnected table which has target percentages for gross margin (table 2). I have another table which has the raw data in which I calculate the margin percentages (table 1). 

 

PROBLEM = My Target Gross Margin IF statement (the output of either YES/NO listed on Table 1) is not calculating correctly in relation to the target percentages (the percentages listed in Table 2). 

- EXAMPLE: See line 1 of Table 1. Item Cost $8.93 -> The Target Gross Margin % = 71%. If the Margin %_purch does not exceed this percentage, the output should = NO. Currently, the output = YES

 

TARGET SOLUTION = The goal is to have the IF statement produce the correct output of either YES or NO true to the percentage targets listed in table 2. 

- EXAMPLE: See line 1 of Table 1. Item Cost $8.93 -> The Margin %_purch is < Target Gross Margin 71%. The output should = NO. 

 

Any ideas on where I am going wrong? My intuition is leading me to believe that my DAX syntax is wrong. 

 

 

Below are my DAX Syntax: 

 

MeetTargetGrossMargin? =
IF(
[Margin %_purch] >
SELECTEDVALUE('Cost Gross Margin%'[Gross Margin]),
"Yes", "No"
)
 
Margin %_purch = DIVIDE(SUM(PurchaseOrderTrans[PRICE]) - SUM(PurchaseOrderTrans[Item Cost]), SUM(PurchaseOrderTrans[PRICE]))
 
 

(Table 1 - Raw Data w/ Margin %_purch)

targetgrossmargin.png

 

 

 

(Table 2 - Disconnected Table w/ Target Gross Margin Percentages)

costmarginmatrix.png

 

2 ACCEPTED SOLUTIONS

@Anonymous

Can you show me the actual structure of your Table1? What you show in your first screen capture looks like a  matrix visual. If so, what fields are you using in that visual?

If I understand correctly what you are showing, try this:

 

 
MeetTargetGrossMargin? =
IF (
    [Margin %_purch]
        > CALCULATE (
            VALUES ( Table2[Gross Margin] ),
            FILTER (
                Table2,
                Table2[Cost From] <= SELECTEDVALUE ( Table1[PRICE] )
                    && Table2[Cost To] >= SELECTEDVALUE ( Table1[PRICE] )
            )
        ),
    "Yes",
    "No"
)
 

Code formatted with   www.daxformatter.com

View solution in original post

@Anonymous

 

I see there are quite a few issues. You marked it as solved but it's not working at all Smiley Very Happy Smiley Very Happy

 

1. First of all, the code I provided earlier was meant to be a measure, since you were showing a visual and were already using another measure to calculate the margin

2. I do not understand how the margin is calculated for each item. The code for your measure is:

 

Margin %_purch = DIVIDE(SUM(PurchaseOrderTrans[PRICE]) - SUM(PurchaseOrderTrans[Item Cost]); SUM(PurchaseOrderTrans[PRICE]))

I'm at a loss there, given the set-up you have in your visual. What are you trying to do? How would be the margin calculated conceptually? Why the SUM( )s?? Why does it have to be a measure?

 

3. I would just keep the calculation of the margin as a calculated column in your table, i.e. calculated per row in your table (don't know if that's the requirement):

 

MarginAsCOLUMN =
DIVIDE (
    PurchaseOrderTrans[PRICE] - PurchaseOrderTrans[Item Cost];
    PurchaseOrderTrans[PRICE]
)

 

4. And then another column in your table for the check:

 

MeetTargetGrossMargin? =
IF (
    PurchaseOrderTrans[MarginAsCOLUMN]
        > CALCULATE (
            VALUES ( 'Cost Gross Margin%'[Gross Margin] ),
            FILTER (
                ALL ( 'Cost Gross Margin%' ),
                'Cost Gross Margin%'[Cost From] <= Table1[PRICE]
                    && 'Cost Gross Margin%'[Cost To] >= Table1[PRICE]
            )
        ),
    "Yes",
    "No"
)

 

 

Code formatted with   www.daxformatter.com

View solution in original post

11 REPLIES 11

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.