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
l_vitorl
Regular Visitor

Create measure of max day with data grouped by week

Hi! I'm currently having trouble trying to create a measure that would give me the maximum date of a given week where a table has data entries.

 

Here's a sample of the data

 

Num vehic date  weekday
4302/02/2023  5
13503/02/2023  6
25804/02/2023  7
8605/02/2023  1
24506/02/2023  2
20607/02/2023  3
14808/02/2023  4
7709/02/2023  5
16113/02/2023  2
25714/02/2023  3
615/02/2023  4
3316/02/2023  5
25217/02/2023  6
22119/02/2023  1
3420/02/2023  2
16121/02/2023  3

 

As you can see, some weeks have data entries on all seven days, while others don't. My expected result is something like this

 

Num vehicdate  weekdaymax_dayofweek_with_data
4302/02/2023  504/02/2023
13503/02/2023  604/02/2023
25804/02/2023  704/02/2023
8605/02/2023  109/02/2023
24506/02/2023  209/02/2023
20607/02/2023  309/02/2023
14808/02/2023  409/02/2023
7709/02/2023  509/02/2023
16113/02/2023  217/02/2023
25714/02/2023  317/02/2023
615/02/2023  417/02/2023
3316/02/2023  517/02/2023
25217/02/2023  617/02/2023
22119/02/2023  121/02/2023
3420/02/2023  221/02/2023
16121/02/2023  321/02/2023

 

I've actually solved my problem by creating a summarized related table, but in my opinion its rather "ugly" so I was wondering if a more simple and elegant solution with a measure could be achieved. Any ideas?

 

Thanks in advance

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You'll need a week commencing column in your date table, and you'll need to use the date column from the date table in your visual, not the date column from the fact table. Then you can create a measure like

Max day of week with data =
VAR CurrentWeek =
    SELECTEDVALUE ( 'Date'[Week commencing] )
VAR MaxDateWithData =
    CALCULATE (
        MAX ( 'Fact Table'[Date] ),
        REMOVEFILTERS ( 'Date' ),
        'Date'[Week commencing] = CurrentWeek
    )
RETURN
    MaxDateWithData

View solution in original post

2 REPLIES 2
johnt75
Super User
Super User

You'll need a week commencing column in your date table, and you'll need to use the date column from the date table in your visual, not the date column from the fact table. Then you can create a measure like

Max day of week with data =
VAR CurrentWeek =
    SELECTEDVALUE ( 'Date'[Week commencing] )
VAR MaxDateWithData =
    CALCULATE (
        MAX ( 'Fact Table'[Date] ),
        REMOVEFILTERS ( 'Date' ),
        'Date'[Week commencing] = CurrentWeek
    )
RETURN
    MaxDateWithData

Thanks!

 

Had to adapt a few things to account for other columns that needed to be filtered in the visual (which I ommited in the sample data), but it worked.

 

For some reason the REMOVEFILTERS wasn't needed, not sure why. I will test a few scenarios to check for some unexpected behaviour but all in all it worked.

 

Here's the adapted formula, in case someone needs

 

maxdaywithdata =

VAR numweekyear =
WEEKNUM(fact_vehicle_dates[date])

VAR client =
fact_vehicle_dates[CD_CLIENT]

VAR MaxDayOnWeekNum =
    CALCULATE (
        MAX(fact_vehicle_dates[weekday]),
        FILTER( fact_vehicle_dates, WEEKNUM(fact_vehicle_dates[date]) = numweekyear),
        FILTER( fact_vehicle_dates, fact_vehicle_dates[CD_CLIENT] = client)
    )
RETURN
    MaxDayOnWeekNum

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.