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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
hperalta
Regular Visitor

good challenge - build Mesure from dax query

have a table with this structure:
column1: Website_pageview_id (int column)
column2: created_at (date column)
column3: time (time column)
column4: Website_session_id
column5: pageview_url

Screenshot 2023-09-05 122247.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 



so Website_session_id 17562 has 6 pages associated, each with a unique Website_pageview_id. calculating the minimum Website_pageview_id  associated with 1 Website_session_id you get the first visited page by Website_session_id.

Using Dax Studio I'am able to calculate how many times a specific page was used / visited as the first page.

 

EVALUATE
VAR firstpage =
    SELECTCOLUMNS (
        ADDCOLUMNS (
            SUMMARIZE (
                website_sessions,
                website_sessions[website_session_id]
            ),
            "firstpagecode",
                CALCULATE (
                    MINX (
                        website_sessions,
                        website_sessions[website_pageview_id]
                    )
                )
        ),
        [firstpagecode]
    )
VAR filtersessions =
    CALCULATETABLE (
        website_sessions,
        TREATAS (
            firstpage,
            website_sessions[website_pageview_id]
        )
    )
RETURN
    GROUPBY (
        filtersessions,
        website_sessions[pageview_url],
        "hits",
            SUMX (
                CURRENTGROUP (),
                1
            )
    )

 

 

and i get this result:

Screenshot 2023-09-05 123205.png

 

On PowerBi desktop I built a measure from the query:

N times page visited 1st time = 
VAR currentpagess = MAX(website_sessions[pageview_url])

VAR firstpage =
    SELECTCOLUMNS (
        ADDCOLUMNS (
            SUMMARIZE (
                website_sessions,
                website_sessions[website_session_id]
            ),
            "firstpagecode",
                CALCULATE (
                    MINX (
                        website_sessions,
                        website_sessions[website_pageview_id]
                    )
                )
        ),
        [firstpagecode]
    )
VAR filtersessions =
    CALCULATETABLE (
        website_sessions,
        TREATAS (
            firstpage,
            website_sessions[website_pageview_id]
        )
    )

Var Result = 
CALCULATE(
SUMX(
    FILTER(filtersessions,
    website_sessions[pageview_url] = currentpagess
    )  
    , 1)
)

Var Result2 = 
COUNTROWS( filtersessions )


Return
Result2





I get this result Using pageview_url as dimension (in the code before i can use result or result2 the final is always the same):

Screenshot last.jpg

 

 

Knowing that 

N time pages visited = DISTINCTCOUNT(website_sessions[website_pageview_id] ).
/biling-2; /shipping /billing etc - were never the first page visited

i m not able to build a measure that has the same behavior as the query.
I don't understand why I'm getting the wrong results. it looks like the filter context is not being passed to one or all the variables 


Can someone help me on this one.
i leave here the file if someone is interested 
Download File 

 

1 ACCEPTED SOLUTION
AlbertoFerrari
Most Valuable Professional
Most Valuable Professional

A typical scenario where you need ALL / VALUES, to ignore part of the filter context.
This should work:

N times page visited 1st time  2 = 
VAR currentpagess = MAX(website_sessions[pageview_url])

VAR firstpage =
    CALCULATETABLE ( 
        SELECTCOLUMNS (
            ADDCOLUMNS (
                SUMMARIZE (
                    website_sessions,
                    website_sessions[website_session_id]
                ),
                "firstpagecode",
                    CALCULATE (
                        MINX (
                            website_sessions,
                            website_sessions[website_pageview_id]
                        )
                    )
            ),
            [firstpagecode]
        ),
        ALL ( website_sessions ),
        VALUES ( website_sessions[website_session_id] )
    )
VAR filtersessions =
    CALCULATETABLE (
        website_sessions,
        TREATAS (
            firstpage,
            website_sessions[website_pageview_id]
        )
    )

Var Result = 
CALCULATE(
SUMX(
    FILTER(filtersessions,
    website_sessions[pageview_url] = currentpagess
    )  
    , 1)
)

Var Result2 = 
COUNTROWS( filtersessions )

Return
Result2

You might need to replace ALL with something less strong, this depends on your requirements.
Enjoy DAX

Alberto Ferrari - SQLBI

View solution in original post

3 REPLIES 3
AlbertoFerrari
Most Valuable Professional
Most Valuable Professional

A typical scenario where you need ALL / VALUES, to ignore part of the filter context.
This should work:

N times page visited 1st time  2 = 
VAR currentpagess = MAX(website_sessions[pageview_url])

VAR firstpage =
    CALCULATETABLE ( 
        SELECTCOLUMNS (
            ADDCOLUMNS (
                SUMMARIZE (
                    website_sessions,
                    website_sessions[website_session_id]
                ),
                "firstpagecode",
                    CALCULATE (
                        MINX (
                            website_sessions,
                            website_sessions[website_pageview_id]
                        )
                    )
            ),
            [firstpagecode]
        ),
        ALL ( website_sessions ),
        VALUES ( website_sessions[website_session_id] )
    )
VAR filtersessions =
    CALCULATETABLE (
        website_sessions,
        TREATAS (
            firstpage,
            website_sessions[website_pageview_id]
        )
    )

Var Result = 
CALCULATE(
SUMX(
    FILTER(filtersessions,
    website_sessions[pageview_url] = currentpagess
    )  
    , 1)
)

Var Result2 = 
COUNTROWS( filtersessions )

Return
Result2

You might need to replace ALL with something less strong, this depends on your requirements.
Enjoy DAX

Alberto Ferrari - SQLBI
MFelix
Super User
Super User

Hi @hperalta,

 

This is related with context, believe that there is an easier way to solve this. Create a column on your model that gives you the order of the pages so in this case I added the following column to my the model:

 

Page View Rank =
RANK (
    DENSE,
    ALL (
        website_sessions[website_pageview_id],
        website_sessions[website_session_id]
    ),
    ORDERBY ( website_sessions[website_pageview_id], ASC ),
    ,
    PARTITIONBY ( website_sessions[website_session_id] )
)

 

MFelix_0-1693931440518.png

Now add the following measure:

 

Total Views first page = 
COUNTROWS( FILTER(website_sessions, website_sessions[Page View Rank] = 1))

 

Result below and in attach file:

MFelix_1-1693931494316.png

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Bom Dia M Felix,

Muito obrigado for your answer. you are indeed correct that is a viable solution to achieve the correct numbers. 
i must admit that i was (still am) so focus in building the measure that did no look other solutions. And
in the end the countrows filter solution or calculate 

agora sim = 
CALCULATE(
    COUNTROWS( mvf_website_pageviews ),
    mvf_website_pageviews[Ranking] = 1
)

is the same. actually a small change in the data model made a huge difference on the measure.
thanks for you idea and time spend.
ill still wait for another measure solution (if it arrives )... if i don't have any  more answers I'll mark you answer as solution... 



Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors