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

Count if at least one value meets criteria then set new column to true

Hello folks,

 

In a table, besides other fields, I have 2 columns: one contains an attribute ([Atributo]) and the other contains its respective value ([Valor], "Sim" = Yes and "Não" = No). I need to create a new column that counts if there is at least one value [Valor] = "Sim" (Yes) based on the "cluster" of the column [Mapeamento]. For all values in [Mapeamento] that contains string "RAC 4", if there's at least one [Valor] = "Sim", I need to set the new column to "Sim" for all of its values. For the others, the value should be the same in [Valor]. Something like the below table:

 

AtributoValorMapeamentoNew Column
rac_102SimRAC 10_2Sim

rac_101

SimRAC 10_1Sim
rac_9NãoRAC 9Não
rac_62SimRAC 6_2Sim
rac_61NãoRAC 6_1Não
rac_5SimRAC 5Sim
rac_45NãoRAC 4Sim
rac_44SimRAC 4Sim
rac_43NãoRAC 4Sim
rac_42SimRAC 4Sim
rac_41NãoRAC 4Sim
rac_3NãoRAC 3Não
rac_2NãoRAC 2Não
rac_1SimRAC 1Sim

 

Does anyone have any idea how it can be done?

Thanks in advance,

Thiago.

2 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@Anonymous , the grouping is not clear you need something like

 if(countx(filter(Table,[Mapeamento] =earlier([Mapeamento]) && [Valor] = "Sim"),[Mapeamento] ) >0, "Yes","No")

 

View solution in original post

Anonymous
Not applicable

@amitchandak , @Fowmy :

 

I tried your formula but the result is "Sim" for all rows in the table. 

I've made some changes and got to the expected result, thanks so much for all your help.

 

New_Valor = 
    if(
        countx(
            filter(
                data;
                data[Mapeamento] = earlier(data[Mapeamento]) &&
                data[user_id] = earlier(data[user_id]) && 
                data[Valor] = "Sim" &&
                data[Mapeamento] = "RAC 4"
            ); 
            data[Mapeamento]);
            "Sim";
            data[Valor]
    )

 

Thanks guys!!! You saved my day! 

View solution in original post

5 REPLIES 5
mahoneypat
Employee
Employee

You can use an expression like this in your calculated column:

 

New Column =
IF (
    ISBLANK (
        CALCULATE (
            COUNTROWS ( Table ),
            ALLEXCEPT ( Table, Table[Mapeamento] ),
            Table[Valor] = "Sim"
        )
    ),
    "Nao",
    "Sim"
)

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


BA_Pete
Super User
Super User

Hi @Anonymous ,

 

If you want to push this work away from the data model then you can do this in Power Query as follows:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKkpMjjc0MFLSUQrOzAWSQY7OCoYG8UZKsTowSUN0SUO4pCVQ0O/w4nyonCVcwgzVRDMkA80MUTWZIZlniqLLFC5uYoqqxwQhY4KiBUnCGKcWI1xaDHFpQTPLGC5hhCqBFGyogaYUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Atributo = _t, Valor = _t, Mapeamento = _t]),
    groupMapeamento = Table.Group(Source, {"Mapeamento"}, {{"data", each _, type table [Atributo=text, Valor=text, Mapeamento=text, New Column=text]}}),
    addYourColumn = Table.AddColumn(groupMapeamento, "yourColumn", each if List.Contains(Table.Column([data], "Valor"), "Sim") then "Sim" else "Nao"),
    expandOtherCols = Table.ExpandTableColumn(addYourColumn, "data", {"Atributo", "Valor"}, {"Atributo", "Valor"})
in
    expandOtherCols

In Power Query, go to New Source > Blank Query then in Advanced Editor paste my code over the default code to follow the steps I took.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




amitchandak
Super User
Super User

@Anonymous , the grouping is not clear you need something like

 if(countx(filter(Table,[Mapeamento] =earlier([Mapeamento]) && [Valor] = "Sim"),[Mapeamento] ) >0, "Yes","No")

 

Anonymous
Not applicable

@amitchandak , @Fowmy :

 

I tried your formula but the result is "Sim" for all rows in the table. 

I've made some changes and got to the expected result, thanks so much for all your help.

 

New_Valor = 
    if(
        countx(
            filter(
                data;
                data[Mapeamento] = earlier(data[Mapeamento]) &&
                data[user_id] = earlier(data[user_id]) && 
                data[Valor] = "Sim" &&
                data[Mapeamento] = "RAC 4"
            ); 
            data[Mapeamento]);
            "Sim";
            data[Valor]
    )

 

Thanks guys!!! You saved my day! 

Fowmy
Super User
Super User

@Anonymous 

Add a new column:

 

SimPresent = IF(
    CALCULATE(
    COUNTROWS('Data'),ALLEXCEPT('Data','Data'[Mapeamento]), 'Data'[Valor]="Sim")>0,"Sim","Não")

 

If you are satisfied with my answer, please mark it as a solution so others can easily find it.

Don't forget to give KUDOS ? to replies that help answer your questions


Subscribe to ExcelFort: Learn Power BI, Power Query and Excel

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

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.