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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
dphillips
Helper IV
Helper IV

Populate a column based on the latest boolean value

A list of students are stored in a database. Every term, the students are rolled over to the next term so in the students table there will be mulitple rows for each student. For each row, the status of a student, called the IBFlag, is one of the fields. See table below. When the student's YearLevel = 11 the IBFlag may change to True. I want to created a column called FinalFlag which is 0 in every row for a student if their IBFlag is always false but, if the IBFlag becomes TRUE in Year 11  then every row for that student right back to when they were in their first year level is set to 1. This will allow me to set a filter of 1 with the FinalFlag and for all the data to show for a student, not just when he was in Year 11 and 12.

 

YearSemesterIDYearLevelIBFlagFinalFlag
201211104215FALSE0
201221104215FALSE0
201311104216FALSE0
201321104216FALSE0
201411104217FALSE0
201421104217FALSE0
201511104218FALSE0
201521104218FALSE0

 

Here is a link to data and an example of what the FinalFlag coulumn should look like.

 

Any help on the best way to do this would be much appreciated.

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Try this:

 

 

FinalFlag =
VAR Test = 
	CALCULATE(
		MAX( TableName[IBFlag] )
		,ALLEXCEPT( TableName, TableName[ID] )
	)
RETURN
IF(
	Test
	,1
	,0
)

 

 

A couple things to note.

 

1) change the TableName to your actual table name.  Columns in DAX statements should always be referenced as TableName[ColumnName], this is best practice.

 

2) TRUE() will evaluate to 1 as a decimal, and FALSE() will evaluate to 0.  So I think this formula will work.

 

Essentially, we're testing to see if the IBFlag is ever true by asking for the MAX.  We're removing the filter context created by using CALCULATE() in a row context by using ALLEXCEPT(), so that the only thing it's checking against is the [ID] column.

 

if the above formula doesn't work, try this one:

 

 

FinalFlag =
VAR Test = 
	CALCULATE(
		MAX( TableName[IBFlag] )
		,ALLEXCEPT( TableName, TableName[ID] )
	)
RETURN
IF(
	Test
	,1
	,0
)

This changes the TRUE() to a 1.

 

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Try this:

 

 

FinalFlag =
VAR Test = 
	CALCULATE(
		MAX( TableName[IBFlag] )
		,ALLEXCEPT( TableName, TableName[ID] )
	)
RETURN
IF(
	Test
	,1
	,0
)

 

 

A couple things to note.

 

1) change the TableName to your actual table name.  Columns in DAX statements should always be referenced as TableName[ColumnName], this is best practice.

 

2) TRUE() will evaluate to 1 as a decimal, and FALSE() will evaluate to 0.  So I think this formula will work.

 

Essentially, we're testing to see if the IBFlag is ever true by asking for the MAX.  We're removing the filter context created by using CALCULATE() in a row context by using ALLEXCEPT(), so that the only thing it's checking against is the [ID] column.

 

if the above formula doesn't work, try this one:

 

 

FinalFlag =
VAR Test = 
	CALCULATE(
		MAX( TableName[IBFlag] )
		,ALLEXCEPT( TableName, TableName[ID] )
	)
RETURN
IF(
	Test
	,1
	,0
)

This changes the TRUE() to a 1.

 

Max did not want to work with boolean values so I had to create a column which stored the True as 1 and False as 0 then use that field in the calculated column. Thanks for your help. Works well. Am able to filter on the IBFlag and it will keep all values, even those before the IBFlag went from False to True...

Anonymous
Not applicable

Ok, use this calculated column then, it will eliminate the need to have a separate column to convert TRUE to 1 and FALSE to 0:

 

FinalFlag =
VAR Test = 
	CALCULATE(
		VALUES( TableName[IBFlag] )
		,ALLEXCEPT( TableName, TableName[ID] )
	)
RETURN
IF(
	TRUE() IN Test
	,1
	,0
)

VALUES() will produce a table of the distinct values in the [IBFlag] column.  So it will be one of these:

 

null (0 rows)

True (1 row)

False (1 row)

True, False (2 rows)

 

Now we check if TRUE() exists in that test table that we created.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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