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

Need flag columns for categorical variable

Hi,

 

I have the following dataset

 

IDLane
abc100Lane 2
abc100Lane 3
abc200Lane 2
abc300Lane 3
abc400Lane 2
abc400Lane 3

Based on this dataset I want to create 2 flag columns called Lane 2 flag and Lane 3 flag.

If for a particular ID there exists a Lane 2 then all rows for Lane 2 flag should become Yes else all rows for Lane 2 flag (for that ID) should become No.  The same calculation for Lane 3 flag.

Please find below the expected output. 

 

*Rows are color-coded based on ID groups for understanding only

IDLaneLane 2 flagLane 3 flag
abc100Lane 2YesYes
abc100Lane 3YesYes
abc200Lane 2YesNo
abc300Lane 3NoYes
abc400Lane 2YesYes
abc400Lane 3YesYes
abc500Lane 1NoNo
1 ACCEPTED SOLUTION
KHorseman
Community Champion
Community Champion

Using a DAX calculated column, here's what I came up with for Lane 2 Flag:

Lane 2 Flag = IF(
    COUNTROWS(
        FILTER(
            ALL(TableName),
            TableName[ID] = EARLIER(TableName[ID])
            && TableName[Lane] = "Lane 2"
        )
    ) > 0,
    "Yes",
    "No"
)

If that works for you, I'm sure coming up with Lane 3 Flag won't be too difficult from there...

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

4 REPLIES 4
KHorseman
Community Champion
Community Champion

Using a DAX calculated column, here's what I came up with for Lane 2 Flag:

Lane 2 Flag = IF(
    COUNTROWS(
        FILTER(
            ALL(TableName),
            TableName[ID] = EARLIER(TableName[ID])
            && TableName[Lane] = "Lane 2"
        )
    ) > 0,
    "Yes",
    "No"
)

If that works for you, I'm sure coming up with Lane 3 Flag won't be too difficult from there...

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Thanks, This works

Could you also explain the logic as I'm not an advanced user.

Absolutely. The ALL function returns the entire table, which is then filtered down to only rows that match the filtering criteria in the next two lines. EARLIER means on each row, check that the ID column in the filtered ALL table matches the current row's ID. So in more plain English this formula says:

 

"On each row in this table, count all rows in the entire table where the ID is the same as this row, and the Lane = 'Lane 2'. If that count is greater than 0, then there is something in Lane 2, so mark this row as 'Yes'. Otherwise, mark it 'No'."





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Wow, thanks again !

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.