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

1 or 0 based on sum two columns

Hi

 

I have two columns with integers.

I need to sum this two columns, and if the value is 1 or greater, then sum 1, else 0.

How can I do this in DAX?

 

Thank you!

Example:
Column1    Column2    Result

1                        1             1

0                        0             0

0                        1             1

1                        0             1 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Finally, I solved with this dax expression:
 
# CondicionalSum = CALCULATE(
    COUNT('Table1'[Column1]);
    FILTER(
        'Table1';
        'Table1'[Column1] >= 1 ||
        'Table1'[Column2] >= 1
    )
)
 
Thanks!

View solution in original post

Anonymous
Not applicable

Finally I've changed it to PowerQuery with the following expression. The solution in DAX also works, but according to the link below, it's faster and better to use Power Query computed columns whenever the expression only depends on other columns of the same row of the table where you create the new column

 

PowerQuery Computed Column - The better choice

Column = if ([Column1]+[Column2]>0) then 1 else 0

 

DAX Calculated Column:

Column = if(Table1[Column1]+Table1[Column2]>0;1;0)

 

Thanks!!

 

https://www.sqlbi.com/articles/comparing-dax-calculated-columns-with-power-query-computed-columns/

View solution in original post

10 REPLIES 10
Anonymous
Not applicable

Finally, I solved with this dax expression:
 
# CondicionalSum = CALCULATE(
    COUNT('Table1'[Column1]);
    FILTER(
        'Table1';
        'Table1'[Column1] >= 1 ||
        'Table1'[Column2] >= 1
    )
)
 
Thanks!
az38
Community Champion
Community Champion

@Anonymous 

it looks completely different with first task, anyway, great to see you have solved the problem. good luck! 🙂

do not hesitate to kudo useful posts and mark solutions as solution
Linkedin


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Anonymous
Not applicable

@az38 yeah agree with you it doesnt sum up two columns

Anonymous
Not applicable

Hi ,

you can use this measure

 

Column = if( 'Table'[data1]+'Table'[data2]>0,1,0)

 

sumcomlumn.PNG

 

Regards,

Husna

az38
Community Champion
Community Champion

@Anonymous why measure, not calculated column? measure uses more memory, from my experience, for aim to row-by-row calculations column is much better


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Anonymous
Not applicable

Are you sure? I came from QlikView, and it better to have simple calculations done in processing time rather than in load time.

 

But I'm not sure in PowerBi

az38
Community Champion
Community Champion

@Anonymous  agree. but there is a difference between calculated column and measure. column is about row context (which is less), measure is about filter and query context.


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Anonymous
Not applicable

@az38 I havent used measure over there , it a column where in formula is inserted .

 

az38
Community Champion
Community Champion

Hi @Anonymous 

try new column

Column = if(Table1[Column1]+Table1[Column2]>0;1;0)

do not hesitate to kudo useful posts and mark solutions as solution
Linkedin


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Anonymous
Not applicable

Finally I've changed it to PowerQuery with the following expression. The solution in DAX also works, but according to the link below, it's faster and better to use Power Query computed columns whenever the expression only depends on other columns of the same row of the table where you create the new column

 

PowerQuery Computed Column - The better choice

Column = if ([Column1]+[Column2]>0) then 1 else 0

 

DAX Calculated Column:

Column = if(Table1[Column1]+Table1[Column2]>0;1;0)

 

Thanks!!

 

https://www.sqlbi.com/articles/comparing-dax-calculated-columns-with-power-query-computed-columns/

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.