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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Will_Ice
Frequent Visitor

Find if any n out of m rows are true

I'm pretty new to Power BI (and DAX) and I can't figure out how to solve this without manually adding a bunch of columns.

 

I'm trying to evaluate my data to see if 'n' out of 'm' (simplified as 2 out of 3) chronological values meet the specified criteria (i.e., True). The tricky thing is that I could get 2 Trues at anytime. Here is some example data (grouped by Item # and Month):

Will_Ice_1-1726666648561.png

 

 

Each part has its own Average performance and individual values. Essentially, I'm trying to find if at any time a group of the part's Value is higher than its Average. In the example, parts "xxx1" and "xxx2" should both be flagged since they have at least one group where 2 out of 3 entries are high values. Part "xxx3" has the same number of high values, but its groups only have 1 out of 3 so this part should not get flagged at all.

 

So far, I have this code:

 

 

 

Check for 2 out of 3 = 
//Constants
VAR _n_Trues = 2
VAR _m_Range = 3

//Calculates average based on how data has been filtered by the dashboard.
VAR vAVG = AVERAGE([Value])

//Condition #1
//Adds a column to show if the value meets condition.
VAR tbl1 = ADDCOLUMNS(
    tblSource
    , "Check", [Value] > vAVG
)

//Condition #2
//Adds a column to show if 'n' number of values (current AND previous 'm' values) were marked as true.
VAR tbl2 = ADDCOLUMNS(
    tbl1
    //Filters previous table down to the true values and within the target range (should be based on the current value AND the previous 'n'-1 values).
    , "Group Check", COUNTROWS(
        FILTER(
            tbl1
            , [Check] = TRUE()
            && [Month Index] >= MAX([Month Index]) - _m_Range + 1
            && [Month Index] <= MAX([Month Index])
        )
    ) >= _n_Trues
)

//Filters the second table (should still be the same size as the source table before filtering) for any groups that met conditions.
VAR tbl3 = FILTER(
    tbl2
    , [Group Check] = TRUE()
)

RETURN
//If both conditions are true, the third table should have at least 1 entry and trigger a true here.
COUNTROWS(tbl3) > 0

 

 

 

 

This kinda works, but it's not matching my flags. I assume it's because I'm using MAX([Month Index]), but it could be anything.

 

Ideally, I want to use this to flag any parts with suspicious behavior and use these results to prioritize the parts.

4 REPLIES 4
lbendlin
Super User
Super User

Something like this?

 

lbendlin_0-1726516140815.png

 

Close! It has the right number of flags on parts "xxx1" and "xxx2", but the flags seem to be in a different order than I would and "xxx3" shouldn't have any flags. It looks like it skipped over April on "xxx1" too.

 

I just updated the sample data to try and explain a bit more.

Clear as mud.  I don't understand the Group Check logic.  Can you try again please?

I can imagine.  Let me try again. I changed the table in the original post to picture to try and show the logic a little more. If it helps, this is a simplified version of statisical process control rules (or Nelson rules).

 

My current workaround is working with calculated columns. I first create one column that checks if each row's value meets a specific condition (i.e., >Average). I then create another column to check if the result for the current row AND the previous 2 rows (3 rows total) have at least 2 Trues, so it found a group of 3 rows with 2 Trues. Then, I add one more column to check if there were any groups that were marked as True for that specific part.

 

This column approach works alright, but it isn't flexible and doesn't allow me to pass a slicer value or a measure into it.  So, I'm trying to convert this into a measure.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.