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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Column flag with DAX

Hello,

I am trying to create a simple flag column using DAX and I am having trouble getting the result that I want. Here is the scenario:

 

In my source dataset I have initiativeID's that are related to a specific week, and a flag for records in the current week. I would like to add a flag for records that are in the previous week. I've been trying to work this out in DAX with no success. I feel like what I have should work but for some reason it doesnt. My thought process was to look at each ID, and if it is exactly 1 less than the ID of the current week, then flag those records as 1. Otherwise flag as 0. If I pull out just the part where I am subtracting 1 and put that into a separate card, I get the correct value of 10071. Both the ID and previous week flag are currently set to whole number types. Below is a screenshot that highlights exactly which records I want to be flagged as 1.

 

isPreviousWeek =
IF (
    InitiativeOverview_Dimension[initiativeid]
        = CALCULATE (
            DISTINCT ( InitiativeOverview_Dimension[initiativeid] ),
            InitiativeOverview_Dimension[isCurrentWeek] = 1
        ) - 1,
    1,
    0
)

 

DAX Flag.PNG

Any ideas?

Thanks!

6 REPLIES 6
Anonymous
Not applicable

Some variables and the use of MAX might be the solution you're looking for.

 

Something to this effect:

isPreviousWeek = 
VAR previous_id = CALCULATE(
MAX(InitiativeOverview_Dimension[initiativeid]),
ALL(InitiativeOverview_Dimension[initiativeid])) - 1
RETURN
IF ( 
SELECTEDVALUE(InitiativeOverview_Dimension[initiativeid]) = previous_id,
1, 0 )
Anonymous
Not applicable

Thanks! I will check out that file.

 

@Anonymous Thank you for the suggesstion and trying to help me work through it. Unfortunately the upcoming week is loaded towards the end of each week. Therefore the MAX ID will change and the current week temporarily does not have the MAX ID.

@Anonymous ,

 

In case the file did not help you let me know

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
Anonymous
Not applicable

Hey @amitchandak 

 

Unfortunately my organization blocks dropbox so I'm not able to download the file.

 

Even if that works, I would really like to know why my DAX statement above does not work. I'm trying to get a deeper understanding of the language. To me, what I have makes sense.

 

 

 

Anonymous
Not applicable

Just wanted to try one more time to see if anyone knows why this statement does not provide the expected result described in my initial post:

 

isPreviousWeek =
IF (
    InitiativeOverview_Dimension[initiativeid]
        = CALCULATE (
            DISTINCT ( InitiativeOverview_Dimension[initiativeid] ),
            InitiativeOverview_Dimension[isCurrentWeek] = 1
        ) - 1,
    1,
    0
)

 

Thanks!

amitchandak
Super User
Super User

Refer this file. I created a calendar table and created week wise rank. Your last week max rank -1. Calculated this week vs last week too

https://www.dropbox.com/s/d9898a48e76wmvl/sales_analytics_weekWise.pbix?dl=0

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
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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