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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
calcstuff
Frequent Visitor

Rolling Window Conditional Checks

I've got Bill and Melinda who've taken food safety classes:

 

PersonFood Safety Class Date
Bill3/18/2020
Melinda    4/15/2020

 

Once you've taken a food safety class you're qualified to perform food safety checks (FSC) for the next year. After that first 365 days is up you have to continuously perform at least 2 food safety checks for the previous 1 year period to maintain your food safety certification, and be able to perform more food safety checks. Here's the log of food safety checks:

 

PersonFood Safety Check   FSC ID
Bill5/18/2020FSC005
Bill5/30/2020FSC154
Bill1/14/2021FSC247
Bill2/25/2023FSC313
Melinda    6/15/2020FSC005
Melinda7/14/2020FSC154
Melinda2/28/2021FSC247
Melinda3/15/2021FSC313
Melinda3/29/2022FSC399
Melinda3/30/2022FSC502

 

In Power BI DAX how can I determine which food safety checks were valid and which were not? The expected output would look like this:

 

Person       Food Safety Check    FSC ID      # FSC Past Year    Validity     Notes
Bill5/18/2020FSC005N/AValidGrace period, as initial class was within the first year
Bill5/30/2020FSC154N/AValidGrace period, as initial class was within the first year
Bill1/14/2021FSC2472ValidNone
Bill2/25/2023FSC3130InvalidNone
Melinda6/15/2020FSC005N/AValidGrace period, as initial class was within the first year
Melinda7/14/2020FSC154N/AValidGrace period, as initial class was within the first year
Melinda2/28/2021FSC2472ValidNone
Melinda3/15/2021FSC3132ValidNone
Melinda3/10/2022FSC3991InvalidNone
Melinda3/11/2022FSC4002InvalidOnce you're invalid everything after is invalid
Melinda3/12/2022FSC4013InvalidOnce you're invalid everything after is invalid

 

To use Bill as an example:

  • Bill passed his food safety class on 3/18/2020
  • After that Bill can perform food safety checks for 1 year, he performed two (FSC005, FSC154) and they are valid.
  • Next Bill performed food safety check FSC247 and he did indeed perform at least 2 food safety checks in the previous 365 days, so this FSC247 is valid
  • On 2/25/2023 Bill performed food safety check FSC313 and this is invalid because he had 0 food safety checks in the previous 365 days
  • The last point in time Bill had at least 2 audits within the previous 365 days while still being qualified was 5/30/2021. Anything after 5/30/2021 is therefore invalid.

I manually created and calculated the "# FSC Past Year" and "Validity" columns. How can I create them with DAX?

1 ACCEPTED SOLUTION
barritown
Super User
Super User

Hi @calcstuff,

I have a solution for you, but I must challenge your logic.

FSC247 for Bill and FSC247 + FSC313 for Melinda should be considered valid not because they have 2 valid checks in the previous 365 days but because they happened within one year after the training.

So, on the first step we calculate this difference:

barritown_0-1696597142782.png

On the second step we create the Validity column:

barritown_1-1696597185165.png

First step CC in plain text:

Days since training = 
VAR _curPerson = [Person]
VAR _trainingDate = MINX ( FILTER ( People, [Person] = _curPerson ), [Food Safety Class Date] )
VAR _curDate = [Food Safety Check]
RETURN DATEDIFF ( _trainingDate, _curDate, DAY )

Second step CC in plain text:

Validity = 
VAR Trigger1 = IF ( [Days since training] <= 365, TRUE (), FALSE () )
VAR CurrentPerson = [Person]
VAR CurrentDate = [Food Safety Check]
VAR PrevYrDate = [Food Safety Check] - 365
VAR _tbl1 = FILTER ( Checks, [Days since training] > 365 && [Food Safety Check] <= CurrentDate && [Person] = CurrentPerson )
VAR _tbl2 = ADDCOLUMNS ( _tbl1, 
                         "Trigger",
                         VAR CurDate = [Food Safety Check]
                         VAR CurDate_1y = CurDate - 365   
                         RETURN COUNTX ( FILTER ( Checks, [Person] = CurrentPerson && [Food Safety Check] < CurDate && [Food Safety Check] >= CurDate_1y ), [FSC ID] ) )
RETURN IF ( Trigger1, "Valid", IF ( COUNTX ( FILTER ( _tbl2, [Trigger] < 2 ), [FSC ID] ) > 0, "Invalid", "Valid" ) )

Best Regards,

Alexander

My YouTube vlog in English

My YouTube vlog in Russian

 

View solution in original post

1 REPLY 1
barritown
Super User
Super User

Hi @calcstuff,

I have a solution for you, but I must challenge your logic.

FSC247 for Bill and FSC247 + FSC313 for Melinda should be considered valid not because they have 2 valid checks in the previous 365 days but because they happened within one year after the training.

So, on the first step we calculate this difference:

barritown_0-1696597142782.png

On the second step we create the Validity column:

barritown_1-1696597185165.png

First step CC in plain text:

Days since training = 
VAR _curPerson = [Person]
VAR _trainingDate = MINX ( FILTER ( People, [Person] = _curPerson ), [Food Safety Class Date] )
VAR _curDate = [Food Safety Check]
RETURN DATEDIFF ( _trainingDate, _curDate, DAY )

Second step CC in plain text:

Validity = 
VAR Trigger1 = IF ( [Days since training] <= 365, TRUE (), FALSE () )
VAR CurrentPerson = [Person]
VAR CurrentDate = [Food Safety Check]
VAR PrevYrDate = [Food Safety Check] - 365
VAR _tbl1 = FILTER ( Checks, [Days since training] > 365 && [Food Safety Check] <= CurrentDate && [Person] = CurrentPerson )
VAR _tbl2 = ADDCOLUMNS ( _tbl1, 
                         "Trigger",
                         VAR CurDate = [Food Safety Check]
                         VAR CurDate_1y = CurDate - 365   
                         RETURN COUNTX ( FILTER ( Checks, [Person] = CurrentPerson && [Food Safety Check] < CurDate && [Food Safety Check] >= CurDate_1y ), [FSC ID] ) )
RETURN IF ( Trigger1, "Valid", IF ( COUNTX ( FILTER ( _tbl2, [Trigger] < 2 ), [FSC ID] ) > 0, "Invalid", "Valid" ) )

Best Regards,

Alexander

My YouTube vlog in English

My YouTube vlog in Russian

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.