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
Ritaf1983
Super User
Super User

cumulative distinct by month

I have a star schema, and I need to calculate the cumulative number of active customers each month. A customer can be counted multiple times if they were active in different months. An active customer is one who made a transaction and has a row in the transactions table.

The calculation should cover the last 12 months, with the first month starting in October 2023 (from the 1st to the 7th), and the last month being the current one, up to the latest day.

The cumulative count should sum the active customers month by month.

I need help with Dax formula to achieve it.

PBIX is attached

Ritaf1983_0-1728320624798.png

 

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile
1 ACCEPTED SOLUTION

Hi,

Thank you for your message.

Sorry that I don't understand 100%, but, if the condition is to specify the starting period based on the needs, please try something like below.
In the measure, regarding VAR _startingdate, I hope you can try to fix it based on the needs. 

 

expected result measure: =
VAR _startdate =
    DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), 1 ) //custom start date
VAR _startdatecode =
    MAXX (
        FILTER (
            ALL ( 'Calendar'[Date], 'Calendar'[Year-Month Code] ),
            'Calendar'[Date] = _startdate
        ),
        'Calendar'[Year-Month Code]
    ) //custom yearmonth code
VAR _t =
    FILTER (
        WINDOW (
            1,
            ABS,
            0,
            REL,
            ALL ( 'Calendar'[Year-Month], 'Calendar'[Year-Month Code] ),
            ORDERBY ( 'Calendar'[Year-Month Code], ASC )
        ),
        'Calendar'[Year-Month Code] >= _startdatecode
    )
VAR _bydefinition =
    ADDCOLUMNS (
        _t,
        "@distinctcount", CALCULATE ( COUNTROWS ( DISTINCT ( 'orders'[Customer ID] ) ) )
    )
RETURN
    SUMX ( _bydefinition, [@distinctcount] )

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.

View solution in original post

5 REPLIES 5
ThxAlot
Super User
Super User

RT_12M = 
VAR __lymtd =
    CALCULATE(
        [Customers quantity],
        CALCULATETABLE(
            DATEADD( DATESMTD( 'Calendar'[Date] ), -1, YEAR ),
            KEEPFILTERS( 'Calendar'[Date] <= TODAY() )
        )
    )
VAR __11m =
    CALCULATE(
        SUMX( VALUES( 'Calendar'[Year-Month] ), [Customers quantity] ),
        DATESINPERIOD( 'Calendar'[Date], MIN( 'Calendar'[Date] ) - 1, -11, MONTH )
    )
RETURN
    __lymtd + __11m
RT_12M = 
VAR __lymtd =
    CALCULATE(
        [Customers quantity],
        CALCULATETABLE(
            DATEADD( DATESMTD( 'Calendar'[Date] ), -1, YEAR ),
            KEEPFILTERS( 'Calendar'[Date] <= TODAY() )
        )
    )
VAR __11m =
    CALCULATE(
        SUMX( VALUES( 'Calendar'[Year-Month] ), [Customers quantity] ),
        DATESINPERIOD( 'Calendar'[Date], MIN( 'Calendar'[Date] ) - 1, -11, MONTH )
    )
RETURN
    __lymtd + __11m

ThxAlot_0-1728333334773.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



Hi @ThxAlot 
Thank you very much for your response. I'm not referring to a rolling 12-month sum, but rather a cumulative count month by month for a 12-month period. Please see my reply to @Jihwan_Kim 

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile
Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly, but please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1728322609719.png

WINDOW function (DAX) - DAX | Microsoft Learn

 

 

expected result measure: = 
VAR _t =
    WINDOW (
        1,
        ABS,
        0,
        REL,
        ALL ( 'Calendar'[Year-Month], 'Calendar'[Year-Month Code] ),
        ORDERBY ( 'Calendar'[Year-Month Code], ASC )
    )
VAR _bydefinition =
    ADDCOLUMNS (
        _t,
        "@distinctcount", CALCULATE ( COUNTROWS ( DISTINCT ( 'orders'[Customer ID] ) ) )
    )
