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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
JCMT
Frequent Visitor

Rolling DistinctCount + Filters

Hi All, 

 

My dataset has a calendar table and a table containing records with various properties that are linked by date to the calendar table, I've setup a measure to output a rolling total count of unique record IDs for the past 7 days as per below: 

 

 

 

Unique Record IDs 7D Count = 
CALCULATE(
    SUMX(
        VALUES(Calendar[Date]), DISTINCTCOUNT(Records[ID]),
        FILTER(
            ALLSELECTED(Calendar),Calendar[Date] >= (max(Calendar[Date])-6) && Calendar[Date] <= max(Calendar[Date])
            )
            )

 

 

 

 This measure works perfectly if the data is unfiltered, however applying a filter to one of the fields in the Record table restricts the available date range doesn't give me the result I'm after. 

 

Below is an example of the output of the Rolling Count after filtering 

Date Unique Record IDsRolling Count (7D)
25/08/2024 0 
26/08/2024 0 
27/08/2024 33
28/08/2024 58
29/08/2024 1018
30/08/2024 624
31/08/2024 0 
1/09/2024 0 
2/09/2024 0 
3/09/2024 0 
4/09/2024 0 
5/09/2024 0 
6/09/2024 0 
7/09/2024 0 

And here's the desired output 

Date Unique Record IDsRolling Count (7D)
25/08/2024 00
26/08/2024 00
27/08/2024 33
28/08/2024 58
29/08/2024 1018
30/08/2024 624
31/08/2024 024
1/09/2024 024
2/09/2024 024
3/09/2024 021
4/09/2024 016
5/09/2024 06
6/09/2024 00
7/09/2024 00

I was under the impression that ALLSELECTED might override the impact of the filter applied to the record table but that seems to be an incorrect assumption on my part. 

 

Found this post which seemed to be an almost identical situation however I've had no luck adapting the solution to get the desired output. Any suggestions / assistance would be much appreciated.

 

Edit: Sorry - table formatting bad. 

1 ACCEPTED SOLUTION
JCMT
Frequent Visitor

Still haven't identified exactly what the issue was but it seems it was something in the source dataset because when I started from scratch with a fresh copy of the data and re created the measures it all behaved as expected. 

 

Ultimately I wanted a rolling average, here are the final measures: 

Unique Record IDs = COALESCE(DISTINCTCOUNT(Records[ID]), 0)


Unique Record IDs Rolling 7D Average =
DIVIDE(
    CALCULATE(
        SUMX(
            VALUES('Calendar'[Date]), [Unique Record IDs]
            ),
            REMOVEFILTERS('Calendar'[Date]),
            'Calendar'[Date] >= (max('Calendar'[Date])-6) && 'Calendar'[Date] <= max('Calendar'[Date])
            ),
        7
)

 

View solution in original post

3 REPLIES 3
JCMT
Frequent Visitor

Still haven't identified exactly what the issue was but it seems it was something in the source dataset because when I started from scratch with a fresh copy of the data and re created the measures it all behaved as expected. 

 

Ultimately I wanted a rolling average, here are the final measures: 

Unique Record IDs = COALESCE(DISTINCTCOUNT(Records[ID]), 0)


Unique Record IDs Rolling 7D Average =
DIVIDE(
    CALCULATE(
        SUMX(
            VALUES('Calendar'[Date]), [Unique Record IDs]
            ),
            REMOVEFILTERS('Calendar'[Date]),
            'Calendar'[Date] >= (max('Calendar'[Date])-6) && 'Calendar'[Date] <= max('Calendar'[Date])
            ),
        7
)

 

SamWiseOwl
Super User
Super User

Hi @JCMT 

Three things to note:
AllSelected removes internal filters, so the filters caused by the date row inside of the visual.

All removes all filters on the given table/column both internal (visual) and external (slicer).

This could be part of the misunderstanding.

 

Secondly:

Calculate lets you change, replace or remove filters.

Filter keeps the existing filters and adds to them.

So this could also be a cause of your issue.

 

Finally applying a filter to one of the fields in the Record table

Removing/adding filters to the calendar table will affect the Record table.

However if the filter is on some other column for example RecordType this wouldn't be removed by the AllSelected here.

You could use something like AllExcept(Record, Calendar[Date]) to remove all filters on the Record table but keep the filters on the Calendar table.

 

What are the filters you are adding and which do you want to keep?


If you are happy with this answer please mark as a solution for others to find !

Kudos are always appreciated! Check out our free Power BI video courses.

Thank you very much for your response..

 

Finally applying a filter to one of the fields in the Record table

Removing/adding filters to the calendar table will affect the Record table.

However if the filter is on some other column for example RecordType this wouldn't be removed by the AllSelected here.

You could use something like AllExcept(Record, Calendar[Date]) to remove all filters on the Record table but keep the filters on the Calendar table.

 

What are the filters you are adding and which do you want to keep?


I want to keep the filter on the Record table (eg: RecordType) and remove any implicit filtering on the Calendar table. 

 

I can't paste raw data but can generate a mock up if that's needed to help further. 

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.