Forum Discussion

jarivera777's avatar
jarivera777
Frequent Visitor
2 years ago

Counting multiple duplicate columns by a distinct column.

Hi all, I am new to Power BI and I need a solution. I have 5 columns (Impacts 1-5) that have duplicates and blanks, but the primary key (ID) is distinct:

I need to create a distinct column of all my Impact colors 1-5; then I need to calculate each instance of that color. My desired visual would look like this:

Thank you for your help!

3 Replies

  • jarivera777 , You can use UNPIVOT for this

    Unpivot the Impact Columns:

    Go to the "Transform Data" option to open Power Query Editor.
    Select the columns Impact 1 to Impact 5.
    Right-click and choose "Unpivot Columns". This will transform your data from a wide format to a long format, creating two new columns: Attribute (which will contain the original column names) and Value (which will contain the Impact colors).

    Filter the Value column to remove any blank entries.
    Create a Distinct Column:

    In Power Query Editor, select the Value column.
    Go to the "Remove Duplicates" option to create a distinct list of Impact colors.
    Count Instances of Each Color:

    Close and apply the changes to load the data back into Power BI.
    Create a new measure to count the instances of each color:


    DAX
    ColorCount = COUNTROWS('YourTable')



    Create the Visual:
    Use a bar chart or any other suitable visual.
    Set the Value column as the axis and the ColorCount measure as the value.

  • To expand upon bhanu_gautam 's answer, you can do the whole transformation in Power Query. 
    Here is an example code you can paste into the advanced editor of a blank query so you can review each step needed. 
    The end result will look something like this...

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZG9DoMwDITfJTMojfkLa/eqpVuFGKI2YmgEFSri9ZsY4qCGBc6E+2xf2pYJljDgksMJcisbkW7qrl/0vE5q6LUVZzPjaxqXgXVJy8BWBS/JDt6+/UnOFfTQxoyLFWjOrBC8invf5uljon6uVM93wDhGjgPIeICL6vXwVfum4ZMHIqKwQq4IN1GTpZsio+9L24QTSqK0Vc2hiDF/GYQp6AABFW4iBAEgioPEwXLIkC5RWO8DGXm0y/5eD9aoHUHwOib4zGjqOA3WdT8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Closed Date" = _t, #"Qtr/Yr" = _t, #"Impact 1" = _t, #"Impact 2" = _t, #"Impact 3" = _t, #"Impact 4" = _t, #"Impact 5" = _t]),
        #"Changed Type" = 
        Table.TransformColumnTypes(
            Source,
            {
                {"ID", Int64.Type}, {"Closed Date", type date}, {"Qtr/Yr", type text}, {"Impact 1", type text}, {"Impact 2", type text}, {"Impact 3", type text}, {"Impact 4", type text}, {"Impact 5", type text}
            }
        ),
        distinctQuarters = 
        List.Distinct(#"Changed Type"[#"Qtr/Yr"]),
        #"Removed Columns" = 
        Table.RemoveColumns(
            #"Changed Type",
            {"ID", "Closed Date"}
        ),
        #"Unpivoted Other Columns" = 
        Table.UnpivotOtherColumns(
            #"Removed Columns", 
            {"Qtr/Yr"}, 
            "Attribute", 
            "Value"
        ),
        #"Grouped Rows" = 
        Table.Group(
            #"Unpivoted Other Columns", 
            {"Value"}, 
            {
                {"Total", each Table.RowCount(_), Int64.Type}, {"nestedTable", each Table.SelectColumns(_, "Qtr/Yr"), type table [#"Qtr/Yr"=nullable text, Value=text]}
            }
        ),
        groupNested = 
        Table.TransformColumns(
            #"Grouped Rows", 
            {
                {"nestedTable", each Table.Group(_, {"Qtr/Yr"}, {{"qtrCount", each Table.RowCount(_), Int64.Type}})}
            }
        ),
        pivotNested = 
        Table.TransformColumns(
            groupNested, 
            {
                {"nestedTable", each Table.Pivot(_, List.Distinct(_[#"Qtr/Yr"]), "Qtr/Yr", "qtrCount")}
            }
        ),
        expandNested = 
        Table.ExpandTableColumn(
            pivotNested, 
            "nestedTable", 
            distinctQuarters
        ),
        removeBlankColors = 
        Table.SelectRows(
            expandNested, 
            each ([Value] <> "")
        ),
        changeQtrTypes = 
        Table.TransformColumnTypes(
            removeBlankColors,
            {
                {"Q1-2024", Int64.Type}, {"Q2-2024", Int64.Type}, {"Q3-2023", Int64.Type}, {"Q4-2023", Int64.Type}
            }
        )
    in
        changeQtrTypes
    • jarivera777's avatar
      jarivera777
      Frequent Visitor

      Hi There! After I "Removed Duplicates", I noticed the Attribute column only captured Color Impacts 1 & 2, and, in the Value column, it did not capture all of my colors.  When I selected "Closed and Apply", I recieved the following error notification: