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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Pandadev
Post Prodigy
Post Prodigy

Get count of ID in the last 7 days , counting duplicate dates as one

I am struggling to get a formula to work , where I have a list of ID's and dates . and I want a count ID based the last 7 days

I have added this column to get the 7 day date -     7 days = Today()-7

I then added another column with this formula

Count ID 7 days = (CALCULATE(COUNTA('TU Feed'[ID]),FILTER(ALLSELECTED('TU Feed'),'TU Feed'[ID]=EARLIER('TU Feed'[ID])&&'TU Feed'[departure_date]>='TU Feed'[7 Days])))

 

But it is not working correctly. I have tried researching this for hours but can't get it to work. any help would be much appreciated

 

An example of my data in TU Feed

ID  - departure_date  -   time

1   - 20/04/2020        - 01:20

2   - 20/04/2020        -04:50

3  -  20/04/2020        -05:45

1  - 21/04/2020        - 06:45

1  - 21/04/2020        - 12:45  [exclude as duplicate date]

2  - 21/04/2020         - 04:20

2  - 21/04/2020         - 09:20 [exclude as duplicate date]

1  - 22/04/2020         - 10:45

1  - 22/04/2020         - 19:45

2  - 22/04/2020         - 04:20

3  - 22/04/2020         - 09:20

 

 

ID 1 - should equal 3

ID 2 - should equal 3

ID 3 - should equal 2

 

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

Hi, @Pandadev 

 

Based on your description, today is 4/28/2020.  I created data to reproduce your scenario.

Table:

b1.png

 

You may create a measure as below.

 

Count = 
var _id = SELECTEDVALUE('Table'[ID])
return
CALCULATE(
    DISTINCTCOUNT('Table'[Departure_date]),
    FILTER(
        ALLSELECTED('Table'),
        'Table'[ID] = _id&&
        'Table'[Departure_date]>=TODAY()-7&&
        'Table'[Departure_date]<=TODAY()
    )
)

 

 

Result:

b2.png

 

Best Regards

Allan

 

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

5 REPLIES 5
v-alq-msft
Community Support
Community Support

Hi, @Pandadev 

 

Based on your description, today is 4/28/2020.  I created data to reproduce your scenario.

Table:

b1.png

 

You may create a measure as below.

 

Count = 
var _id = SELECTEDVALUE('Table'[ID])
return
CALCULATE(
    DISTINCTCOUNT('Table'[Departure_date]),
    FILTER(
        ALLSELECTED('Table'),
        'Table'[ID] = _id&&
        'Table'[Departure_date]>=TODAY()-7&&
        'Table'[Departure_date]<=TODAY()
    )
)

 

 

Result:

b2.png

 

Best Regards

Allan

 

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

Thanks , for your help , i changed ALLSELECTED to just ALL in your formula , and it now works perfectly.

I think as I was filtering the table to show latest date , the  ALL SELECTED method was not working. 

jthomson
Solution Sage
Solution Sage

Why wouldn't a distinctcount of the date field work?

In my visual IU am filtering by latest date , so it only shows the ltest departure_time per ID , so when I do that it only shows 1 , as I assume it is filtering out all of the previous departure dates. i wanted to be able to show just the latest departure and then show how many in the past 7 days . 

The simplest form of the answer is: you have to use ALL() to restore the rows of the table so you can count them when the visuals are just displaying one row.

I wrote:

A measure to get the start date: 

CurrentDay = Today()
A measure to get the date 7 days back:
SevenDaysAgo = [CurrentDay]-7
And then a measure to count the distinct number of flights between the start date and 7 days back:
Seven Day Count =
var acount = calculate(DISTINCTCOUNT('Table'[Date]),
                                               filter(ALL('Table'),'Table'[Date]<=[SevenDaysAgo]))
the catch is that calculate will not accept the date comparison directly, so I had to wrap it in FILTER()
the ALL() as an argument to FILTER() makes sure I'm filtering all the records in 'Table', regardless of any filters being imposed by visuals on the report (which might be showing only one row)
return acount




Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.