RETURN
    SUMX ( _bydefinition, [@distinctcount] )

 

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.

Hi @Jihwan_Kim 
Thank you very much, for the help. You got it right, except I need it for the last 13 months, not from the start of the calender. Meaning, October 2023 should be 0, and the accumulation should start from there. Your calculation sums correctly, but I can't manage to limit the period.
screenshot from my original file :

Ritaf1983_0-1728329413427.png

I have a dax for standard distinct count like :

Active_customers_12_months =
VAR last_date = 'Last refresh'[Last_closed_day]
VAR first_date = DATE(YEAR(last_date) - 1, MONTH(last_date), 1)
VAR last_day_of_first_month = DAY(last_date)
RETURN
CALCULATE(
    DISTINCTCOUNT('FactSalesDaily'[CustomerKey]),
    FILTER(
        'Calendar',
        'Calendar'[Date] >= first_date &&
        'Calendar'[Date] <= last_date &&
        (
            YEAR('Calendar'[Date]) <> YEAR(first_date) ||
            MONTH('Calendar'[Date]) <> MONTH(first_date) ||
            DAY('Calendar'[Date]) <= last_day_of_first_month
        )
    )
)
and tried to create something similar for the cumulated with your solution :
expected result measure =
VAR last_date = 'Last refresh'[Last_closed_day]
VAR first_date = DATE(YEAR(last_date) - 1, MONTH(last_date), 1) -- Custom start date (e.g., October 2023)
VAR _t =
    WINDOW (
        1, -- Start of window
        ABS, -- Absolute reference from the start
        0, -- No offset
        REL, -- Relative positioning for window
        FILTER(
            ALL ( 'Calendar'[Year-Month], 'Calendar'[Year-Month Code] ),
            MIN('Calendar'[Date]) >= first_date && MAX('Calendar'[Date]) <= last_date -- Use MIN and MAX to aggregate and ensure the custom date range is respected
        ),
        ORDERBY ( 'Calendar'[Year-Month Code], ASC ) -- Order by month
    )
VAR _bydefinition =
    ADDCOLUMNS (
        _t,
        "@distinctcount", CALCULATE ( DISTINCTCOUNT ( 'FactSalesDaily'[CustomerKey] ) ) -- Distinct count of customers within the custom window
    )
RETURN
    SUMX ( _bydefinition, [@distinctcount] ) -- Accumulate distinct counts across the window
 
But it is not working, the calculation is there except of start / end point
Ritaf1983_1-1728329576647.png

 

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

Hi,

Thank you for your message.

Sorry that I don't understand 100%, but, if the condition is to specify the starting period based on the needs, please try something like below.
In the measure, regarding VAR _startingdate, I hope you can try to fix it based on the needs. 

 

expected result measure: =
VAR _startdate =
    DATE ( YEAR ( TODAY () ) - 1, MONTH ( TODAY () ), 1 ) //custom start date
VAR _startdatecode =
    MAXX (
        FILTER (
            ALL ( 'Calendar'[Date], 'Calendar'[Year-Month Code] ),
            'Calendar'[Date] = _startdate
        ),
        'Calendar'[Year-Month Code]
    ) //custom yearmonth code
VAR _t =
    FILTER (
        WINDOW (
            1,
            ABS,
            0,
            REL,
            ALL ( 'Calendar'[Year-Month], 'Calendar'[Year-Month Code] ),
            ORDERBY ( 'Calendar'[Year-Month Code], ASC )
        ),
        'Calendar'[Year-Month Code] >= _startdatecode
    )
VAR _bydefinition =
    ADDCOLUMNS (
        _t,
        "@distinctcount", CALCULATE ( COUNTROWS ( DISTINCT ( 'orders'[Customer ID] ) ) )
    )
RETURN
    SUMX ( _bydefinition, [@distinctcount] )

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!

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.

December 2024

A Year in Review - December 2024

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