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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
minhvuong93
Helper II
Helper II

[Help] Count with condition across column

Hi all,

I have a data of customer annual sales by month and by SalesRep like below.

I want to do something like countif , to see: How Many Months did a Customer have the Volume Sales (Volume month >0). This should be the total volume of the customer (does not matter if 2 or more Sales Rep sold to him)

 

For Excel it is easy to use Countif like below. But I don't know how to achieve the same result in DAX ?

 

powerbi.PNG

 Thanks for your kind help,

 

 

2 ACCEPTED SOLUTIONS
Reid_Havens
Most Valuable Professional
Most Valuable Professional

Hi there,

 

Happy to help! So DAX works best calculating rows within a column. It's trickier when you have values for a customer split across columns, however there is a wonderful feature in "Get & Transform (Power Query)" that lets you UnPivot columns onto rows. If you're able to do that in Power Query first then creating the DAX measure is MUCH easier. Your table would look like the one pictured below following the "UnPivoting".

UnPivoted.png

With the table in this structure you would create two DAX measures: [Total Volume] & [# of Months w/ Sales], with the idea that DAX measures are a lot like lego blocks, one building on the other. The [Total Volume] measure will be referenced in [# of Months w/ Sales] which will count the months of sales.

 

[Total Volume] Formula:

= SUM( 'Table'[Volume] )

[# of Months w/ Sale] Formula:

=
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Volume Month] ),
    FILTER ( 'Table', [Total Volume] > 0 )
)

I'm happy to send you over the PBI file with these transformations/formulas as well if you'd like.

 

View solution in original post

Have a play with this in the Query Editor

 

Highlight the three columns you want to "unpivot" then right click and select Unpivot columns

 

unpivot1.png

Turned into this....

 

unpivot 2.png

 


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

View solution in original post

8 REPLIES 8
Reid_Havens
Most Valuable Professional
Most Valuable Professional

Hi there,

 

Happy to help! So DAX works best calculating rows within a column. It's trickier when you have values for a customer split across columns, however there is a wonderful feature in "Get & Transform (Power Query)" that lets you UnPivot columns onto rows. If you're able to do that in Power Query first then creating the DAX measure is MUCH easier. Your table would look like the one pictured below following the "UnPivoting".

UnPivoted.png

With the table in this structure you would create two DAX measures: [Total Volume] & [# of Months w/ Sales], with the idea that DAX measures are a lot like lego blocks, one building on the other. The [Total Volume] measure will be referenced in [# of Months w/ Sales] which will count the months of sales.

 

[Total Volume] Formula:

= SUM( 'Table'[Volume] )

[# of Months w/ Sale] Formula:

=
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Volume Month] ),
    FILTER ( 'Table', [Total Volume] > 0 )
)

I'm happy to send you over the PBI file with these transformations/formulas as well if you'd like.

 

Hi @Reid_Havens

I tried to use your formula but  it was taking forever to calculate the result.

Is there any faster way to perform this 😞

Reid_Havens
Most Valuable Professional
Most Valuable Professional

Hi @minhvuong93,

 

Three main factors determine how fast something calculate: The size of the data, speed of the machine the calculation is being ran on, and the complexity of the formula. Could you answer the first two for me? The SUM and DISTINCTCOUNT formulas in DAX are very basic calculations that should require very little processing power to calculate.

@Reid_Havens

Thanks Reid,

Actually I have more than 36 month column and million of rows. But I will have a try since this is better than create a column function.. ,

Could you please kindly send me the file you mentioned as well 🙂

Highly appreciate your quick support,

Cheers,

 

Have a play with this in the Query Editor

 

Highlight the three columns you want to "unpivot" then right click and select Unpivot columns

 

unpivot1.png

Turned into this....

 

unpivot 2.png

 


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Phil_Seamark
Microsoft Employee
Microsoft Employee

Hi @minhvuong93

 

In the short term, you could add the following calculated column to your table to simulate the COUNTIF function

 

 

Number of Times Buying = 
              IF([Volume Jan]>0,1,0) 
            + IF([Volume Feb]>0,1,0) 
            + IF([Volume Mar]>0,1,0) 

 

However,  I recommend you pivot your data structure to be the following which will make it alot easier to perform a variety of calculations and not rely on hardcoding as per my above column

 

 

SalesRep , CustomerID , Month  , Volume
----------------------------------------
SR1 , C1 , Jan 17 , 1
SR1 , C1 , Mar 17 , 1
SR2 , C1 , Mar 17 , 1
etc ....

 


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Thanks @Phil_Seamark ,

The problem is the data provided for me is fixed like that. 

How can I restructure the data from what I have into like you mention above?

Since it is a very large data with million of rows

 

Hi @minhvuong93

 

You can pivot it either in the Query Editor or in DAX.  If you can do it in the Query Editor then your model will have less work to do.

 

 


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.