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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Oleander
Helper I
Helper I

Calculate a value from a schedule for a specifi week

HI,

I have a schedule like this for between specific dates and I want to show values for a Department  for a specific week how do I go about a measure to do that ? 

I have Calender table with all dates and responding weeks that I want to pick the week from and get the Data from each department . 

DepDataStart dateEnd date
11122022-01-012022-04-17
11222022-01-022022-04-17
11352022-01-032022-04-17
11452022-01-042022-04-17
11572022-01-052022-04-17
11672022-01-062022-04-17
11742022-01-072022-04-17
11842022-01-082022-04-17
11922022-01-092022-04-17
12022022-01-102022-04-17
11122022-04-182022-05-03
11222022-04-182022-05-03
11342022-04-182022-05-03
11442022-04-182022-05-03
11562022-04-182022-05-03
11662022-04-182022-05-03
11742022-04-182022-05-03
11842022-04-182022-05-03
11922022-04-182022-05-03
12022022-04-182022-05-03
2 ACCEPTED SOLUTIONS

Hi @Oleander ,

 

Please add the following custom function to convert the date to ISO week number. 

Referencing: M functions to convert between ISO 8601 Week  

let
    getISO8601Week = (someDate as date) =>
        let
            getDayOfWeek = (d as date) =>
                let
                    result = 1 + Date.DayOfWeek(d, Day.Monday)
                in
                    result,

            getNaiveWeek = (inDate as date) =>
                let
                    // monday = 1, sunday = 7
                    weekday = getDayOfWeek(inDate),

                    weekdayOfJan4th = getDayOfWeek(#date(Date.Year(inDate), 1, 4)),

                    ordinal = Date.DayOfYear(inDate),

                    naiveWeek = Number.RoundDown(
                        (ordinal - weekday + 10) / 7
                    )
                in
                    naiveWeek,

            thisYear = Date.Year(someDate),

            priorYear = thisYear - 1,

            nwn = getNaiveWeek(someDate),

            lastWeekOfPriorYear =
                getNaiveWeek(#date(priorYear, 12, 28)),

            // http://stackoverflow.com/a/34092382/2014893
            lastWeekOfThisYear =
                getNaiveWeek(#date(thisYear, 12, 28)),

            weekNumber =
                if
                    nwn < 1
                then
                    lastWeekOfPriorYear
                else
                    if
                        nwn > lastWeekOfThisYear
                    then
                        1
                    else
                        nwn 
        in
            weekNumber
in
    getISO8601Week

vkkfmsft_2-1651800660241.png

 

Then modify the formula of the custom column.

 

WeekList = {DateToISOWeek([Start date])..DateToISOWeek([End date])}

vkkfmsft_1-1651800558500.png

 

Best Regards,
Winniz

View solution in original post

Hi,

This works though all dates must be in the same year.

 

//Göran

View solution in original post

7 REPLIES 7
Oleander
Helper I
Helper I

I would like to get as a result if I choose a week 16 :

For example :

Week 16 

Dep  Data

111   2

112   2

113   4

114   4

115   6

116   6

117   4

118   4

119   2

120   2

 

Hi @Oleander ,

 

1. If you want to use query parameter for filtering, then try the M code (in my calendar, week 17 is from 4/18/2022 to 4/24/2022):

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdBLCsAgDEXRvTiuYGL8raV0/9uombQ2PGlAB+pB5Z5nIKJwBNaZmGOiOZ6FRGrhOlSxVYxUnptlVRkpsUqQUtFWVZCqVlWkVMiqGlLdqo7UsCUGUJyMooTu+rafB++LReOh9juVP7/fKXEpTV1/VXWp5nqxu9TwlDDtgbpu", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Dep = _t, Data = _t, #"Start date" = _t, #"End date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Dep", Int64.Type}, {"Data", Int64.Type}, {"Start date", type date}, {"End date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "WeekList", each {Date.WeekOfYear([Start date], Day.Monday)..Date.WeekOfYear([End date], Day.Monday)}),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "IsContainSelectWeek", each List.Contains([WeekList], SelectWeekNum)),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([IsContainSelectWeek] = true))
in
    #"Filtered Rows"

vkkfmsft_1-1651734317410.png

 

2. If you want to filter by the slicer, then you can try to create the measure and display the values in the visual-level filter where the measure is not blank.

 

SelectRows = 
COUNTROWS (
    FILTER (
        'Table (2)',
        'Table (2)'[Start date] <= MAX ( Calender[Dates] )
            && 'Table (2)'[End date] >= MIN ( Calender[Dates] )
    )
)

vkkfmsft_2-1651734905449.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you so much, Winniz for your suggestion to a solution.

I will try this 

//Göran

Hi there !

I got half way due to that I can get the week nr from your solution but as you say and a occuring issue is that our Week numbering is not the same .

If I use the ISOWeeknum weeknum),21 for us I get the week 16 from 2022-04-18 to 2022-04-24 . If I can solve this week calculation I'll be a lot closer to a solution. 

Oleander_0-1651744622649.png

 

Hi @Oleander ,

 

Please add the following custom function to convert the date to ISO week number. 

Referencing: M functions to convert between ISO 8601 Week  

let
    getISO8601Week = (someDate as date) =>
        let
            getDayOfWeek = (d as date) =>
                let
                    result = 1 + Date.DayOfWeek(d, Day.Monday)
                in
                    result,

            getNaiveWeek = (inDate as date) =>
                let
                    // monday = 1, sunday = 7
                    weekday = getDayOfWeek(inDate),

                    weekdayOfJan4th = getDayOfWeek(#date(Date.Year(inDate), 1, 4)),

                    ordinal = Date.DayOfYear(inDate),

                    naiveWeek = Number.RoundDown(
                        (ordinal - weekday + 10) / 7
                    )
                in
                    naiveWeek,

            thisYear = Date.Year(someDate),

            priorYear = thisYear - 1,

            nwn = getNaiveWeek(someDate),

            lastWeekOfPriorYear =
                getNaiveWeek(#date(priorYear, 12, 28)),

            // http://stackoverflow.com/a/34092382/2014893
            lastWeekOfThisYear =
                getNaiveWeek(#date(thisYear, 12, 28)),

            weekNumber =
                if
                    nwn < 1
                then
                    lastWeekOfPriorYear
                else
                    if
                        nwn > lastWeekOfThisYear
                    then
                        1
                    else
                        nwn 
        in
            weekNumber
in
    getISO8601Week

vkkfmsft_2-1651800660241.png

 

Then modify the formula of the custom column.

 

WeekList = {DateToISOWeek([Start date])..DateToISOWeek([End date])}

vkkfmsft_1-1651800558500.png

 

Best Regards,
Winniz

Hi,

This works though all dates must be in the same year.

 

//Göran

Vijay_A_Verma
Super User
Super User

If you can also give the example of possible output, it will be easier for experts here to provide a solution to you. 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors