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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
yakovlol
Resolver I
Resolver I

Count Consecutive Months for Users

Hello)

I have a problem could you please help me to count consecutive Months from the last available month in the data set for Users (column ID)

For example, for User 1 I want to have count 3 (3 months when a user with id 1 has values)

User 2 - the same 3
User 3 - 0 (because there is no values)
User 4 - 3 and so on

yakovlol_1-1689080410999.png

Maybe there are any chances to solve it?

Thank you so much for support.

 

1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @yakovlol ,

 

Firstly, please make sure your table looks like as below.

vrzhoumsft_0-1689234677642.png

Or you can try UNPIVOT function to translate it in Power Query Editor.

Measure:

Measure = 
VAR _STEP1 =
    ADDCOLUMNS (
        'Table',
        "Flag",
            IF (
                EOMONTH ( 'Table'[Date], 0 )
                    + 1
                        IN CALCULATETABLE ( VALUES ( 'Table'[Date] ), ALLEXCEPT ( 'Table', 'Table'[ID] ) ),
                1,
                0
            ),
        "MaxDate",
            MAXX (
                FILTER ( 'Table', 'Table'[ID] = EARLIER ( 'Table'[ID] ) ),
                'Table'[Date]
            )
    )
VAR _STEP2 =
    ADDCOLUMNS (
        _STEP1,
        "PrevioiusDate",
            MAXX (
                FILTER ( _STEP1, [ID] = EARLIER ( [ID] ) && [Date] < [MaxDate] && [Flag] = 0 ),
                [Date]
            )
    )
RETURN
    COUNTAX (
        FILTER (
            _STEP2,
            [Date] > [PrevioiusDate]
                && [Date] <= [MaxDate]
                && [Value] <> 0
        ),
        [ID]
    )

Result is as below.

vrzhoumsft_1-1689234734625.png

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-rzhou-msft
Community Support
Community Support

Hi @yakovlol ,

 

Firstly, please make sure your table looks like as below.

vrzhoumsft_0-1689234677642.png

Or you can try UNPIVOT function to translate it in Power Query Editor.

Measure:

Measure = 
VAR _STEP1 =
    ADDCOLUMNS (
        'Table',
        "Flag",
            IF (
                EOMONTH ( 'Table'[Date], 0 )
                    + 1
                        IN CALCULATETABLE ( VALUES ( 'Table'[Date] ), ALLEXCEPT ( 'Table', 'Table'[ID] ) ),
                1,
                0
            ),
        "MaxDate",
            MAXX (
                FILTER ( 'Table', 'Table'[ID] = EARLIER ( 'Table'[ID] ) ),
                'Table'[Date]
            )
    )
VAR _STEP2 =
    ADDCOLUMNS (
        _STEP1,
        "PrevioiusDate",
            MAXX (
                FILTER ( _STEP1, [ID] = EARLIER ( [ID] ) && [Date] < [MaxDate] && [Flag] = 0 ),
                [Date]
            )
    )
RETURN
    COUNTAX (
        FILTER (
            _STEP2,
            [Date] > [PrevioiusDate]
                && [Date] <= [MaxDate]
                && [Value] <> 0
        ),
        [ID]
    )

Result is as below.

vrzhoumsft_1-1689234734625.png

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Solution Authors
Top Kudoed Authors