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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
somogyitamas88
Regular Visitor

Disctinct count in daterange with visual calculations

Hi all!

I have an activity table with dates and user's operations. I need to calculate for a line chart (with timeline) a number for each date with unique user id-s on the previous 7 days.

I've created a DAX function over the datamodel with window function, but it's too slow. So I would like to replace it with a visual calculation formula where I put the date and userid fields to the line chart from the model.

What should be the formula then?

12 REPLIES 12
v-echaithra
Community Support
Community Support

Hi @somogyitamas88 ,

May I ask if you have resolved this issue? Please let us know if you have any further issues, we are happy to help.

Thank you.

v-echaithra
Community Support
Community Support

Hi @somogyitamas88 ,

Thank you @FBergamaschi , @Kedar_Pande  for your inputs.

I just wanted to check if the issue has been resolved on your end, or if you require any further assistance. Please feel free to let us know, we’re happy to help!


Thank you 

Kedar_Pande
Super User
Super User

@somogyitamas88 

 

7D Unique Users = 
RANKX(
WINDOW(-6,0,ALLSELECTED(Date[Date]),ORDERBY(Date[Date])),
DISTINCTCOUNT(Activity[UserID]),
,
ASC
)

Date (X-axis), UserID (small multiples or legend), measure above (Y)

FBergamaschi
Super User
Super User

Hi @somogyitamas88,

I see the code was alreayd given to you so I will not go on that

 

My worry is that you realize than the visual calculation will give a different result than a measure on the first 6 days if you apply a filter to the dates in the report

 

Imagine you filter a single month, say November 2025. On 1st of nov 205 I assume you would like to see a number depending on the last 7 days of october. But that, with a visual calculation, is impossible

 

Just wanted you to be aware of this

 

If this helped, please consider giving kudos and mark as a solution

@me in replies or I'll lose your thread

Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

Consider voting this Power BI idea

Francesco Bergamaschi

MBA, M.Eng, M.Econ, Professor of BI

Thank you.

cengizhanarslan
Super User
Super User

Instead of window function could you try the formula below:

Active Users (7D) =
VAR EndDate = MAX ( 'DimDate'[Date] )
RETURN
CALCULATE(
    DISTINCTCOUNT ( Activity[UserId] ),
    DATESINPERIOD ( 'DimDate'[Date], EndDate, -7, DAY )
)
_________________________________________________________
If this helped, ✓ Mark as Solution | Kudos appreciated
Connect on LinkedIn | Follow on Medium
AI-assisted tools are used solely for wording support. All conclusions are independently reviewed.

Unfortunately it's too slow.

This is my current one:

NumOfUsers7day_timeline =
CALCULATE(
    [NumOfUsers],
    WINDOW(
        -6, 0,
        REL,
        ORDERBY('Date'[Dates], ASC)
    )
)


It's faster however with more 10-15 sec loading.

MasonMA
Super User
Super User

Hi, 

 

You can use this below for visual calculation.

test =
    CALCULATE(
    SUM([UserID]),
    WINDOW(
        -6, 0,
        ORDERBY([Activity Date]
    )
    )
    )

MasonMA_0-1768318812067.png

 

 

deborshi_nag
Resident Rockstar
Resident Rockstar

Hi @somogyitamas88

 

Solution: For each date on the X‑axis, count the unique User IDs over the previous 7 calendar days (including that date).
 
- Put Date on the line chart X‑axis (continuous, daily).
- Drag UserId into the visual so that it’s available to the calculation without cluttering the chart.
- With the visual selected, choose Home ▸ New calculation to open the Visual Calculation editor. 
 
Use the following visual calculation formula -
 

Active Users (7D, VC) =
COUNTROWS(
    DISTINCT(
        SELECTCOLUMNS(
            WINDOW( -6, 0, ORDERBY([Date], ASC) ),
            "U", [UserId]
        )
    )
)
 
Hope this helps - please appreciate by leaving a Kudos or accepting it as a Solution to help others! 
I trust this will be helpful. If you found this guidance useful, you are welcome to acknowledge with a Kudos or by marking it as a Solution.

Hi, it only shows the distinct num of dates in the previous 7 days. (Mainly show 7, but when there are no activity on a day, its less).

Hi  @somogyitamas88

The visual calculation is operating on one row per Date, so the WINDOW is returning 7 date rows, and the DISTINCT(...) is effectively counting dates—not users.
 
Try the following change:

 

  • X‑axis: put your Date (continuous, daily).

  • Y‑axis: any metric (you can even use a dummy measure like 1).

  • Details: add UserId (this is the key step).

    • If the chart explodes into too many series, don’t worry—Power BI’s line chart won’t draw a separate line per detail value; Details just increases row-granularity for the calc.
    • With the visual selected, Home ▸ New calculation and paste:

      Active Users (7D, VC) =
      COUNTROWS(
          DISTINCT(
              SELECTCOLUMNS(
                  WINDOW( -6, 0, ORDERBY([Date], ASC) ),
                  "U", [UserId]
              )
          )
      )
I trust this will be helpful. If you found this guidance useful, you are welcome to acknowledge with a Kudos or by marking it as a Solution.

For me Details accepts only measure (at least implicit one). So I can drag User ID nowhere.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.