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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
domdom
Helper II
Helper II

counting date instances occurring for last 2 months by user

Hello,

 

I have a question if you are able to help with it.  I have the following table of dates against user_ids (these will be updating frequently about 20,000 records):

 

TABLE A:

----------------------

date                      user_id

20/02/2016           A

22/02/2016           C

27/02/2016           D

04/03/2016           C

06/03/2016           C

01/04/2016           C

01/04/2016           A

30/04/2016           B

05/05/2016           C

06/05/2016           B

26/05/2016           A

01/06/2016           B

01/09/2016           C

 

 

I'd like to add another column to this same table which counts the total dates that exist between the date of the current record less 2 months for each user_id.  E.g.  for the value against the date 05/05/2016 for user_id C - it should count the total dates in the table between 05/03/2016 to 05/05/2016 that also have a user_id of C- so it would show 3 - as the following dates fall within this range:

 

06/03/2016           C

01/04/2016           C

05/05/2016           C

 

hope that makes senses?

 

 

Using my sample data - the final table should look as follows:

 

TABLE A:

----------------------

date                      user_id          total dates less 2 months by user

20/02/2016           A                   1

22/02/2016           C                   1

27/02/2016           D                   1 

04/03/2016           C                   2 

06/03/2016           C                   3

01/04/2016           C                   4

01/04/2016           A                   2

30/04/2016           B                   1

05/05/2016           C                   3

06/05/2016           B                   2

26/05/2016           A                   2

01/06/2016           B                   3

01/09/2016           C                   1  

 

If you are able to help with this it would be most appreciated!

 

many thanks

1 ACCEPTED SOLUTION
Zubair_Muhammad
Community Champion
Community Champion

@domdom

 

Hi,

 

Please try this column

 

Column =
VAR CurrentDate = Table1[date ]
VAR TwoMonthsbefore =
    DATE ( YEAR ( CurrentDate ), MONTH ( CurrentDate ) - 2, DAY ( CurrentDate ) )
RETURN
    CALCULATE (
        COUNTROWS ( Table1 ),
        FILTER (
            ALLEXCEPT ( Table1, Table1[user_id ] ),
            Table1[date ] <= CurrentDate
                && Table1[date ] >= TwoMonthsbefore
        )
    )

Regards
Zubair

Please try my custom visuals

View solution in original post

3 REPLIES 3
v-jiascu-msft
Employee
Employee

Hi @domdom,

 

Could you please mark the proper answers as solutions?

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Zubair_Muhammad
Community Champion
Community Champion

@domdom

 

Hi,

 

Please try this column

 

Column =
VAR CurrentDate = Table1[date ]
VAR TwoMonthsbefore =
    DATE ( YEAR ( CurrentDate ), MONTH ( CurrentDate ) - 2, DAY ( CurrentDate ) )
RETURN
    CALCULATE (
        COUNTROWS ( Table1 ),
        FILTER (
            ALLEXCEPT ( Table1, Table1[user_id ] ),
            Table1[date ] <= CurrentDate
                && Table1[date ] >= TwoMonthsbefore
        )
    )

Regards
Zubair

Please try my custom visuals

@domdom

 

pbix file attached as well

 

countingd.png


Regards
Zubair

Please try my custom visuals

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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