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
September Update

Check it Out!

Click here to learn more about the September 2022 updates!

Power BI Show Episode 10 Recap

The Power BI Community Show

Watch the playback when Amit Chandak, a Power BI Super User, demos how to use Field Parameters to make reports more dynamic.

Power BI Dev Camp Session 26

New Date - Check it Out!

Mark your calendars and join us on Thursday, October 6 at 11a PDT for a great session with Ted Pattison!

Health and Life Sciences Power BI User Group

Health and Life Sciences Power BI User Group

Power BI specialists at Microsoft have created a community user group where customers in the provider, payor, pharma, health solutions, and life science industries can collaborate.

Top Solution Authors
Top Kudoed Authors