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
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?
2 REPLIES 2
divyed
Resolver IV
Resolver IV

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

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.

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!

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.