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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
kb26w
Frequent Visitor

Aggregating over date in a measure

Hello,

 

I want to build a measure which shows the number of days people login to their account per month, quarter, year. 

 

The data is in this format - with user ID, login date. I then create end of month and end of quarter in query view and create "Days with a login (month)" and "Days with a login (quarter) as calculated columns using the DAX below. 

 

kb26w_2-1734692322904.jpeg

 

 

Days with a login (month) =

CALCULATE (

    DISTINCTCOUNT ( 'Login activity'[Login date] ),

    ALLEXCEPT (

        'Login activity',

        'Login activity'[User Id],

        'Login activity'[End of Month]

    )

)

Days with a login (quarter) =

CALCULATE (

    DISTINCTCOUNT ( 'Login activity'[Login date] ),

    ALLEXCEPT (

        'Login activity',

        'Login activity'[User Id],

        'Login activity'[End of Quarter]

    )

)

I then do a distinct count of user ID to build the visuals below. 

kb26w_3-1734692322906.jpeg

 

 

Can someone help me with the dax to build the measures which allow me to build the above visuals but without the need for multiple visuals and calculated columns? 

 

So I want to use the dates hierarchy from my dates table on the x-axis (so I can drill up and down) and then build measures for number of people who have logged in on 1 day, 2 days, 3 days etc. So I will have multiple measures which work using my dates table and no calculated columns. 

 

Thank you in advance 🙂

2 ACCEPTED SOLUTIONS
DataNinja777
Super User
Super User

Hi @kb26w ,

 

If there is a direct relationship between the Login activity table and the Calendar table on the Date column, you can calculate the number of days users log in at various levels of the date hierarchy (month, quarter, year) directly using DAX measures.

To calculate the total number of distinct login days dynamically, you can use the following measure:

Total Login Days = 
CALCULATE(
    DISTINCTCOUNT('Login activity'[Login date])
)

This measure leverages the direct relationship to apply filters from the Calendar table’s date hierarchy to the Login activity table. For instance, if you place the year, quarter, or month from the Calendar table on the x-axis of a visual, this measure will automatically aggregate login days for the selected level.

If you want to calculate how many users logged in on a specific number of days during a selected period, you can use a measure like this:

Users by Login Days = 
VAR LoginDaysSummary =
    SUMMARIZE(
        'Login activity',
        'Login activity'[User ID],
        "LoginDays", DISTINCTCOUNT('Login activity'[Login date])
    )
RETURN
    COUNTROWS(
        FILTER(
            LoginDaysSummary,
            [LoginDays] > 0 -- Adjust this condition as needed for specific ranges
        )
    )

This measure dynamically groups users by the number of days they logged in and counts the users who meet the specified condition. Using these measures with the Calendar table’s hierarchy allows you to create flexible visuals that can drill through different time periods (e.g., month, quarter, year).

The direct relationship between the Calendar and Login activity tables ensures that filters from the date hierarchy are seamlessly applied, making the calculations dynamic and adaptable without unnecessary complexity. This approach avoids the need for calculated columns and enables you to use a single visual to display the required insights at different levels of granularity.

 

Best regards,

View solution in original post

Jihwan_Kim
Super User
Super User

Hi,

I tried to create a sample pbix file like below, and please check the below picture and the attached pbix file.

In this sample I created a login_count dimension table by writing DAX formula like below.

 

Jihwan_Kim_1-1735134951641.png

 

 

Jihwan_Kim_0-1735134856986.png

 

 

login_count = 
VAR _maxcount =
    MAXX (
        ADDCOLUMNS (
            VALUES ( login[user_id] ),
            "@logincount", CALCULATE ( COUNTROWS ( login ) )
        ),
        [@logincount]
    )
RETURN
    SELECTCOLUMNS ( GENERATESERIES ( 1, _maxcount, 1 ), "Count", [Value] )

 

 

And then, create a measure and put it into the visualization.

ID count by login count: = 
VAR _logincountlegend =
    MAX ( login_count[Count] )
VAR _logincount =
    ADDCOLUMNS (
        VALUES ( login[user_id] ),
        "@logincount", CALCULATE ( COUNTROWS ( login ) )
    )
RETURN
    COUNTROWS ( FILTER ( _logincount, [@logincount] = _logincountlegend ) )

 


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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

2 REPLIES 2
Jihwan_Kim
Super User
Super User

Hi,

I tried to create a sample pbix file like below, and please check the below picture and the attached pbix file.

In this sample I created a login_count dimension table by writing DAX formula like below.

 

Jihwan_Kim_1-1735134951641.png

 

 

Jihwan_Kim_0-1735134856986.png

 

 

login_count = 
VAR _maxcount =
    MAXX (
        ADDCOLUMNS (
            VALUES ( login[user_id] ),
            "@logincount", CALCULATE ( COUNTROWS ( login ) )
        ),
        [@logincount]
    )
RETURN
    SELECTCOLUMNS ( GENERATESERIES ( 1, _maxcount, 1 ), "Count", [Value] )

 

 

And then, create a measure and put it into the visualization.

ID count by login count: = 
VAR _logincountlegend =
    MAX ( login_count[Count] )
VAR _logincount =
    ADDCOLUMNS (
        VALUES ( login[user_id] ),
        "@logincount", CALCULATE ( COUNTROWS ( login ) )
    )
RETURN
    COUNTROWS ( FILTER ( _logincount, [@logincount] = _logincountlegend ) )

 


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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
DataNinja777
Super User
Super User

Hi @kb26w ,

 

If there is a direct relationship between the Login activity table and the Calendar table on the Date column, you can calculate the number of days users log in at various levels of the date hierarchy (month, quarter, year) directly using DAX measures.

To calculate the total number of distinct login days dynamically, you can use the following measure:

Total Login Days = 
CALCULATE(
    DISTINCTCOUNT('Login activity'[Login date])
)

This measure leverages the direct relationship to apply filters from the Calendar table’s date hierarchy to the Login activity table. For instance, if you place the year, quarter, or month from the Calendar table on the x-axis of a visual, this measure will automatically aggregate login days for the selected level.

If you want to calculate how many users logged in on a specific number of days during a selected period, you can use a measure like this:

Users by Login Days = 
VAR LoginDaysSummary =
    SUMMARIZE(
        'Login activity',
        'Login activity'[User ID],
        "LoginDays", DISTINCTCOUNT('Login activity'[Login date])
    )
RETURN
    COUNTROWS(
        FILTER(
            LoginDaysSummary,
            [LoginDays] > 0 -- Adjust this condition as needed for specific ranges
        )
    )

This measure dynamically groups users by the number of days they logged in and counts the users who meet the specified condition. Using these measures with the Calendar table’s hierarchy allows you to create flexible visuals that can drill through different time periods (e.g., month, quarter, year).

The direct relationship between the Calendar and Login activity tables ensures that filters from the date hierarchy are seamlessly applied, making the calculations dynamic and adaptable without unnecessary complexity. This approach avoids the need for calculated columns and enables you to use a single visual to display the required insights at different levels of granularity.

 

Best regards,

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.