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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
LABrowne
Helper II
Helper II

Complex Conditional Calculation Formula

Hi there,

 

I have the following dax code:

(1)

V2 Net Sales =
SUMX(
    VALUES(AdjustmentType[AdjustmentTypeId]),
    SWITCH(AdjustmentType[AdjustmentTypeId],
    2, [V1 Gross Tax Sales],
    1, [V1 Net Before Tax Sales],
    3, [V1 Net Before Tax Sales],
    4, [V1 Net Before Tax Sales],
    5, [V1 Net Before Tax Sales]
    )
)
 
I need to build in another condition that if (2) Contract[ContractTable] = 22, then it has to use the following formula:
 
([Net Sales] / [Order Period]) * [Days Since Order]).
 
For all other contracts it should follow (1) though.
 
Any help would be great.
1 ACCEPTED SOLUTION
123abc
Community Champion
Community Champion

I see, if you're encountering an issue with having multiple columns in the VALUES function, you can modify the code to use a combination of CALCULATETABLE and FILTER. Here's an updated version of the code:

 

V2 Net Sales =
SUMX(
VALUES(AdjustmentType[AdjustmentTypeId]),
SWITCH(
TRUE(),
AdjustmentType[AdjustmentTypeId] = 2, [V1 Gross Tax Sales],
AdjustmentType[AdjustmentTypeId] = 1,
IF (
CALCULATE(
COUNTROWS(
FILTER(
Contract,
Contract[ContractTable] = 22
)
)
) > 0,
([V1 Net Before Tax Sales] / [Order Period]) * [Days Since Order],
[V1 Net Before Tax Sales]
),
AdjustmentType[AdjustmentTypeId] = 3, [V1 Net Before Tax Sales],
AdjustmentType[AdjustmentTypeId] = 4, [V1 Net Before Tax Sales],
AdjustmentType[AdjustmentTypeId] = 5, [V1 Net Before Tax Sales],
BLANK()
)
)

 

In this version, I've used CALCULATE along with COUNTROWS and FILTER to check if there is any row in the Contract table where ContractTable is equal to 22. If true, it applies the specified formula; otherwise, it uses [V1 Net Before Tax Sales]. Adjust the column and table names as needed based on your data model.

 

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

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @LABrowne ,

Please try below dax formula:

V2 Net Sales = 
   SUMX(
       VALUES(AdjustmentType[AdjustmentTypeId]),
       IF(
           MAX(Contract[ContractTable]) = 22,
           ([Net Sales] / [Order Period]) * [Days Since Order],
           SWITCH(
               AdjustmentType[AdjustmentTypeId],
               2, [V1 Gross Tax Sales],
               1, [V1 Net Before Tax Sales],
               3, [V1 Net Before Tax Sales],
               4, [V1 Net Before Tax Sales],
               5, [V1 Net Before Tax Sales]
           )
       )
   )

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

123abc
Community Champion
Community Champion

Certainly! To incorporate the additional condition you mentioned, you can modify your DAX code as follows:

 

V2 Net Sales =
SUMX(
VALUES(AdjustmentType[AdjustmentTypeId], Contract[ContractTable]),
IF(
Contract[ContractTable] = 22,
([Net Sales] / [Order Period]) * [Days Since Order],
SWITCH(
AdjustmentType[AdjustmentTypeId],
2, [V1 Gross Tax Sales],
1, [V1 Net Before Tax Sales],
3, [V1 Net Before Tax Sales],
4, [V1 Net Before Tax Sales],
5, [V1 Net Before Tax Sales]
)
)
)

 

In this modified code, I added the Contract[ContractTable] to the VALUES function, and I used an IF statement to check if the contract is equal to 22. If it is, then it uses the formula you provided; otherwise, it uses the SWITCH statement as before. This should achieve the desired behavior based on the specified conditions.

 

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

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

Hi there,

 

Thanks for your response but it's not letting me have 2 columns for the VALUES bit only 1 column is allowed?

123abc
Community Champion
Community Champion

I see, if you're encountering an issue with having multiple columns in the VALUES function, you can modify the code to use a combination of CALCULATETABLE and FILTER. Here's an updated version of the code:

 

V2 Net Sales =
SUMX(
VALUES(AdjustmentType[AdjustmentTypeId]),
SWITCH(
TRUE(),
AdjustmentType[AdjustmentTypeId] = 2, [V1 Gross Tax Sales],
AdjustmentType[AdjustmentTypeId] = 1,
IF (
CALCULATE(
COUNTROWS(
FILTER(
Contract,
Contract[ContractTable] = 22
)
)
) > 0,
([V1 Net Before Tax Sales] / [Order Period]) * [Days Since Order],
[V1 Net Before Tax Sales]
),
AdjustmentType[AdjustmentTypeId] = 3, [V1 Net Before Tax Sales],
AdjustmentType[AdjustmentTypeId] = 4, [V1 Net Before Tax Sales],
AdjustmentType[AdjustmentTypeId] = 5, [V1 Net Before Tax Sales],
BLANK()
)
)

 

In this version, I've used CALCULATE along with COUNTROWS and FILTER to check if there is any row in the Contract table where ContractTable is equal to 22. If true, it applies the specified formula; otherwise, it uses [V1 Net Before Tax Sales]. Adjust the column and table names as needed based on your data model.

 

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

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

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.