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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
MikkoL
Frequent Visitor

Week needs data from previous week - problem with filter context?

Hello,

I need to fetch last weeks Sales Orders (SO) in order to calculate current week correctly. I have managed to get last week SOs in cases when I have a SO for an item in the current week, but if there is no SO for a particular item in current week no SO data from last week is fetched๐Ÿ˜… I use a MS timeline to select the weeks.

 

For me this feels like I don't access the last week items in the selected weeks context.... (even I can access last weeks SOs)

Here is one version of a measure (note that I need to bind the SO table item to part table in order to get item level data):

PWeekSOs =
VAR LW = WEEKNUM(AVERAGE(OpenSalesRels[ReqDate])) - 1
return
CALCULATE([SOReq Qty],  
    FILTER(ALL(OpenSalesRels),WEEKNUM(OpenSalesRels[ReqDate])=LW &&  OpenSalesRels[CompPart] = MIN(Part[CompPart]))
)
 
MikkoL_0-1740485892689.png

 

 

 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi,

Thanks for the solution @bhanu_gautam  offered, and i want to offer some more information for user to refer to.

hello @MikkoL , you can refer to the following sample.

Sample data 

vxinruzhumsft_0-1740538617612.png

Sample  measure

Sum = SUM('Table'[Column2])

Create a new table

Table 2 = SUMMARIZE('Table',[Year],[Weeknum],[Year-Week])

Then create the following measures.

Currentsales =
VAR a =
    CALCULATE (
        SUM ( 'Table'[Column2] ),
        FILTER ( 'Table', 'Table'[Year-Week] IN VALUES ( 'Table 2'[Year-Week] ) )
    )
RETURN
    a
LastSales =
CALCULATE (
    [Sum],
    FILTER (
        'Table',
        [Weeknum]
            = SELECTEDVALUE ( 'Table 2'[Weeknum] ) - 1
            && 'Table'[Year] IN VALUES ( 'Table 2'[Year] )
    )
)

Then put the following field to the matrix.

vxinruzhumsft_1-1740538795608.png

 

Output

vxinruzhumsft_2-1740538818898.png

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Thank you @Anonymous and @bhanu_gautam for your great help.
Actually here is a solution that works and it is a combination from both of your answers:
BR, Mikko

PWeekSOs2 =
  CALCULATE (
    OpenSalesRels[SOReq Qty],
    FILTER (
        ALL(OpenSalesRels),
        OpenSalesRels[Weeknum] = SELECTEDVALUE ( FilterDates[Weeknum] ) - 1
        && OpenSalesRels[Year] = SELECTEDVALUE( ( FilterDates[Year] )
    )
),
VALUES(Part[CompPart])
)
Preview
 
 
 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi,

Thanks for the solution @bhanu_gautam  offered, and i want to offer some more information for user to refer to.

hello @MikkoL , you can refer to the following sample.

Sample data 

vxinruzhumsft_0-1740538617612.png

Sample  measure

Sum = SUM('Table'[Column2])

Create a new table

Table 2 = SUMMARIZE('Table',[Year],[Weeknum],[Year-Week])

Then create the following measures.

Currentsales =
VAR a =
    CALCULATE (
        SUM ( 'Table'[Column2] ),
        FILTER ( 'Table', 'Table'[Year-Week] IN VALUES ( 'Table 2'[Year-Week] ) )
    )
RETURN
    a
LastSales =
CALCULATE (
    [Sum],
    FILTER (
        'Table',
        [Weeknum]
            = SELECTEDVALUE ( 'Table 2'[Weeknum] ) - 1
            && 'Table'[Year] IN VALUES ( 'Table 2'[Year] )
    )
)

Then put the following field to the matrix.

vxinruzhumsft_1-1740538795608.png

 

Output

vxinruzhumsft_2-1740538818898.png

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you @Anonymous and @bhanu_gautam for your great help.
Actually here is a solution that works and it is a combination from both of your answers:
BR, Mikko

PWeekSOs2 =
  CALCULATE (
    OpenSalesRels[SOReq Qty],
    FILTER (
        ALL(OpenSalesRels),
        OpenSalesRels[Weeknum] = SELECTEDVALUE ( FilterDates[Weeknum] ) - 1
        && OpenSalesRels[Year] = SELECTEDVALUE( ( FilterDates[Year] )
    )
),
VALUES(Part[CompPart])
)
Preview
 
 
 
bhanu_gautam
Super User
Super User

@MikkoL , Try using

DAX
PWeekSOs =
VAR CurrentWeek = WEEKNUM(AVERAGE(OpenSalesRels[ReqDate]))
VAR LastWeek = CurrentWeek - 1
RETURN
CALCULATE(
[SOReq Qty],
FILTER(
ALL(OpenSalesRels),
WEEKNUM(OpenSalesRels[ReqDate]) = LastWeek
),
VALUES(Part[CompPart])
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Hello Bhanu,

That was super fast reply๐Ÿ‘thank you! Unfortunately I got the same answer as with my (now yours is named PWeekSOs4):

MikkoL_0-1740487011960.png

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon โ€“ Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save โ‚ฌ200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.