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

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

Reply
s1
Helper I
Helper I

More efficient method to check last 4 rows of data in DAX for calculated column

Hello,

 

I guess my question is:

  • Is my DAX code written in an obviously inefficient way?
  • If yes, is there a better way to do it?

 

I created a DAX calculated column to flag in which weeks a company is "Engaged", which is defined as:

  • The company has performed at least 1 action per week
  • For at least 3 of the last 4 weeks

I wrote the code below to achieve this. However, it seems to be incredibly slow. Before creating this column, it takes 1 second to add a new calculated column to the Company Events table. After creating this column, it takes about 30 seconds to do the same thing. The table is currently 14,000 rows, but I have example data below.

 

 

 

 

Engaged = 
VAR company = 'Company Events'[Company Name]
VAR week = 'Company Events'[Weeks since Purchase]
RETURN
IF(
    CALCULATE(
        DISTINCTCOUNT('Company Events'[Weeks since Purchase]),
        ALL('Company Events'),
        'Company Events'[Company Name] = company,
        'Company Events'[Weeks since Purchase] <= week,
        'Company Events'[Weeks since Purchase] >= week - 3,
        'Company Events'[Actions] > 0
    ) >= 3,
    1,
    0
)

 

 

 

 

Example table with desired result in blue.

Company Name  Date (week)  Weeks since Purchase  Actions  Engaged  
Company A2022-09-041550
Company A2022-09-111600
Company A2022-09-181730
Company A2022-09-2518121
Company A2022-10-021900
Company A2022-10-092000
Company B2022-09-04320
Company B2022-09-11430
Company B2022-09-18521
Company B2022-09-25641
Company B2022-10-02701
Company B2022-10-09800
Company C2022-09-180110
Company C2022-09-25170
Company C2022-10-02291
Company C2022-10-09321

 

Thanks in advance!

 

additional notes:

I need this to be a calculated column and not a measure, because I am building several other calculated columns off of it (in a related company table).

I also cannot easily do this in Power Query, because "Actions" is a calculated column that relies on several relationships to calculate.

1 ACCEPTED SOLUTION

Sorry, the suggestions didn't help resolve the problem. I ended up having to move the work into PowerQuery to get my performance back. (I suspect there may have been a circular reference issue with calculated columns between tables)
Should I mark my reply to you here as the solution, so that it isn't sitting as open?

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi  @s1 ,

Whether your problem has been resolved? If yes, could you please mark the helpful post as Answered? It will help the others in the community find the solution easily if they face the same problem as yours. Thank you.

Best Regards

Sorry, the suggestions didn't help resolve the problem. I ended up having to move the work into PowerQuery to get my performance back. (I suspect there may have been a circular reference issue with calculated columns between tables)
Should I mark my reply to you here as the solution, so that it isn't sitting as open?

Anonymous
Not applicable

Hi @s1 ,

Thanks for your reply. Yes, you can mark your reply as the solution if your problem has been resolved. Thank you.

Best Regards

Anonymous
Not applicable

Hi @s1 ,

Please update the formula of the calculated column [Engaged] as below and check if it run faster...

Engaged =
VAR company = 'Company Events'[Company Name]
VAR week = 'Company Events'[Weeks since Purchase]
VAR wpcount =
    CALCULATE (
        DISTINCTCOUNT ( 'Company Events'[Weeks since Purchase] ),
        'Company Events',
        'Company Events'[Company Name] = company,
        'Company Events'[Weeks since Purchase] <= week,
        'Company Events'[Weeks since Purchase] >= week - 3,
        'Company Events'[Actions] > 0
    )
RETURN
    IF ( wpcount >= 3, 1, 0 )

In addition, you can refer the following blog to optimize your DAX formula.

Improve Power BI Performance by Optimizing your DAX

Best Regards

amitchandak
Super User
Super User

@s1 , better to use Date table and week rank

 

new columns
Week Start date = 'Date'[Date]+-1*WEEKDAY('Date'[Date],2)+1
Week End date = 'Date'[Date]+ 7-1*WEEKDAY('Date'[Date],2)
Week Rank = RANKX(all('Date'),'Date'[Week Start date],,ASC,Dense)
OR
Week Rank RANKX(all('Date'),'Date'[Year Week],,ASC,Dense) //YYYYWW format

 

measures

Last 4 weeks = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-4 && 'Date'[Week Rank]<=max('Date'[Week Rank])))
This Week = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])))
Last Week = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])-1))
Last year Week= CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]=(max('Date'[Week Rank]) -52)))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
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.

Top Kudoed Authors