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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Amanda24
Regular Visitor

Calculated column with SWITCH conditions and current week vs previous week comparisons

Hello,

I really need help with calculated column results as it does not give correct results.

I need to compare quantity with the current week vs previous week or comparison week after another week.

My current code:

comparison_test =
VAR CurrentWeekQnty = CALCULATE(SUM('Table'[qnty]),FILTER(ALL('Calendar'),'Calendar'[Week Rank]=max('Calendar'[Week Rank])))
VAR PreviousWeekQnty = CALCULATE(SUM( 'Table'[qnty]), FILTER(ALL('Calendar'),'Calendar'[Week Rank]=max('Calendar'[Week Rank])-1))
RETURN
    SWITCH(
        TRUE(),
        CurrentWeekQnty = PreviousWeekQnty, "Quantity didn't change",
        CurrentWeekQnty > PreviousWeekQnty, "Increased",
        CurrentWeekQnty < PreviousWeekQnty, "Decreased",
       
        "New Material"
    )
 
week rank calculated column code:
Week Rank = RANKX(all('Calendar'),'Calendar'[Week],,ASC,Dense)
 
The example data:
MaterialRecord DateQntyCurrent code ouputDesired code output
Material 1 1.8.202316Quantity didn't changeNew Material
Material 1 8.8.202316Quantity didn't changeQuantity didn't change
Material 1 14.8.202316Quantity didn't changeQuantity didn't change
Material 1 22.8.202316DecreasedQuantity didn't change
Material 1 28.8.202316IncreasedQuantity didn't change
Material 2 1.8.202310Quantity didn't changeNew Material
Material 2 8.8.202314Quantity didn't changeIncreased
Material 2 14.8.202314Quantity didn't changeQuantity didn't change
Material 2 22.8.202314DecreasedQuantity didn't change
Material 2 28.8.202314IncreasedQuantity didn't change
2 ACCEPTED SOLUTIONS
lbendlin
Super User
Super User

You need to explicitly check PreviousWeekQnty against BLANK(), and move the "New Material"  step up for that.

 

RETURN
    SWITCH(
        TRUE(),
        ISBLANK(PreviousWeekQnty),"New Material",
        CurrentWeekQnty = PreviousWeekQnty, "Quantity didn't change",
        CurrentWeekQnty > PreviousWeekQnty, "Increased",
         "Decreased"
    )

View solution in original post

v-zhangti
Community Support
Community Support

Hi, @Amanda24 

 

You can try the following methods.

Column:

Weeknum = WEEKNUM([Record Date],2)

vzhangti_0-1694517103237.png

comparison_test = 
VAR _CurrentWeekQnty = SUM('Table'[Qnty])
VAR _PreviousWeekQnty = CALCULATE(SUM( 'Table'[qnty]), FILTER(ALL('Table'),[Weeknum]=SELECTEDVALUE('Table'[Weeknum])-1&&[Material]=SELECTEDVALUE('Table'[Material])))
VAR _N=SWITCH(
        TRUE(),
        _PreviousWeekQnty=BLANK(),"New Material",
        _CurrentWeekQnty = _PreviousWeekQnty, "Quantity didn't change",
        _CurrentWeekQnty > _PreviousWeekQnty, "Increased",
        _CurrentWeekQnty < _PreviousWeekQnty, "Decreased"
    )
Return
_N

vzhangti_1-1694517150068.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

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-zhangti
Community Support
Community Support

Hi, @Amanda24 

 

You can try the following methods.

Column:

Weeknum = WEEKNUM([Record Date],2)

vzhangti_0-1694517103237.png

comparison_test = 
VAR _CurrentWeekQnty = SUM('Table'[Qnty])
VAR _PreviousWeekQnty = CALCULATE(SUM( 'Table'[qnty]), FILTER(ALL('Table'),[Weeknum]=SELECTEDVALUE('Table'[Weeknum])-1&&[Material]=SELECTEDVALUE('Table'[Material])))
VAR _N=SWITCH(
        TRUE(),
        _PreviousWeekQnty=BLANK(),"New Material",
        _CurrentWeekQnty = _PreviousWeekQnty, "Quantity didn't change",
        _CurrentWeekQnty > _PreviousWeekQnty, "Increased",
        _CurrentWeekQnty < _PreviousWeekQnty, "Decreased"
    )
Return
_N

vzhangti_1-1694517150068.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

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

lbendlin
Super User
Super User

You need to explicitly check PreviousWeekQnty against BLANK(), and move the "New Material"  step up for that.

 

RETURN
    SWITCH(
        TRUE(),
        ISBLANK(PreviousWeekQnty),"New Material",
        CurrentWeekQnty = PreviousWeekQnty, "Quantity didn't change",
        CurrentWeekQnty > PreviousWeekQnty, "Increased",
         "Decreased"
    )

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.