cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
siewhun
Regular Visitor

Calculate average weekly active visitor using DAX

Visitor ID  DateVisit
15/10/2022  Visit 1      
25/10/2022Visit 1
35/10/2022Visit 1
15/10/2022Visit 2
55/10/2022Visit 1
25/10/2022Visit 2
15/11/2022Visit 1
65/11/2022Visit 1
95/11/2022Visit 1
105/12/2022Visit 1
115/12/2022Visit 1
125/13/2022Visit 1
25/16/2022Visit 1
25/16/2022Visit 2
135/18/2022Visit 1
145/18/2022Visit 1

 

Hello everyone,

 

I'm trying to calculate average weekly active visitors in PowerBI using DAX based on the example in the table above.

 

First, I need to calculate how many unique visitors visit the shop in one week (Mon-Sun). Based on the calendar, 5/10/2022 - 5/13/2022 will be categorized under one week, and 5/16/2022 - 5/18/2022 is another week. So for the week from 5/10/2022 - 5/13/2022, there are 9 unique visitor and for the week from 5/16/2022 - 5/18/2022, there are 3 unique visitors.

Once I found out the unique visitor for respective weeks, then I can get the average weekly active visitor by:

(9+3)/2 = 6 visitors

Hence, the answer for the example in the screenshot will be 6 visitors. The output will be used in card visualization.

 

Struggling to find the best way to do this, any help or advise will be greatly appreciated!

 

*Take note that Visit 1 means the first visit of the day while Visit 2 means the 2nd visit for the day.

1 ACCEPTED SOLUTION

Hi,

Thank you for your feedback.

What i meant was, consider to insert one more condition that limits same-year-number into the measure.

It depends on how you define year number and week number.

Is it depends on ISO week number?

What happens if year changes during the week?

 

Just for simplifying the situation, I amended the measure a little bit.

Please check the below amended measure.

Based on your decision on how you define year number and week number, especially on the last week of the year or the first week of the year, the below measure has to be resived a little bit.

But I think the below measure will work rest of the weeks during the year.

 

I also fixed the measure in the attached pbix file.

 

 

Expected outcome measure: =
VAR currentyearnumber =
    YEAR ( TODAY () )
VAR week_column =
    FILTER (
        SUMMARIZE (
            ADDCOLUMNS (
                Data,
                "@weeknumber", WEEKNUM ( Data[Date], 21 ),
                "@yearnumber", YEAR ( Data[Date] )
            ),
            Data[Visitor ID],
            [@weeknumber],
            [@yearnumber]
        ),
        [@yearnumber] = currentyearnumber
    )
VAR countID =
    GROUPBY (
        week_column,
        [@weeknumber],
        [@yearnumber],
        "@count", SUMX ( CURRENTGROUP (), 1 )
    )
RETURN
    AVERAGEX ( countID, [@count] )

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

4 REPLIES 4
siewhun
Regular Visitor

Thanks for your solution! As you mentioned, by using WEEKNUM, it will cause a problem if I have data for May 2023. Do you mean replace WEEKNUM by something else?

Hi,

Thank you for your feedback.

What i meant was, consider to insert one more condition that limits same-year-number into the measure.

It depends on how you define year number and week number.

Is it depends on ISO week number?

What happens if year changes during the week?

 

Just for simplifying the situation, I amended the measure a little bit.

Please check the below amended measure.

Based on your decision on how you define year number and week number, especially on the last week of the year or the first week of the year, the below measure has to be resived a little bit.

But I think the below measure will work rest of the weeks during the year.

 

I also fixed the measure in the attached pbix file.

 

 

Expected outcome measure: =
VAR currentyearnumber =
    YEAR ( TODAY () )
VAR week_column =
    FILTER (
        SUMMARIZE (
            ADDCOLUMNS (
                Data,
                "@weeknumber", WEEKNUM ( Data[Date], 21 ),
                "@yearnumber", YEAR ( Data[Date] )
            ),
            Data[Visitor ID],
            [@weeknumber],
            [@yearnumber]
        ),
        [@yearnumber] = currentyearnumber
    )
VAR countID =
    GROUPBY (
        week_column,
        [@weeknumber],
        [@yearnumber],
        "@count", SUMX ( CURRENTGROUP (), 1 )
    )
RETURN
    AVERAGEX ( countID, [@count] )

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Thanks for your edited answer! I think the latest measure solved my concern.

Jihwan_Kim
Super User
Super User

Hi,

Please check the below measure and the attached pbix file.

Please be noted that if your real data is showing not just one year, the year number calculation also has to be considered in the measure.

 

Expected outcome measure: = 
VAR week_column =
    SUMMARIZE (
        ADDCOLUMNS ( Data, "@weeknumber", WEEKNUM ( Data[Date], 21) ),
        Data[Visitor ID],
        [@weeknumber]
    )
VAR countID =
    GROUPBY ( week_column, [@weeknumber], "@count", SUMX ( CURRENTGROUP (), 1 ) )
RETURN
    AVERAGEX ( countID, [@count] )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Helpful resources

Announcements
August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power BI Dev Camp Session 24 without aka link and time 768x460.jpg

Ted's Dev Camp - July 28, 2022

Watch Session 24 of Ted's Dev Camp along with past sessions!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors