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
Mark_Timson
Helper I
Helper I

How to Sum based on an if statement

Hi all,

 

I was wondering if it was possible to do something similar to the below in power bi

 

IF Table.Col = "Y" THEN SUM(Table1.Col) ELSE IF Table.Col = "N" THEN SUM(Table2.Col) ELSE 0.

 

I want to change the column I am summing on based on its related value in another table in this case when it = Y or = N.

 

Your help would be appreciated.

8 REPLIES 8
AkhilAshok
Solution Sage
Solution Sage

Tough to answer this without seeing the datamodel or how you use the measure in report. Is Table[Col] a madatory single value filter in report? If yes, then you could simply do it with a measure:

Measure =
SWITCH (
    SELECTEDVALUE ( Table[Col] ),
    "Y", SUM ( Table1[Col] ),
    "N", SUM ( Table1[Col] ),
    0
)

If this is not the case, and you want the measure to be dynamically based on value in the Table[Col], then you could try as below (assuming you have a One-to-Many relationship between Table -> Table1 & Table -> Table2):

Measure =
VAR MeasureY =
    CALCULATE ( SUM ( Table1[Col] ), Table[Col] = "Y" )
VAR MeasureN =
    CALCULATE ( SUM ( Table2[Col] ), Table[Col] = "N" )
RETURN
    MeasureY + MeasureN

Hi Akhil,

 

Thanks for replying. My result set isnt actually based off of these y or n values its more like an expression on the data. Its originally from business objects. Below is an extract from what I am trying to reproduce in power bi. 

 

IF Data .PNG

Have you tried the 2nd option which I mentioned?

Hi, 

 

Im not trying to sum both together Im trying to return one or the other based on the data in the table. 

Hi @Mark_Timson ,

If it is convenient, could you share your data sample and your desired output so that we could help further on it?

Best  Regards,

Cherry

 

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

What is the type of object in BO Universe? Is it created as a measure or dimension or detail? If it is created as a measure, and if the Projection setting of the object is Sum, then it should work the same way as I showed using DAX, since the entire logic gets summed up at the report level (both Y and N cases).

Gordonlilj
Solution Sage
Solution Sage

Hi,
You could try:
 
SWITCH(TRUE(),Table.Col="Y",SUM(Table1.Col),Table.Col ="N",SUM(Table2.Col),0)

This didnt work as the column that is being filtered is not related I think. 

 

IF Data .PNG

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.