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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
slow_turtle03
Frequent Visitor

Previous 6 week totals by week

I need to calculate the previous totals from the past 6 weeks in a bar graph. Below is the code I have and I keep getting an error. 

 

Units PW =
VAR CurrentWeek = SELECTEDVALUE(TB_MDH_DATA_DAILY_MONTHLY[WeekNum])
VAR CurrentYear = SELECTEDVALUE(TB_MDH_DATA_DAILY_MONTHLY[DT].[Year])
VAR MaxWeekNumber = CALCULATE(MAX(TB_MDH_DATA_DAILY_MONTHLY[WeekNum]), ALL(TB_MDH_DATA_DAILY_MONTHLY[DT]))

RETURN
SUMX(
FILTER (ALL(TB_MDH_DATA_DAILY_MONTHLY[DT]),
IF(CurrentWeek = 1,
WEEKNUM = MaxWeekNumber && TB_MDH_DATA_DAILY_MONTHLY[DT] = CurrentYear - 1,
WEEKNUM = CurrentWeek -1 && TB_MDH_DATA_DAILY_MONTHLY[DT] = CurrentYear) )
[Unit Totals])
1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @slow_turtle03 ,

 

I think you can try to create a calendar table as below and then create a measure.

Calendar = 
VAR _BASIC =
    ADDCOLUMNS (
        CALENDARAUTO (),
        "Year", YEAR ( [Date] ),
        "Month", MONTH ( [Date] ),
        "Day", DAY ( [Date] ),
        "WeekDay", WEEKDAY ( [Date], 2 ),
        "WeekNum", WEEKNUM ( [Date], 2 )
    )
VAR _ADD =
    ADDCOLUMNS ( _BASIC, "WeekStart", [Date] - [WeekDay] + 1 )
RETURN
    _ADD

Measure:

Measure =
CALCULATE (
    SUM ( 'Table'[Value] ),
    FILTER (
        ALL ( 'Calendar' ),
        'Calendar'[WeekStart] < MAX ( 'Calendar'[WeekStart] )
            && 'Calendar'[WeekStart]
                >= MAX ( 'Calendar'[WeekStart] ) - 6 * 7
    )
)

 

Best Regards,
Rico Zhou

 

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

2 REPLIES 2
v-rzhou-msft
Community Support
Community Support

Hi @slow_turtle03 ,

 

I think you can try to create a calendar table as below and then create a measure.

Calendar = 
VAR _BASIC =
    ADDCOLUMNS (
        CALENDARAUTO (),
        "Year", YEAR ( [Date] ),
        "Month", MONTH ( [Date] ),
        "Day", DAY ( [Date] ),
        "WeekDay", WEEKDAY ( [Date], 2 ),
        "WeekNum", WEEKNUM ( [Date], 2 )
    )
VAR _ADD =
    ADDCOLUMNS ( _BASIC, "WeekStart", [Date] - [WeekDay] + 1 )
RETURN
    _ADD

Measure:

Measure =
CALCULATE (
    SUM ( 'Table'[Value] ),
    FILTER (
        ALL ( 'Calendar' ),
        'Calendar'[WeekStart] < MAX ( 'Calendar'[WeekStart] )
            && 'Calendar'[WeekStart]
                >= MAX ( 'Calendar'[WeekStart] ) - 6 * 7
    )
)

 

Best Regards,
Rico Zhou

 

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

VahidDM
Super User
Super User

HI @slow_turtle03 

 

Check this:
https://www.vahiddm.com/post/weekly-time-intelligence-dax

 

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

Appreciate your Kudos!! 

Badges.jpg

LinkedIn | Twitter | Blog | YouTube 

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

Check out the October 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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