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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
mayara_alencar
New Member

Count clients with sales in distinct months

I'm trying to create  a measure where I'm counting the distinct clients from a vendor where the client has bought something in the selected month and also in the other two months before. Example: the select month is august and the client has to have sales in august and also in july or june. What i tried to use but is not working:

Active Clients=

var temp = SUMMARIZECOLUMNS( vw_sales[StartOfMonth],vw_sales[codclient], vw_sales[codvendor],
"actualsales",[.Sales R$],
"salesothermonths", [.Compra meses anteriores])
RETURN
CALCULATE(distinctcount(vw_sales[codclient]),  FILTER(temp, not ISBLANK(sumx(temp, [salesothermonths])) &&sumx(temp, [actualsales])>0))
 
 
I also tried and didn't work:
Var salesothermonths =  CALCULATE(
    distinctcount(vw_sales[codclient]),
    UNION(
        DATEADD(dimTempo[data], -1, MONTH),
        DATEADD(dimTempo[data], -2, MONTH)
    )
)
var actualsales = CALCULATE(distinctcount(vw_sales[codclient]), DATEADD(dimTempo[data],0,MONTH))
var ttclientes = distinctcount(vw_sales[codclient])
RETURN
SWITCH(TRUE(),actualsales>0 && salesothermonths>0,ttclientes)
 
Help?
6 REPLIES 6
v-yaningy-msft
Community Support
Community Support

Hi, @mayara_alencar 

Thanks for the reply from divyed and Jihwan_Kim. You can try the following dax to achieve your need.

vyaningymsft_0-1737442755780.png

Active = 
VAR _slicerMonth = SELECTEDVALUE(Slicer[month])
VAR _earlierMonth = _slicerMonth - 2
VAR _month = SELECTEDVALUE('Table 1'[month])
VAR _result = IF(_month>=_earlierMonth && _month <=_slicerMonth,CALCULATE(COUNTROWS('Table 2'),FILTER('Table 2','Table 2'[Active?] = "Active")))
RETURN
_result

Inactive = 
VAR _slicerMonth = SELECTEDVALUE(Slicer[month])
VAR _earlierMonth = _slicerMonth - 2
VAR _month = SELECTEDVALUE('Table 1'[month])
VAR _result = IF(_month>=_earlierMonth && _month <=_slicerMonth,CALCULATE(COUNTROWS('Table 2'),FILTER('Table 2','Table 2'[Active?] = "Inactive")))
RETURN
_result

 

 

Best Regards,
Yang

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know.
Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

mayara_alencar
New Member

This is an example of what i mean: 

mayara_alencar_0-1736777008460.png

And this should be the result:

mayara_alencar_1-1736777052851.png

 

Here's the sample:

Sample table

divyed
Super User
Super User

Hello @mayara_alencar ,

 

You can mofiy your dax to achieve the goal :

 

Active Clients =
VAR SelectedMonth = SELECTEDVALUE(vw_sales[StartOfMonth]) -- The selected month
VAR DateRange =
DATESINPERIOD(
vw_sales[StartOfMonth],
SelectedMonth,
-2,
MONTH
) -- Create a 3-month range including the selected month and 2 prior months
RETURN
CALCULATE(
DISTINCTCOUNT(vw_sales[codclient]), -- Count distinct clients
FILTER(
vw_sales,
vw_sales[StartOfMonth] IN DateRange && -- Check if the StartOfMonth falls within the range
vw_sales[Sales R$] > 0 -- Ensure there are sales
)
)

 

Modify variable and column names as per need.

I hope this helps.

Did I answer your query ? Mark this as solution if this helps, Kudos are appreciated.

 

Cheers

Did I answer your question ? Mark this as solution if this helps, Kudos are appreciated.
Proud to be a super user! LinkedIn : https://www.linkedin.com/in/neeraj-kumar-62246b26/

This one didn't work. It only counted the clients that have sales>0 in the current month. It should count the ones that also have sales in the 2 previous months.

Jihwan_Kim
Super User
Super User

Hi,

I tried to create a sample pbix file like below, and please check the below picture and the attached pbix file whether it suits.

 

Jihwan_Kim_1-1736566451771.png

 

 

Jihwan_Kim_0-1736566370849.png

 

Client count: = 
VAR _t = SUMMARIZE ( sales, client[client] ) 
RETURN 
COUNTROWS ( _t )

 

WINDOW function (DAX) - DAX | Microsoft Learn

 

Client count also in the otehr two months before: =
VAR _t =
    WINDOW (
        -2,
        REL,
        0,
        REL,
        ALL ( 'calendar'[Year-Month], 'calendar'[Year-Month sort] ),
        ORDERBY ( 'calendar'[Year-Month sort], ASC )
    )
RETURN
    CALCULATE ( [Client count:], _t )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

This also didn't work 😢

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!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.