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
aa_KF
Helper I
Helper I

Creating a Counter Column

Hello, 

I want to create new column that acts as a Counter for col1, col2, col3. It will count the number of "Yes" values in each row, starting from col1 to col3 values. 

 

Sample Table:

 

Name     |   col1  |   col2  |   col3

James     |   Yes   |            |   Yes   

Alex        |   No   |   No     |  Yes   

John       |  Yes    |  Yes     |  Yes   

George   |   No  |   Yes     |   

  

 

 

Desired Column:

 

Name     |   col1  |   col2  |   col3  |   Counter

James     |   Yes   |            |   Yes   |     2

Alex        |   No   |   No     |  Yes    |     1

John       |  Yes    |  Yes     |  Yes    |     3

George   |   No   |   Yes     |           |     1

 

Thank you and most appreciated.

2 ACCEPTED SOLUTIONS
ChrisMendoza
Resident Rockstar
Resident Rockstar

@aa_KF -

 

More of an exercise in trying new things (for me):

 

I took your sample and unpivoted your [col1],[col2], and [col3] in the Query Editor.

3.PNG

Create a [CountYes] measure as:

 

CountYes = 
CALCULATE(
    COUNTROWS(Table1),
    Table1[Value] = "Yes"
)

Then I used https://www.sqlbi.com/articles/using-concatenatex-in-measures/ as an example/template to create a [Counter Measure] as:

Counter Measure = 
VAR TotalYes = [CountYes]
VAR NameYes =
    ADDCOLUMNS (
        VALUES ( Table1[Name] ),
        "Yes's", [CountYes]
    )
RETURN
    IF (
        ISFILTERED ( Table1[Name] ),
        IF (
            TotalYes > 0,
            CONCATENATEX (
                SELECTCOLUMNS (
                    TOPN (
                        1,
                        NameYes,
                        [Name]
                    ),
                    "Result", [Yes's]
                ),
                [Result]
            )
        ),
        FORMAT([CountYes],"0")
    )

Producting a Matrix visual as:

4.PNG

 

Thank you for the inspiration to try new things!

 

 

 






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



View solution in original post

v-frfei-msft
Community Support
Community Support

Hi @aa_KF ,

 

Please check the following steps as below.

 

1, Unpivot the table in power query as below.

 

Capture.PNG

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMTS1W0lGKBJNQRqxOtJJjDljELx9GwCS88jPy4BoQJEjKPTW/KD0VpgEmEQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [name = _t, col1 = _t, col2 = _t, col3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"col1", type text}, {"col2", type text}, {"col3", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"name"}, "Attribute", "Value")
in
    #"Unpivoted Other Columns"

2. Create a measure based on the new table.

Measure = 
IF (
    ISFILTERED ( 'Table'[Attribute] ),
    MAX ( 'Table'[Value] ),
    CALCULATE ( COUNTROWS ( 'Table' ), FILTER ( 'Table', 'Table'[Value] = "Yes" ) )
)

3. Then we can get the result by creating a matrix.

2.PNG

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

5 REPLIES 5
aa_KF
Helper I
Helper I

@jdbuchanan71  @mussaenda  @ChrisMendoza  @v-frfei-msft  Thank you a lot all!! I really appreicate it. 

 

I'm going to try them all.

 

Thank you again!

v-frfei-msft
Community Support
Community Support

Hi @aa_KF ,

 

Please check the following steps as below.

 

1, Unpivot the table in power query as below.

 

Capture.PNG

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMTS1W0lGKBJNQRqxOtJJjDljELx9GwCS88jPy4BoQJEjKPTW/KD0VpgEmEQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [name = _t, col1 = _t, col2 = _t, col3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"name", type text}, {"col1", type text}, {"col2", type text}, {"col3", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"name"}, "Attribute", "Value")
in
    #"Unpivoted Other Columns"

2. Create a measure based on the new table.

Measure = 
IF (
    ISFILTERED ( 'Table'[Attribute] ),
    MAX ( 'Table'[Value] ),
    CALCULATE ( COUNTROWS ( 'Table' ), FILTER ( 'Table', 'Table'[Value] = "Yes" ) )
)

3. Then we can get the result by creating a matrix.

2.PNG

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
ChrisMendoza
Resident Rockstar
Resident Rockstar

@aa_KF -

 

More of an exercise in trying new things (for me):

 

I took your sample and unpivoted your [col1],[col2], and [col3] in the Query Editor.

3.PNG

Create a [CountYes] measure as:

 

CountYes = 
CALCULATE(
    COUNTROWS(Table1),
    Table1[Value] = "Yes"
)

Then I used https://www.sqlbi.com/articles/using-concatenatex-in-measures/ as an example/template to create a [Counter Measure] as:

Counter Measure = 
VAR TotalYes = [CountYes]
VAR NameYes =
    ADDCOLUMNS (
        VALUES ( Table1[Name] ),
        "Yes's", [CountYes]
    )
RETURN
    IF (
        ISFILTERED ( Table1[Name] ),
        IF (
            TotalYes > 0,
            CONCATENATEX (
                SELECTCOLUMNS (
                    TOPN (
                        1,
                        NameYes,
                        [Name]
                    ),
                    "Result", [Yes's]
                ),
                [Result]
            )
        ),
        FORMAT([CountYes],"0")
    )

Producting a Matrix visual as:

4.PNG

 

Thank you for the inspiration to try new things!

 

 

 






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



mussaenda
Super User
Super User

Counter = (CONTAINSSTRINGEXACT(Table1[col1],"Yes")) + (CONTAINSSTRINGEXACT(Table1[col2],"Yes")) + (CONTAINSSTRINGEXACT(Table1[col3],"Yes"))

I don't know if this is right but it works

jdbuchanan71
Super User
Super User

Try this

Counter = IF (Table1[Col1] = "yes",1,0) + IF (Table1[Col2] = "yes",1,0) + IF (Table1[Col3] = "yes",1,0)

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.